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
/
det.hhs
64 lines (51 loc) · 2.14 KB
/
det.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
53
54
55
56
57
58
59
60
61
62
63
/**
* @author Jason Reynolds
* @param input the square matrix to take in and find the determinant of
* @returns the determinant of the matrix, as a number
*
* functions finds determinant of a matrix, wrapper functions from mathjs
*/
function det(input) {
*import math: ndim
// wrong argument number
if (arguments.length === 0) {
throw new Error('Exception occurred in det - no argument given');
}
else if (arguments.length > 1) {
throw new Error('Exception occurred in det - wrong argument number');
}
// type check
if (!(Array.isArray(input)) && !(input instanceof Mat) && !(input instanceof Tensor)) {
throw new Error('Exception occurred in det - input is not a Mat, Tensor or JS Array');
}
// dimension check
if (ndim(input) !== 2) {
throw new Error('Exception occurred in det - input is not 2-dimensional');
}
//set raw_in to be either a clone of Mat or input itself if JS array
let in_type = (input instanceof Mat) || (input instanceof Tensor);
let raw_in = (in_type) ? input.clone() : input;
//in the case that it's an input of Mat
if (input instanceof Mat) {
//check the dimensions: are rows, cols 0? is it not square? if so, throw error
if (input.rows === 0 || input.cols === 0 || !(input.rows === input.cols)) {
throw new Error('Exception occurred in det - wrong dimensions for input');
}
//since it's Mat, degrade to JS array
raw_in = raw_in.val;
//plug it into mathjs.det and return
return mathjs.det(raw_in);
}
//in the case that it's a JS array or Tensor
else {
//again check the input. is any dim 0? is it not square? if so, throw error.
if (in_type) {
input = raw_in.val;
}
if (input.length === 0 || input[0].length === 0 || !(input.length === input[0].length)) {
throw new Error('Exception occurred in det - wrong dimensions for input');
}
//plug in input clone into mathjs.det and return. Technically since det doesn't modify you could use parameter 'input'
return mathjs.det(input);
}
}