-
Notifications
You must be signed in to change notification settings - Fork 0
/
_LU.js
43 lines (34 loc) · 981 Bytes
/
_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
const { isArray, column, columns, row, rows, interchangeRows } = require('./utils');
const identity = require('./identity');
module.exports = function _LU(m) {
const _rows = rows(m);
const cols = columns(m);
const lMatrix = identity(_rows);
let j = 0;
for (let i = 0; i < _rows; i++) {
let k = i;
let pivot = m[k][j];
while (pivot === 0) {
k++;
if (k >= _rows) return [0,0]; // means no LU transformation
pivot = m[k][j];
}
// if (i !== k) {
// interchangeRows(m, i, k);
// }
// if (k > _rows - 1)
// m[i] = m[i].map(x => x / pivot);
for (let u = i + 1; u < _rows; u++) {
let underPivot = m[u][j];
if (underPivot === 0) continue;
if (k <= _rows - 1) {
underPivot /= pivot;
}
lMatrix[u][j] = underPivot;
m[u] = m[i].map((x, z) => (m[u][z] - (x * underPivot)));
}
j++;
if (j === cols - 1) break;
}
return [lMatrix , m]; // returns [LT , UT]
}