Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
lapytko authored Sep 16, 2019
1 parent 156674e commit f722dd7
Show file tree
Hide file tree
Showing 37 changed files with 1,104 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Gauss.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28010.2019
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gauss", "Gauss\Gauss.csproj", "{57C4D1F2-DD71-4987-9160-5E8260556484}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{57C4D1F2-DD71-4987-9160-5E8260556484}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{57C4D1F2-DD71-4987-9160-5E8260556484}.Debug|Any CPU.Build.0 = Debug|Any CPU
{57C4D1F2-DD71-4987-9160-5E8260556484}.Release|Any CPU.ActiveCfg = Release|Any CPU
{57C4D1F2-DD71-4987-9160-5E8260556484}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {70A69724-3978-4877-99D8-9E91272B1968}
EndGlobalSection
EndGlobal
133 changes: 133 additions & 0 deletions Gauss/Gauss.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
using System;
using System.Collections;
using System.Data;

namespace Gauss {
public class GaussSolutionNotFound : Exception {
public GaussSolutionNotFound(string msg)
: base("Ðåøåíèå íå ìîæåò áûòü íàéäåíî: \r\n" + msg) {
}
}

public class LinearSystem {
private double[,] initial_a_matrix;
private double[,] a_matrix; // ìàòðèöà A
private double[] x_vector; // âåêòîð íåèçâåñòíûõ x
private double[] initial_b_vector;
private double[] b_vector; // âåêòîð b
private double eps; // ïîðÿäîê òî÷íîñòè äëÿ ñðàâíåíèÿ âåùåñòâåííûõ ÷èñåë
private int size; // ðàçìåðíîñòü çàäà÷è


public LinearSystem(double[,] a_matrix, double[] b_vector)
: this(a_matrix, b_vector, 0.0001) {
}
public LinearSystem(double[,] a_matrix, double[] b_vector, double eps) {
if (a_matrix == null || b_vector == null)
throw new ArgumentNullException("Îäèí èç ïàðàìåòðîâ ðàâåí null.");

int b_length = b_vector.Length;
int a_length = a_matrix.Length;
if (a_length != b_length * b_length)
throw new ArgumentException(@"Êîëè÷åñòâî ñòðîê è ñòîëáöîâ â ìàòðèöå A äîëæíî ñîâïàäàòü ñ êîëè÷åñòâîì ýëåìåíòðîâ â âåêòîðå B.");

this.initial_a_matrix = a_matrix; // çàïîìèíàåì èñõîäíóþ ìàòðèöó
this.a_matrix = (double[,])a_matrix.Clone(); // ñ å¸ êîïèåé áóäåì ïðîèçâîäèòü âû÷èñëåíèÿ
this.initial_b_vector = b_vector; // çàïîìèíàåì èñõîäíûé âåêòîð
this.b_vector = (double[])b_vector.Clone(); // ñ åãî êîïèåé áóäåì ïðîèçâîäèòü âû÷èñëåíèÿ
this.x_vector = new double[b_length];
this.size = b_length;
this.eps = eps;

GaussSolve();
}

public double[] XVector {
get {
return x_vector;
}
}

// èíèöèàëèçàöèÿ ìàññèâà èíäåêñîâ ñòîëáöîâ
private int[] InitIndex() {
int[] index = new int[size];
for (int i = 0; i < index.Length; ++i)
index[i] = i;
return index;
}

// ïîèñê ãëàâíîãî ýëåìåíòà â ìàòðèöå
private double FindR(int row, int[] index) {
int max_index = row;
double max = a_matrix[row, index[max_index]];
double max_abs = Math.Abs(max);
for (int cur_index = row + 1; cur_index < size; ++cur_index) {
double cur = a_matrix[row, index[cur_index]];
double cur_abs = Math.Abs(cur);
if (cur_abs > max_abs) {
max_index = cur_index;
max = cur;
max_abs = cur_abs;
}
}

if (max_abs < eps) {
if (Math.Abs(b_vector[row]) > eps)
throw new GaussSolutionNotFound("Ñèñòåìà óðàâíåíèé íåñîâìåñòíà.");
else
throw new GaussSolutionNotFound("Ñèñòåìà óðàâíåíèé èìååò ìíîæåñòâî ðåøåíèé.");
}

// ìåíÿåì ìåñòàìè èíäåêñû ñòîëáöîâ
int temp = index[row];
index[row] = index[max_index];
index[max_index] = temp;

return max;
}

// ðåøåíèå ìåòîäîì Ãàóññà
private void GaussSolve() {
int[] index = InitIndex();
GaussForwardStroke(index);
GaussBackwardStroke(index);
GaussDiscrepancy();
}

// Ïðÿìîé õîä ìåòîäà Ãàóññà
private void GaussForwardStroke(int[] index) {
for (int i = 0; i < size; ++i) {
double r = FindR(i, index);
for (int j = 0; j < size; ++j)
a_matrix[i, j] /= r;
b_vector[i] /= r;
for (int k = i + 1; k < size; ++k) {
double p = a_matrix[k, index[i]];
for (int j = i; j < size; ++j)
a_matrix[k, index[j]] -= a_matrix[i, index[j]] * p;
b_vector[k] -= b_vector[i] * p;
a_matrix[k, index[i]] = 0.0;
}
}
}

// Îáðàòíûé õîä ìåòîäà Ãàóññà
private void GaussBackwardStroke(int[] index) {
for (int i = size - 1; i >= 0; --i) {
double x_i = b_vector[i];
for (int j = i + 1; j < size; ++j)
x_i -= x_vector[index[j]] * a_matrix[i, index[j]];
x_vector[index[i]] = x_i;
}
}

// x - ðåøåíèå óðàâíåíèÿ, ïîëó÷åííîå ìåòîäîì Ãàóññà
private void GaussDiscrepancy() {
for (int i = 0; i < size; ++i) {
double actual_b_i = 0.0;
for (int j = 0; j < size; ++j)
actual_b_i += initial_a_matrix[i, j] * x_vector[j];
}
}
}
}
90 changes: 90 additions & 0 deletions Gauss/Gauss.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="15.0">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.50727</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{57C4D1F2-DD71-4987-9160-5E8260556484}</ProjectGuid>
<OutputType>WinExe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Gauss</RootNamespace>
<AssemblyName>Gauss</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileUpgradeFlags>
</FileUpgradeFlags>
<UpgradeBackupLocation>
</UpgradeBackupLocation>
<OldToolsVersion>2.0</OldToolsVersion>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Deployment" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="MainForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="MainForm.Designer.cs">
<DependentUpon>MainForm.cs</DependentUpon>
</Compile>
<Compile Include="Gauss.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="MainForm.resx">
<SubType>Designer</SubType>
<DependentUpon>MainForm.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<SubType>Designer</SubType>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<None Include="app.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Properties\Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
Loading

0 comments on commit f722dd7

Please sign in to comment.