-
Notifications
You must be signed in to change notification settings - Fork 96
/
util.go
132 lines (116 loc) · 2.3 KB
/
util.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
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
package eaopt
import (
"math"
)
func copyFloat64s(fs []float64) []float64 {
var fsc = make([]float64, len(fs))
copy(fsc, fs)
return fsc
}
func newInts(n uint) []int {
var ints = make([]int, n)
for i := range ints {
ints[i] = i
}
return ints
}
// Divide each element in a float64 slice by a given value.
func divide(floats []float64, value float64) []float64 {
var divided = make([]float64, len(floats))
for i, v := range floats {
divided[i] = v / value
}
return divided
}
// Compute the cumulative sum of a float64 slice.
func cumsum(floats []float64) []float64 {
var summed = make([]float64, len(floats))
copy(summed, floats)
for i := 1; i < len(summed); i++ {
summed[i] += summed[i-1]
}
return summed
}
// Find the minimum between two uints.
func minUint(a, b uint) uint {
if a <= b {
return a
}
return b
}
// Find the minimum between two ints.
func minInt(a, b int) int {
if a <= b {
return a
}
return b
}
// Compute the sum of an int slice.
func sumInts(ints []int) (sum int) {
for _, v := range ints {
sum += v
}
return
}
// Compute the sum of a float64 slice.
func sumFloat64s(floats []float64) (sum float64) {
for _, v := range floats {
sum += v
}
return
}
// Compute the minimum value of a float64 slice.
func minFloat64s(floats []float64) (min float64) {
min = math.Inf(1)
for _, f := range floats {
if f < min {
min = f
}
}
return
}
// Compute the maximum value of a float64 slice.
func maxFloat64s(floats []float64) (max float64) {
max = math.Inf(-1)
for _, f := range floats {
if f > max {
max = f
}
}
return
}
// Compute the mean of a float64 slice.
func meanFloat64s(floats []float64) float64 {
return sumFloat64s(floats) / float64(len(floats))
}
// Compute the variance of a float64 slice.
func varianceFloat64s(floats []float64) float64 {
var (
m = meanFloat64s(floats)
ss float64
)
for _, x := range floats {
ss += math.Pow(x-m, 2)
}
return ss / float64(len(floats))
}
type set map[interface{}]bool
type setInt map[interface{}]int
// union merges two slices and ignores duplicates.
func union(x, y set) set {
var (
u = make(set)
blackList = make(map[interface{}]bool)
)
for i := range x {
u[i] = true
blackList[i] = true
}
for i := range y {
if !blackList[i] {
u[i] = true
blackList[i] = true
}
}
return u
}