Skip to content

Commit f722dd7

Browse files
authored
Add files via upload
1 parent 156674e commit f722dd7

37 files changed

+1104
-0
lines changed

Diff for: Gauss.sln

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28010.2019
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gauss", "Gauss\Gauss.csproj", "{57C4D1F2-DD71-4987-9160-5E8260556484}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{57C4D1F2-DD71-4987-9160-5E8260556484}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{57C4D1F2-DD71-4987-9160-5E8260556484}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{57C4D1F2-DD71-4987-9160-5E8260556484}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{57C4D1F2-DD71-4987-9160-5E8260556484}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {70A69724-3978-4877-99D8-9E91272B1968}
24+
EndGlobalSection
25+
EndGlobal

Diff for: Gauss/Gauss.cs

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
using System;
2+
using System.Collections;
3+
using System.Data;
4+
5+
namespace Gauss {
6+
public class GaussSolutionNotFound : Exception {
7+
public GaussSolutionNotFound(string msg)
8+
: base("Ðåøåíèå íå ìîæåò áûòü íàéäåíî: \r\n" + msg) {
9+
}
10+
}
11+
12+
public class LinearSystem {
13+
private double[,] initial_a_matrix;
14+
private double[,] a_matrix; // ìàòðèöà A
15+
private double[] x_vector; // âåêòîð íåèçâåñòíûõ x
16+
private double[] initial_b_vector;
17+
private double[] b_vector; // âåêòîð b
18+
private double eps; // ïîðÿäîê òî÷íîñòè äëÿ ñðàâíåíèÿ âåùåñòâåííûõ ÷èñåë
19+
private int size; // ðàçìåðíîñòü çàäà÷è
20+
21+
22+
public LinearSystem(double[,] a_matrix, double[] b_vector)
23+
: this(a_matrix, b_vector, 0.0001) {
24+
}
25+
public LinearSystem(double[,] a_matrix, double[] b_vector, double eps) {
26+
if (a_matrix == null || b_vector == null)
27+
throw new ArgumentNullException("Îäèí èç ïàðàìåòðîâ ðàâåí null.");
28+
29+
int b_length = b_vector.Length;
30+
int a_length = a_matrix.Length;
31+
if (a_length != b_length * b_length)
32+
throw new ArgumentException(@"Êîëè÷åñòâî ñòðîê è ñòîëáöîâ â ìàòðèöå A äîëæíî ñîâïàäàòü ñ êîëè÷åñòâîì ýëåìåíòðîâ â âåêòîðå B.");
33+
34+
this.initial_a_matrix = a_matrix; // çàïîìèíàåì èñõîäíóþ ìàòðèöó
35+
this.a_matrix = (double[,])a_matrix.Clone(); // ñ å¸ êîïèåé áóäåì ïðîèçâîäèòü âû÷èñëåíèÿ
36+
this.initial_b_vector = b_vector; // çàïîìèíàåì èñõîäíûé âåêòîð
37+
this.b_vector = (double[])b_vector.Clone(); // ñ åãî êîïèåé áóäåì ïðîèçâîäèòü âû÷èñëåíèÿ
38+
this.x_vector = new double[b_length];
39+
this.size = b_length;
40+
this.eps = eps;
41+
42+
GaussSolve();
43+
}
44+
45+
public double[] XVector {
46+
get {
47+
return x_vector;
48+
}
49+
}
50+
51+
// èíèöèàëèçàöèÿ ìàññèâà èíäåêñîâ ñòîëáöîâ
52+
private int[] InitIndex() {
53+
int[] index = new int[size];
54+
for (int i = 0; i < index.Length; ++i)
55+
index[i] = i;
56+
return index;
57+
}
58+
59+
// ïîèñê ãëàâíîãî ýëåìåíòà â ìàòðèöå
60+
private double FindR(int row, int[] index) {
61+
int max_index = row;
62+
double max = a_matrix[row, index[max_index]];
63+
double max_abs = Math.Abs(max);
64+
for (int cur_index = row + 1; cur_index < size; ++cur_index) {
65+
double cur = a_matrix[row, index[cur_index]];
66+
double cur_abs = Math.Abs(cur);
67+
if (cur_abs > max_abs) {
68+
max_index = cur_index;
69+
max = cur;
70+
max_abs = cur_abs;
71+
}
72+
}
73+
74+
if (max_abs < eps) {
75+
if (Math.Abs(b_vector[row]) > eps)
76+
throw new GaussSolutionNotFound("Ñèñòåìà óðàâíåíèé íåñîâìåñòíà.");
77+
else
78+
throw new GaussSolutionNotFound("Ñèñòåìà óðàâíåíèé èìååò ìíîæåñòâî ðåøåíèé.");
79+
}
80+
81+
// ìåíÿåì ìåñòàìè èíäåêñû ñòîëáöîâ
82+
int temp = index[row];
83+
index[row] = index[max_index];
84+
index[max_index] = temp;
85+
86+
return max;
87+
}
88+
89+
// ðåøåíèå ìåòîäîì Ãàóññà
90+
private void GaussSolve() {
91+
int[] index = InitIndex();
92+
GaussForwardStroke(index);
93+
GaussBackwardStroke(index);
94+
GaussDiscrepancy();
95+
}
96+
97+
// Ïðÿìîé õîä ìåòîäà Ãàóññà
98+
private void GaussForwardStroke(int[] index) {
99+
for (int i = 0; i < size; ++i) {
100+
double r = FindR(i, index);
101+
for (int j = 0; j < size; ++j)
102+
a_matrix[i, j] /= r;
103+
b_vector[i] /= r;
104+
for (int k = i + 1; k < size; ++k) {
105+
double p = a_matrix[k, index[i]];
106+
for (int j = i; j < size; ++j)
107+
a_matrix[k, index[j]] -= a_matrix[i, index[j]] * p;
108+
b_vector[k] -= b_vector[i] * p;
109+
a_matrix[k, index[i]] = 0.0;
110+
}
111+
}
112+
}
113+
114+
// Îáðàòíûé õîä ìåòîäà Ãàóññà
115+
private void GaussBackwardStroke(int[] index) {
116+
for (int i = size - 1; i >= 0; --i) {
117+
double x_i = b_vector[i];
118+
for (int j = i + 1; j < size; ++j)
119+
x_i -= x_vector[index[j]] * a_matrix[i, index[j]];
120+
x_vector[index[i]] = x_i;
121+
}
122+
}
123+
124+
// x - ðåøåíèå óðàâíåíèÿ, ïîëó÷åííîå ìåòîäîì Ãàóññà
125+
private void GaussDiscrepancy() {
126+
for (int i = 0; i < size; ++i) {
127+
double actual_b_i = 0.0;
128+
for (int j = 0; j < size; ++j)
129+
actual_b_i += initial_a_matrix[i, j] * x_vector[j];
130+
}
131+
}
132+
}
133+
}

