- Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathGaussJordanElimination.cs
152 lines (132 loc) · 4.37 KB
/
GaussJordanElimination.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
usingSystem;
namespaceAlgorithms.Numeric;
/// <summary>
/// Algorithm used to find the inverse of any matrix that can be inverted.
/// </summary>
publicclassGaussJordanElimination
{
privateintRowCount{get;set;}
/// <summary>
/// Method to find a linear equation system using gaussian elimination.
/// </summary>
/// <param name="matrix">The key matrix to solve via algorithm.</param>
/// <returns>
/// whether the input matrix has a unique solution or not.
/// and solves on the given matrix.
/// </returns>
publicboolSolve(double[,]matrix)
{
RowCount=matrix.GetUpperBound(0)+1;
if(!CanMatrixBeUsed(matrix))
{
thrownewArgumentException("Please use a n*(n+1) matrix with Length > 0.");
}
varpivot=PivotMatrix(refmatrix);
if(!pivot)
{
returnfalse;
}
Elimination(refmatrix);
returnElementaryReduction(refmatrix);
}
/// <summary>
/// To make simple validation of the matrix to be used.
/// </summary>
/// <param name="matrix">Multidimensional array matrix.</param>
/// <returns>
/// True: if algorithm can be use for given matrix;
/// False: Otherwise.
/// </returns>
privateboolCanMatrixBeUsed(double[,]matrix)=>matrix?.Length==RowCount*(RowCount+1)&&RowCount>1;
/// <summary>
/// To prepare given matrix by pivoting rows.
/// </summary>
/// <param name="matrix">Input matrix.</param>
/// <returns>Matrix.</returns>
privateboolPivotMatrix(refdouble[,]matrix)
{
for(varcol=0;col+1<RowCount;col++)
{
if(matrix[col,col]==0)
{
// To find a non-zero coefficient
varrowToSwap=FindNonZeroCoefficient(refmatrix,col);
if(matrix[rowToSwap,col]!=0)
{
vartmp=newdouble[RowCount+1];
for(vari=0;i<RowCount+1;i++)
{
// To make the swap with the element above.
tmp[i]=matrix[rowToSwap,i];
matrix[rowToSwap,i]=matrix[col,i];
matrix[col,i]=tmp[i];
}
}
else
{
// To return that the matrix doesn't have a unique solution.
returnfalse;
}
}
}
returntrue;
}
privateintFindNonZeroCoefficient(refdouble[,]matrix,intcol)
{
varrowToSwap=col+1;
// To find a non-zero coefficient
for(;rowToSwap<RowCount;rowToSwap++)
{
if(matrix[rowToSwap,col]!=0)
{
returnrowToSwap;
}
}
returncol+1;
}
/// <summary>
/// Applies REF.
/// </summary>
/// <param name="matrix">Input matrix.</param>
privatevoidElimination(refdouble[,]matrix)
{
for(varsrcRow=0;srcRow+1<RowCount;srcRow++)
{
for(vardestRow=srcRow+1;destRow<RowCount;destRow++)
{
vardf=matrix[srcRow,srcRow];
varsf=matrix[destRow,srcRow];
for(vari=0;i<RowCount+1;i++)
{
matrix[destRow,i]=matrix[destRow,i]*df-matrix[srcRow,i]*sf;
}
}
}
}
/// <summary>
/// To continue reducing the matrix using RREF.
/// </summary>
/// <param name="matrix">Input matrix.</param>
/// <returns>True if it has a unique solution; false otherwise.</returns>
privateboolElementaryReduction(refdouble[,]matrix)
{
for(varrow=RowCount-1;row>=0;row--)
{
varelement=matrix[row,row];
if(element==0)
{
returnfalse;
}
for(vari=0;i<RowCount+1;i++)
{
matrix[row,i]/=element;
}
for(vardestRow=0;destRow<row;destRow++)
{
matrix[destRow,RowCount]-=matrix[destRow,row]*matrix[row,RowCount];
matrix[destRow,row]=0;
}
}
returntrue;
}
}