-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlu.js
40 lines (37 loc) · 905 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
'use strict'
var rat = require('big-rat')
var add = require('big-rat/add')
var sub = require('big-rat/sub')
var mul = require('big-rat/mul')
var div = require('big-rat/div')
module.exports = LUdecomp
function LUdecomp(A) {
var n = A.length
var m = A[0].length
if(n !== m) {
return null
}
var L = eye(n)
var U = eye(n)
for(var j = 0; j < n; j++) {
for(var i = j; i < n; i++) {
var sum = rat(0)
for(var k = 0; k < j; k++) {
sum = add(sum, mul(L[k][i], U[j][k]))
}
L[j][i] = sub(A[j][i], sum)
}
var denom = L[j][j]
if(isZero(denom)) {
return null
}
for(var i = j+1; i < n; i++){
var sum = rat(0)
for(var k = 0; k < j; k++){
sum = add(sum, mul(L[k][j], U[i][k]))
}
U[i][j] = div(sub(A[i][j], sum), denom)
}
}
return [L, U]
}