-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathNumJS.MatLU.js
188 lines (159 loc) · 4.25 KB
/
NumJS.MatLU.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* NumJS -- a JavaScript library for numerical computing
*
* Copyright (C) 2011 Clifford Wolf <clifford@clifford.at>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
"use strict";
NumJS.GenericPLU = function(P, L, U)
{
this.P = P;
this.L = L;
this.U = U;
};
NumJS.GenericMatrix.prototype.LU = function()
{
if (this.rows != this.cols)
throw "NumJS.MatLU dimension mismatch";
if ("LU" in this.cache)
return this.cache["LU"];
var A, P, L, U;
var n = this.rows;
// in this LU solver P is just the identity
P = NumJS.PMAT(n);
L = NumJS.MAT(n, n);
A = this.copy();
U = this.copy();
for (var k = 0; k < n; k++)
{
var pivot = U.get(k, k);
if (NumJS.ABS(pivot) < NumJS.eps)
return null;
L.set(k, k, 1);
for (var i = k+1; i < n; i++) {
L.set(i, k, NumJS.DIV(U.get(i, k), pivot));
for (var j = k+1; j < n; j++)
U.set(i, j, NumJS.SUB(U.get(i, j), NumJS.MUL(L.get(i, k), U.get(k, j))));
U.set(i, k, 0);
}
}
this.cache["LU"] = new NumJS.GenericPLU(P, L, U);
return this.cache["LU"];
};
NumJS.GenericMatrix.prototype.PLU = function()
{
if (this.rows != this.cols)
throw "NumJS.MatLU dimension mismatch";
if ("PLU" in this.cache)
return this.cache["PLU"];
var A, P, L, U;
var n = this.rows;
P = NumJS.PMAT(n);
L = NumJS.MAT(n, n);
A = this.copy();
U = this.copy();
for (var k = 0; k < n; k++)
{
// find the correct row
var pivot = U.get(k, k);
var pivot_i = k;
var pivot_abs = NumJS.ABS(pivot);
for (var i = k+1; i < n; i++) {
var pv = U.get(i, k)
var pa = NumJS.ABS(pv);
if (pa > pivot_abs) {
pivot = pv;
pivot_i = i;
pivot_abs = pa;
}
}
if (pivot_i != k) {
// perform row pivoting in L
for (var j = 0; j < k; j++) {
var t1 = L.get(k, j);
var t2 = L.get(pivot_i, j);
L.set(k, j, t2);
L.set(pivot_i, j, t1);
}
// perform row pivoting in U
for (var j = k; j < n; j++) {
var t1 = U.get(k, j);
var t2 = U.get(pivot_i, j);
U.set(k, j, t2);
U.set(pivot_i, j, t1);
}
P.pivot_row(pivot_i, k);
}
if (NumJS.ABS(pivot) < NumJS.eps)
return null;
L.set(k, k, 1);
for (var i = k+1; i < n; i++) {
L.set(i, k, NumJS.DIV(U.get(i, k), pivot));
for (var j = k+1; j < n; j++)
U.set(i, j, NumJS.SUB(U.get(i, j), NumJS.MUL(L.get(i, k), U.get(k, j))));
U.set(i, k, 0);
}
}
this.cache["PLU"] = new NumJS.GenericPLU(P, L, U);
return this.cache["PLU"];
};
NumJS.GenericPLU.prototype.det = function()
{
var result = this.P.sign;
for (var k = 0; k < this.U.rows; k++)
result = NumJS.MUL(result, this.U.get(k, k));
return result;
}
NumJS.GenericPLU.prototype.solve = function(Y)
{
if (!(Y instanceof NumJS.GenericMatrix))
throw "NumJS.MatLU type error";
var n = this.P.rows;
if (n != Y.rows)
throw "NumJS.MatLU dimension mismatch";
var X = NumJS.MAT(n, Y.cols);
for (var k = 0; k < Y.cols; k++)
{
// LU*x = y => L*c = y and U*x = c
var y = new Array();
var c = new Array();
var x = new Array();
// Initialize y, c and x
for (var i = 0; i < n; i++) {
y[this.P.data[i]] = Y.get(i, k);
c[i] = x[i] = 0;
}
// Solve L*c = y
for (var i = 0; i < n; i++) {
var pivot = this.L.get(i, i);
c[i] = NumJS.DIV(y[i], pivot);
for (var j = i+1; j < n; j++)
y[j] = NumJS.SUB(y[j], NumJS.MUL(c[i], this.L.get(j, i)));
}
// Solve U*x = c
for (var i = n-1; i >= 0; i--) {
var pivot = this.U.get(i, i);
x[i] = NumJS.DIV(c[i], pivot);
for (var j = i-1; j >= 0; j--)
c[j] = NumJS.SUB(c[j], NumJS.MUL(x[i], this.U.get(j, i)));
}
// Store x in X
for (var i = 0; i < n; i++)
X.set(i, k, x[i]);
}
return X;
}