- Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathLU.cs
113 lines (94 loc) · 3.61 KB
/
LU.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
usingSystem;
namespaceAlgorithms.Numeric.Decomposition;
/// <summary>
/// LU-decomposition factors the "source" matrix as the product of lower triangular matrix
/// and upper triangular matrix.
/// </summary>
publicstaticclassLu
{
/// <summary>
/// Performs LU-decomposition on "source" matrix.
/// Lower and upper matrices have same shapes as source matrix.
/// Note: Decomposition can be applied only to square matrices.
/// </summary>
/// <param name="source">Square matrix to decompose.</param>
/// <returns>Tuple of lower and upper matrix.</returns>
/// <exception cref="ArgumentException">Source matrix is not square shaped.</exception>
publicstatic(double[,]L,double[,]U)Decompose(double[,]source)
{
if(source.GetLength(0)!=source.GetLength(1))
{
thrownewArgumentException("Source matrix is not square shaped.");
}
varpivot=source.GetLength(0);
varlower=newdouble[pivot,pivot];
varupper=newdouble[pivot,pivot];
for(vari=0;i<pivot;i++)
{
for(vark=i;k<pivot;k++)
{
doublesum=0;
for(varj=0;j<i;j++)
{
sum+=lower[i,j]*upper[j,k];
}
upper[i,k]=source[i,k]-sum;
}
for(vark=i;k<pivot;k++)
{
if(i==k)
{
lower[i,i]=1;
}
else
{
doublesum=0;
for(varj=0;j<i;j++)
{
sum+=lower[k,j]*upper[j,i];
}
lower[k,i]=(source[k,i]-sum)/upper[i,i];
}
}
}
return(L:lower,U:upper);
}
/// <summary>
/// Eliminates linear equations system represented as A*x=b, using LU-decomposition,
/// where A - matrix of equation coefficients, b - vector of absolute terms of equations.
/// </summary>
/// <param name="matrix">Matrix of equation coefficients.</param>
/// <param name="coefficients">Vector of absolute terms of equations.</param>
/// <returns>Vector-solution for linear equations system.</returns>
/// <exception cref="ArgumentException">Matrix of equation coefficients is not square shaped.</exception>
publicstaticdouble[]Eliminate(double[,]matrix,double[]coefficients)
{
if(matrix.GetLength(0)!=matrix.GetLength(1))
{
thrownewArgumentException("Matrix of equation coefficients is not square shaped.");
}
varpivot=matrix.GetLength(0);
varupperTransform=newdouble[pivot,1];// U * upperTransform = coefficients
varsolution=newdouble[pivot];// L * solution = upperTransform
(double[,]l,double[,]u)=Decompose(matrix);
for(vari=0;i<pivot;i++)
{
doublepivotPointSum=0;
for(varj=0;j<i;j++)
{
pivotPointSum+=upperTransform[j,0]*l[i,j];
}
upperTransform[i,0]=(coefficients[i]-pivotPointSum)/l[i,i];
}
for(vari=pivot-1;i>=0;i--)
{
doublepivotPointSum=0;
for(varj=i;j<pivot;j++)
{
pivotPointSum+=solution[j]*u[i,j];
}
solution[i]=(upperTransform[i,0]-pivotPointSum)/u[i,i];
}
returnsolution;
}
}