Diff for: Gauss/Gauss.csproj

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="15.0">
2+
<PropertyGroup>
3+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
4+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
5+
<ProductVersion>8.0.50727</ProductVersion>
6+
<SchemaVersion>2.0</SchemaVersion>
7+
<ProjectGuid>{57C4D1F2-DD71-4987-9160-5E8260556484}</ProjectGuid>
8+
<OutputType>WinExe</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>Gauss</RootNamespace>
11+
<AssemblyName>Gauss</AssemblyName>
12+
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
13+
<FileUpgradeFlags>
14+
</FileUpgradeFlags>
15+
<UpgradeBackupLocation>
16+
</UpgradeBackupLocation>
17+
<OldToolsVersion>2.0</OldToolsVersion>
18+
<TargetFrameworkProfile />
19+
</PropertyGroup>
20+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
21+
<DebugSymbols>true</DebugSymbols>
22+
<DebugType>full</DebugType>
23+
<Optimize>false</Optimize>
24+
<OutputPath>bin\Debug\</OutputPath>
25+
<DefineConstants>DEBUG;TRACE</DefineConstants>
26+
<ErrorReport>prompt</ErrorReport>
27+
<WarningLevel>4</WarningLevel>
28+
<Prefer32Bit>false</Prefer32Bit>
29+
</PropertyGroup>
30+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
31+
<DebugType>pdbonly</DebugType>
32+
<Optimize>true</Optimize>
33+
<OutputPath>bin\Release\</OutputPath>
34+
<DefineConstants>TRACE</DefineConstants>
35+
<ErrorReport>prompt</ErrorReport>
36+
<WarningLevel>4</WarningLevel>
37+
<Prefer32Bit>false</Prefer32Bit>
38+
</PropertyGroup>
39+
<ItemGroup>
40+
<Reference Include="System" />
41+
<Reference Include="System.Data" />
42+
<Reference Include="System.Deployment" />
43+
<Reference Include="System.Drawing" />
44+
<Reference Include="System.Windows.Forms" />
45+
<Reference Include="System.Xml" />
46+
</ItemGroup>
47+
<ItemGroup>
48+
<Compile Include="MainForm.cs">
49+
<SubType>Form</SubType>
50+
</Compile>
51+
<Compile Include="MainForm.Designer.cs">
52+
<DependentUpon>MainForm.cs</DependentUpon>
53+
</Compile>
54+
<Compile Include="Gauss.cs" />
55+
<Compile Include="Program.cs" />
56+
<Compile Include="Properties\AssemblyInfo.cs" />
57+
<EmbeddedResource Include="MainForm.resx">
58+
<SubType>Designer</SubType>
59+
<DependentUpon>MainForm.cs</DependentUpon>
60+
</EmbeddedResource>
61+
<EmbeddedResource Include="Properties\Resources.resx">
62+
<Generator>ResXFileCodeGenerator</Generator>
63+
<SubType>Designer</SubType>
64+
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
65+
</EmbeddedResource>
66+
<None Include="app.config" />
67+
<None Include="Properties\Settings.settings">
68+
<Generator>SettingsSingleFileGenerator</Generator>
69+
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
70+
</None>
71+
<Compile Include="Properties\Resources.Designer.cs">
72+
<AutoGen>True</AutoGen>
73+
<DesignTime>True</DesignTime>
74+
<DependentUpon>Resources.resx</DependentUpon>
75+
</Compile>
76+
<Compile Include="Properties\Settings.Designer.cs">
77+
<AutoGen>True</AutoGen>
78+
<DependentUpon>Settings.settings</DependentUpon>
79+
<DesignTimeSharedInput>True</DesignTimeSharedInput>
80+
</Compile>
81+
</ItemGroup>
82+
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
83+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
84+
Other similar extension points exist, see Microsoft.Common.targets.
85+
<Target Name="BeforeBuild">
86+
</Target>
87+
<Target Name="AfterBuild">
88+
</Target>
89+
-->
90+
</Project>

0 commit comments

Comments
 (0)