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 pathfloor.hhs
81 lines (68 loc) · 2.21 KB
/
floor.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
/**
* @author Blake Wang
* @params input - The structure with elements to be rounded toward negative infinity. Can be a number, 1d array, 2d array, Mat, Tensor.
* @returns a number rounded to negative infinity if input is a number, a Mat with all elements rounded to negative infinity if input is a 1d array, 2d array, Mat or Tensor.
*
*
*
*/
function floor(input) {
*import math: is_number
*import math: ndim
*import math: deep_copy
if (arguments.length === 0) {
throw new Error('Exception occurred in floor - no argument given');
}
else if (arguments.length > 1) {
throw new Error('Exception occurred in floor - wrong argument number');
}
if (is_number(input)) {
if (input >= 0) {
return parseInt(input);
}
else {
return (input % 1 === 0 ? parseInt(input) : parseInt(input) - 1);
}
}
if (!(Array.isArray(input)) && !(input instanceof Mat) && !(input instanceof Tensor)) {
throw new Error('Exception occurred in floor - input must be a number, array, matrix or tensor');
}
// if input is a Mat, Tensor or 2d array
let in_type = ((input instanceof Mat || input instanceof Tensor));
let raw_in = in_type ? input.clone().val : deep_copy(input);
let result = floor_helper(raw_in);
let dim = ndim(result);
if (dim === 1) {
return result;
}
else if (dim === 2) {
return mat(result);
}
else {
return new Tensor(result);
}
function floor_helper(input) {
let dim = ndim(input);
if (dim === 1) {
let result = deep_copy(input);
for (let i = 0; i < input.length; i++) {
let temp = input[i];
if (temp >= 0) {
result[i] = parseInt(temp);
}
else {
result[i] = (temp % 1 === 0 ? parseInt(temp) : parseInt(temp) - 1);
}
}
return result;
}
else {
let result = [];
for (let i = 0; i < input.length; i++) {
let temp = floor_helper(input[i]);
result.push(temp);
}
return result;
}
}
}