This repository has been archived by the owner on Sep 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
lu.hhs
52 lines (41 loc) · 1.58 KB
/
lu.hhs
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
/**
* @author Jason Reynolds
* @param input - the matrix to be decomposed into L, U and p
* @returns - 3 objects of type Mat - L, U and p. A lower triangular matrix, upper triangular and p
*/
//takes in raw array or Mat object and return L, U, p as decomposed objects
function lu(input) {
*import math: ndim
*import math: deep_copy
if (arguments.length === 0) {
throw new Error('Exception occurred in lu - no argument given');
}
else if (arguments.length > 1) {
throw new Error('Exception occurred in lu - wrong argument number');
}
if (!(Array.isArray(input)) && !(input instanceof Mat) && !(input instanceof Tensor)) {
throw new Error('Exception occurred in lu - input must be a 2-dimensional JS array, matrix or tensor');
}
//declare raw_in as input.clone() if Mat otherwise input
let in_type = (input instanceof Mat) || (input instanceof Tensor);
let raw_in = (in_type) ? input.clone() : deep_copy(input);
//when a Mat, degrade to JS array
if (in_type) {
raw_in = raw_in.val;
}
//check: is it a vector? if so, throw error
if (!(ndim(raw_in) === 2)) {
throw new Error('Exception occurred in lu - not a 2d matrix');
}
if (raw_in.length < 2 || raw_in[0].length < 2) {
throw new Error('Exception occurred in lu - wrong dimensions, make sure it\'s a matrix');
}
//mathjs lup on the raw, save as result
let result = mathjs.lup(raw_in);
//return the L, U and p objects as Mats
return {
L: mat(result.L),
U: mat(result.U),
p: mat(result.p)
}
}