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
/
Copy pathis_symmetric.hhs
49 lines (40 loc) · 1.96 KB
/
is_symmetric.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
/**
* @author Jason Reynolds
* @param input - the matrix to check if it's symmetric, needs to be square and 2d, can be Mat or JS
* @returns - boolean value, true or false, depending on if if input is symmetric or not
*
* Function to check if the 2d matrix, raw array or Mat object is symmetric
*
*/
//returns true if symmetric, false if not, throws exception if not correct dimensions or 0 dimensions
function is_symmetric(input) {
*import math: transpose
*import math: ndim
*import math: deep_copy
if (arguments.length === 0) {
throw new Error('Exception occurred in is_symmetric - no argument given');
}
else if (arguments.length > 1) {
throw new Error('Exception occurred in is_symmetric - wrong argument number');
}
if (!(Array.isArray(input)) && !(input instanceof Mat) && !(input instanceof Tensor)) {
throw new Error('Exception occurred in is_symmetric - input must be a 2-dimensional JS array, matrix or tensor');
}
//declare raw_in to be input.clone() if input is Mat otherwise let it be input itself
let in_type = (input instanceof Mat) || (input instanceof Tensor);
let raw_in = (in_type) ? input.clone() : deep_copy(input);
//in the case of input being Mat, degrade raw_in to JS array for generalized code
if (in_type) {
raw_in = raw_in.val;
}
//check: is matrix square? is any dimension 0? is ndim not 2? if so throw an error
if ((!(ndim(raw_in) === 2)) || !(raw_in.length === raw_in[0].length) || raw_in.length === 0 || raw_in[0].length === 0) {
throw new Error('Exception occurred in is_symmetric - either not square, not 2d, or 0 dimension row/cols');
}
//if the Mat object doesn't equal it's transpose, return false
if (!(raw_in === (transpose(raw_in)))) {
return false;
}
//otherwise if all above statements failed, then its of valid size and it's transpose is itself; it passed all tests ==> it's symmetric
return true;
}