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 pathbounds.hhs
146 lines (122 loc) · 4.26 KB
/
bounds.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* @Author Jianan Lin (林家南)
* @param input - a series of number, or an array, matrix, tensor to get the min and max
* @param dim - 0 (bound of all), 1 (bound of column), 2(bound of row)
* @returns - 1-d array or 2-d matrix [min, max], [[min1, min2], [max1, max2]], [[min1, max1], [min2, max2]]
*
*/
function bounds(input, dim = 0) {
*import math: ndim
*import math: deep_copy
*import math: flatten
if (arguments.length === 0) {
throw new Error('Exception occurred in bounds - no argument given');
}
// bounds(1, 2, 3, 4, 5, 6)
if (typeof arguments[0] === 'number') {
let result = [arguments[0], arguments[0]];
for (let i = 1; i < arguments.length; i++) {
if (typeof arguments[i] === 'number') {
if (arguments[i] > result[1]) {
result[1] = arguments[i];
}
else if (arguments[i] < result[0]) {
result[0] = arguments[i];
}
}
else {
throw new Error('Exception occurred in bounds - argument must be number');
}
}
return result;
}
// bounds([1, 2, 3], 0)
if (arguments.length > 2) {
throw new Error('Exception occurred in bounds - wrong argument number');
}
if (!(Array.isArray(input)) && !(input instanceof Mat) && !(input instanceof Tensor)) {
throw new Error('Exception occurred in bounds - input must be an array, matrix or tensor');
}
if (dim !== 0 && dim !== 1 && dim !== 2) {
throw new Error('Exception occurred in bounds - dim must be 0, 1 or 2');
}
let in_type = (input instanceof Mat) || (input instanceof Tensor);
let raw_in = in_type ? input.clone().val : deep_copy(input);
// find bounds of all
if (dim === 0) {
raw_in = flatten(raw_in);
if (raw_in.length === 0) {
return [0, 0];
}
let result = [raw_in[0], raw_in[0]];
for (let i = 1; i < raw_in.length; i++) {
if (raw_in[i] > result[1]) {
result[1] = raw_in[i];
}
else if (raw_in[i] < result[0]) {
result[0] = raw_in[i];
}
}
return result;
}
// dim = 1, get the bound of each column
else if (dim === 1) {
if (ndim(raw_in) !== 2) {
throw new Error('Exception occurred in bounds - input must be 2-dimensional');
}
let m = raw_in.length;
let n = raw_in[0].length;
if (m * n === 0) {
throw new Error('Exception occurred in bounds - wrong size of input');
}
// raw_in = [[1, 2, 3, 4, 5, 6]]
else if (m === 1) {
return mat([raw_in[0], raw_in[0]]);
}
let result = [];
let min_result = [];
let max_result = [];
for (let j = 0; j < n; j++) {
let min_temp = raw_in[0][j];
let max_temp = raw_in[0][j];
for (let i = 1; i < m; i++) {
if (raw_in[i][j] > max_temp) {
max_temp = raw_in[i][j];
}
else if (raw_in[i][j] < min_temp) {
min_temp = raw_in[i][j];
}
}
min_result.push(min_temp);
max_result.push(max_temp);
}
result = [min_result, max_result];
return mat(result);
}
// dim = 2, get the bound of each row
else {
if (ndim(raw_in) !== 2) {
throw new Error('Exception occurred in bounds - input must be 2-dimensional');
}
let m = raw_in.length;
let n = raw_in[0].length;
if (m * n === 0) {
throw new Error('Exception occurred in bounds - wrong size of input');
}
let result = [];
for (let i = 0; i < m; i++) {
let min_temp = raw_in[i][0];
let max_temp = raw_in[i][0];
for (let j = 1; j < n; j++) {
if (raw_in[i][j] > max_temp) {
max_temp = raw_in[i][j];
}
else if (raw_in[i][j] < min_temp) {
min_temp = raw_in[i][j];
}
}
result.push([min_temp, max_temp]);
}
return mat(result);
}
}