-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats.go
74 lines (66 loc) · 1.42 KB
/
stats.go
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
package neuro
import (
"fmt"
"math"
)
// mean computes the mean of a float32 slice.
//
// Parameters:
// - data: the slice of float32 values. Must not be empty.
//
// Returns:
// - float32: the mean
// - error: an error if one occurred, e.g., the slice was empty
func mean(data[]float32) (float32, error) {
if len(data) == 0 {
err := fmt.Errorf("mean: empty slice")
return 0.0, err
}
var sum float32 = 0.0
for _, v := range data {
sum += v
}
return sum / float32(len(data)), nil
}
// max computes the maximum of a float32 slice.
//
// Parameters:
// - data: the slice of float32 values. Must not be empty.
//
// Returns:
// - float32: the maximum
// - error: an error if one occurred, e.g., the slice was empty
func max(data[]float32) (float32, error) {
if len(data) == 0 {
err := fmt.Errorf("max: empty slice")
return 0.0, err
}
var max float32 = - math.MaxFloat32
for _, v := range data {
if v > max {
max = v
}
}
return max, nil
}
// min computes the minimum of a float32 slice.
//
// Parameters:
// - data: the slice of float32 values. Must not be empty.
//
// Returns:
// - float32: the minimum
// - error: an error if one occurred, e.g., the slice was empty
func min(data[]float32) (float32, error) {
if len(data) == 0 {
err := fmt.Errorf("min: empty slice")
return 0.0, err
}
var min float32 = math.MaxFloat32
for _, v := range data {
if v < min {
min = v
}
}
return min, nil
}