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
/
min.hhs
67 lines (55 loc) · 1.91 KB
/
min.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
64
65
66
67
/**
* @OriginalAuthor Jason Reynolds
* @modifiedBy Blake Wang
* @param input - the structure to find the minimum value of. Can be a list, 1d array, 2d array, Mat, Tensor, number, etc
* min(1, 2, 3) or max([1, 2, 3])
* @returns - a number that is the minimum value in the structure
*
* Function that finds the minimum value of a list, matrix or general matrix like structure
*
*/
function min(input) {
*import math: is_number
*import math: flatten
if (arguments.length === 0) {
throw new Error('Exception occurred in min - no argument given');
}
// min(1, 2, 3)
if (arguments.length > 1) {
let result = arguments[0];
for (let i = 0; i < arguments.length; i++) {
if (!is_number(arguments[i])) {
throw new Error('Exception occurred in min - only numbers can be compared');
}
if (result > arguments[i]) {
result = arguments[i];
}
}
return result;
}
if (is_number(input)) {
return input;
}
if (!(Array.isArray(input)) && !(input instanceof Mat) && !(input instanceof Tensor)) {
throw new Error('Exception occurred in min - input can only be number, JS array, matrix or tensor');
}
//declare raw_in as the clone of input if a Mat/Tensor otherwise, input itself
let in_type = (input instanceof Mat || input instanceof Tensor);
let raw_in = (in_type) ? input.clone().val : input;
// let raw_in be a 1-d array
raw_in = flatten(raw_in);
// in case raw_in === []
if (raw_in.length === 0) {
return 0;
}
let result = raw_in[0];
for (let i = 0; i < raw_in.length; i++) {
if (!is_number(raw_in[i])) {
throw new Error('Exception occurred in min - only numbers can be compared');
}
if (result > raw_in[i]) {
result = raw_in[i];
}
}
return result;
}