-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlu.js
171 lines (149 loc) · 3.8 KB
/
lu.js
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import Matrix from './matrix.js';
import WrapperMatrix2D from './WrapperMatrix2D.js';
export default class LuDecomposition {
constructor(matrix) {
matrix = WrapperMatrix2D.checkMatrix(matrix);
let lu = matrix.clone();
let rows = lu.rows;
let columns = lu.columns;
let pivotVector = new Float64Array(rows);
let pivotSign = 1;
let i, j, k, p, s, t, v;
let LUcolj, kmax;
for (i = 0; i < rows; i++) {
pivotVector[i] = i;
}
LUcolj = new Float64Array(rows);
for (j = 0; j < columns; j++) {
for (i = 0; i < rows; i++) {
LUcolj[i] = lu.get(i, j);
}
for (i = 0; i < rows; i++) {
kmax = Math.min(i, j);
s = 0;
for (k = 0; k < kmax; k++) {
s += lu.get(i, k) * LUcolj[k];
}
LUcolj[i] -= s;
lu.set(i, j, LUcolj[i]);
}
p = j;
for (i = j + 1; i < rows; i++) {
if (Math.abs(LUcolj[i]) > Math.abs(LUcolj[p])) {
p = i;
}
}
if (p !== j) {
for (k = 0; k < columns; k++) {
t = lu.get(p, k);
lu.set(p, k, lu.get(j, k));
lu.set(j, k, t);
}
v = pivotVector[p];
pivotVector[p] = pivotVector[j];
pivotVector[j] = v;
pivotSign = -pivotSign;
}
if (j < rows && lu.get(j, j) !== 0) {
for (i = j + 1; i < rows; i++) {
lu.set(i, j, lu.get(i, j) / lu.get(j, j));
}
}
}
this.LU = lu;
this.pivotVector = pivotVector;
this.pivotSign = pivotSign;
}
isSingular() {
let data = this.LU;
let col = data.columns;
for (let j = 0; j < col; j++) {
if (data.get(j, j) === 0) {
return true;
}
}
return false;
}
solve(value) {
value = Matrix.checkMatrix(value);
let lu = this.LU;
let rows = lu.rows;
if (rows !== value.rows) {
throw new Error('Invalid matrix dimensions');
}
if (this.isSingular()) {
throw new Error('LU matrix is singular');
}
let count = value.columns;
let X = value.subMatrixRow(this.pivotVector, 0, count - 1);
let columns = lu.columns;
let i, j, k;
for (k = 0; k < columns; k++) {
for (i = k + 1; i < columns; i++) {
for (j = 0; j < count; j++) {
X.set(i, j, X.get(i, j) - X.get(k, j) * lu.get(i, k));
}
}
}
for (k = columns - 1; k >= 0; k--) {
for (j = 0; j < count; j++) {
X.set(k, j, X.get(k, j) / lu.get(k, k));
}
for (i = 0; i < k; i++) {
for (j = 0; j < count; j++) {
X.set(i, j, X.get(i, j) - X.get(k, j) * lu.get(i, k));
}
}
}
return X;
}
get determinant() {
let data = this.LU;
if (!data.isSquare()) {
throw new Error('Matrix must be square');
}
let determinant = this.pivotSign;
let col = data.columns;
for (let j = 0; j < col; j++) {
determinant *= data.get(j, j);
}
return determinant;
}
get lowerTriangularMatrix() {
let data = this.LU;
let rows = data.rows;
let columns = data.columns;
let X = new Matrix(rows, columns);
for (let i = 0; i < rows; i++) {
for (let j = 0; j < columns; j++) {
if (i > j) {
X.set(i, j, data.get(i, j));
} else if (i === j) {
X.set(i, j, 1);
} else {
X.set(i, j, 0);
}
}
}
return X;
}
get upperTriangularMatrix() {
let data = this.LU;
let rows = data.rows;
let columns = data.columns;
let X = new Matrix(rows, columns);
for (let i = 0; i < rows; i++) {
for (let j = 0; j < columns; j++) {
if (i <= j) {
X.set(i, j, data.get(i, j));
} else {
X.set(i, j, 0);
}
}
}
return X;
}
get pivotPermutationVector() {
return Array.from(this.pivotVector);
}
}