Skip to content

Commit 40c8dee

Browse files
committed
Add "simple gauss jordan elimination" example
1 parent bf8458b commit 40c8dee

File tree

6 files changed

+180
-0
lines changed

6 files changed

+180
-0
lines changed

NumericalCalculation/BasicWithCSharp/BasicWithCSharp.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "LossOfTrailingDigits", "Los
99
EndProject
1010
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "CancellationOfSignificantDigits", "CancellationOfSignificantDigits\CancellationOfSignificantDigits.xproj", "{29AECCDE-8736-4B28-A0E3-5B3F36751283}"
1111
EndProject
12+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "SimpleGaussJordanElimination", "SimpleGaussJordanElimination\SimpleGaussJordanElimination.xproj", "{9BD9D6EF-BF88-444C-9485-A9A72C15D375}"
13+
EndProject
1214
Global
1315
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1416
Debug|Any CPU = Debug|Any CPU
@@ -27,6 +29,10 @@ Global
2729
{29AECCDE-8736-4B28-A0E3-5B3F36751283}.Debug|Any CPU.Build.0 = Debug|Any CPU
2830
{29AECCDE-8736-4B28-A0E3-5B3F36751283}.Release|Any CPU.ActiveCfg = Release|Any CPU
2931
{29AECCDE-8736-4B28-A0E3-5B3F36751283}.Release|Any CPU.Build.0 = Release|Any CPU
32+
{9BD9D6EF-BF88-444C-9485-A9A72C15D375}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
33+
{9BD9D6EF-BF88-444C-9485-A9A72C15D375}.Debug|Any CPU.Build.0 = Debug|Any CPU
34+
{9BD9D6EF-BF88-444C-9485-A9A72C15D375}.Release|Any CPU.ActiveCfg = Release|Any CPU
35+
{9BD9D6EF-BF88-444C-9485-A9A72C15D375}.Release|Any CPU.Build.0 = Release|Any CPU
3036
EndGlobalSection
3137
GlobalSection(SolutionProperties) = preSolution
3238
HideSolutionNode = FALSE
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"profiles": {
3+
"SimpleGaussJordanElimination": {
4+
"commandName": "Project",
5+
"commandLineArgs": "> result.txt"
6+
}
7+
}
8+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using System;
2+
3+
namespace SimpleGaussJordanElimination
4+
{
5+
public class SimpleGaussJordanElimination
6+
{
7+
public static void Main(string[] args)
8+
{
9+
Solve();
10+
}
11+
12+
static void WriteEquations(double[,] leftMatrix, double[] rightVector)
13+
{
14+
var rowCount = rightVector.Length;
15+
if (rowCount != leftMatrix.GetLength(0))
16+
{
17+
throw new ArgumentException();
18+
}
19+
20+
for (int i = 0; i < rowCount; i++)
21+
{
22+
for (int j = 0; j < leftMatrix.GetLength(1); j++)
23+
{
24+
Console.Write($"{leftMatrix[i, j],8:F4} ");
25+
}
26+
27+
Console.WriteLine($" | {rightVector[i],8:F4}");
28+
}
29+
}
30+
31+
static void Solve()
32+
{
33+
const int Dimension = 4;
34+
35+
double[,] matrixA = new double[Dimension, Dimension]
36+
{
37+
{ 2, 3, 1, 4 },
38+
{ 4, 1, -3, -2 },
39+
{ -1, 2, 2, 1 },
40+
{ 3, -4, 4, 3 },
41+
};
42+
43+
double[] vectorB = new double[Dimension]
44+
{
45+
10,
46+
0,
47+
4,
48+
6
49+
};
50+
51+
double[] solutionX;
52+
53+
Console.WriteLine("初期状態");
54+
WriteEquations(matrixA, vectorB);
55+
Console.WriteLine();
56+
57+
// 前進消去
58+
for (int i = 0; i < Dimension - 1; i++)
59+
{
60+
for (int j = i + 1; j < Dimension; j++)
61+
{
62+
var s = matrixA[j, i] / matrixA[i, i];
63+
for (int k = i; k < Dimension; k++)
64+
{
65+
matrixA[j, k] -= matrixA[i, k] * s;
66+
}
67+
vectorB[j] -= vectorB[i] * s;
68+
}
69+
}
70+
71+
Console.WriteLine("前進消去後");
72+
WriteEquations(matrixA, vectorB);
73+
Console.WriteLine();
74+
75+
// 後退消去
76+
for (int i = Dimension - 1; i >= 0; i--)
77+
{
78+
vectorB[i] /= matrixA[i, i];
79+
matrixA[i, i] = 1; // matrixA[i, i] /= matrixA[i, i]
80+
for (int j = i - 1; j >= 0; j--)
81+
{
82+
var s = matrixA[j, i]; // s = matrixA[j, i] / matrixA[i, i]
83+
matrixA[j, i] = 0; // matrixA[j, i] -= s
84+
vectorB[j] -= vectorB[i] * s;
85+
}
86+
}
87+
88+
Console.WriteLine("後退消去後");
89+
WriteEquations(matrixA, vectorB);
90+
Console.WriteLine();
91+
92+
// solutionX = new double[Dimension];
93+
// for (int i = 0; i < Dimension; i++)
94+
// {
95+
// solutionX[i] = vectorB[i] / matrixA[i, i];
96+
// }
97+
solutionX = vectorB;
98+
99+
Console.WriteLine("解");
100+
Console.WriteLine(string.Join("\n", System.Linq.Enumerable.Select(solutionX, x => $"{x,8:F4}")));
101+
}
102+
}
103+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
5+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6+
</PropertyGroup>
7+
8+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
9+
<PropertyGroup Label="Globals">
10+
<ProjectGuid>9bd9d6ef-bf88-444c-9485-a9a72c15d375</ProjectGuid>
11+
<RootNamespace>SimpleGaussJordanElimination</RootNamespace>
12+
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
13+
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
14+
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
15+
</PropertyGroup>
16+
17+
<PropertyGroup>
18+
<SchemaVersion>2.0</SchemaVersion>
19+
</PropertyGroup>
20+
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.targets" Condition="'$(VSToolsPath)' != ''" />
21+
</Project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"version": "1.0.0-*",
3+
"buildOptions": {
4+
"emitEntryPoint": true
5+
},
6+
7+
"dependencies": {
8+
"Microsoft.NETCore.App": {
9+
"type": "platform",
10+
"version": "1.0.1"
11+
}
12+
},
13+
14+
"frameworks": {
15+
"netcoreapp1.0": {
16+
"imports": "dnxcore50"
17+
}
18+
}
19+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
初期状態
2+
2.0000 3.0000 1.0000 4.0000 | 10.0000
3+
4.0000 1.0000 -3.0000 -2.0000 | 0.0000
4+
-1.0000 2.0000 2.0000 1.0000 | 4.0000
5+
3.0000 -4.0000 4.0000 3.0000 | 6.0000
6+
7+
前進消去後
8+
2.0000 3.0000 1.0000 4.0000 | 10.0000
9+
0.0000 -5.0000 -5.0000 -10.0000 | -20.0000
10+
0.0000 0.0000 -1.0000 -4.0000 | -5.0000
11+
0.0000 0.0000 0.0000 -30.0000 | -30.0000
12+
13+
後退消去後
14+
1.0000 0.0000 0.0000 0.0000 | 1.0000
15+
0.0000 1.0000 0.0000 0.0000 | 1.0000
16+
0.0000 0.0000 1.0000 0.0000 | 1.0000
17+
0.0000 0.0000 0.0000 1.0000 | 1.0000
18+
19+
20+
1.0000
21+
1.0000
22+
1.0000
23+
1.0000

0 commit comments

Comments
 (0)