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 pathmean.hhs
101 lines (85 loc) · 3.11 KB
/
mean.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
/**
* @author Jianan Lin (林家南)
* @param input - the list, matrix, tensor or structure that you want to get the mean(average) of
* @returns - a number representing the mean of all the elements of a list, matrix, tensor or other structure
*
*/
function mean(input) {
*import math: is_number
*import math: flatten
*import math: ndim
*import math: deep_copy
// if there are no arguments, there is an error
if (arguments.length === 0) {
throw new Error('No argument given');
}
// if the first argument is a number, then we should get the mean of a list of numbers
if (is_number(arguments[0])) {
// determine whether there is invalid element
for (let i = 0; i < arguments.length; i++) {
if (!is_number(arguments[i])) {
throw new Error('First argument suggests a list, but there is a non number element in arguments.');
}
}
// sum_array is the sum, n is the number, result is sum / n
let sum_array = 0, n = 0;
for (let i = 0; i < arguments.length; i++) {
sum_array += arguments[i];
n++;
}
let result = sum_array / n;
return result;
}
// at this time, we do not accept arguments = [matrix, axis]
// we will add this function in the future
if (!arguments.length === 1) {
throw new Error('Wrong arguments');
}
// now we're dealing with non-numbers and matrix-like structures:
// declare raw_in, set it to a input clone if its a Matrix / Tensor otherwise input itself
let in_type = (input instanceof Mat) || (input instanceof Tensor);
// here we always maintain raw_in to be an array
let raw_in = (in_type) ? input.clone().val : deep_copy(input);
// declare dimension of raw_in
let dims = ndim(raw_in);
// if the input is 1 or 2 dimension
if (dims < 3) {
// in the case dimension = 1, e.g. [1, 2, 3, 4]
if (dims === 1) {
let sum_array = 0, n = 0;
for (let i = 0; i < raw_in.length; i++) {
sum_array += raw_in[i];
n++;
}
let result = sum_array / n;
return result;
}
// in the case dimension = 2, e.g. [[1, 2], [3, 4]]
// also consider this: [[1, 2], [3], [4, 5, 6]]
else if (dims === 2) {
let sum_array = 0, n = 0;
for (let i = 0; i < raw_in.length; i++) {
for (let j = 0; j < raw_in[i].length; j++) {
sum_array += raw_in[i][j];
n++;
}
}
let result = sum_array / n;
return result;
}
}
// in the case dimension > 2, e.g. [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
else if (dims > 2 && is_number(dims)) {
raw_in = flatten(raw_in);
let sum_array = 0, n = 0;
for (let i = 0; i < raw_in.length; i++) {
sum_array += raw_in[i];
n++;
}
let result = sum_array / n;
return result;
}
else {
throw new Error('Invalid dimension of the first argument');
}
}