-
Notifications
You must be signed in to change notification settings - Fork 1
/
buildutil.go
214 lines (181 loc) · 3.1 KB
/
buildutil.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package glasso
import (
"fmt"
"math"
)
func round(val float64, places int) float64 {
var round float64
pow := math.Pow(10, float64(places))
digit := pow * val
_, div := math.Modf(digit)
if div >= 0.5 {
round = math.Ceil(digit)
} else {
round = math.Floor(digit)
}
return round / pow
}
func roundAll(x []float64) []float64 {
f := make([]float64, len(x))
for i, val := range x {
f[i] = round(val, 3)
}
return f
}
func sum(x []float64) float64 {
y := 0.0
for _, z := range x {
y += z
}
return y
}
func mult(x []float64) float64 {
y := 1.0
for _, z := range x {
y *= z
}
return y
}
func mean(x []float64) float64 {
return sum(x) / float64(len(x))
}
func variance(x []float64) float64 {
n := float64(len(x))
if n == 1 {
return 0
} else if n < 2 {
n = 2
}
m := mean(x)
ss := 0.0
for _, v := range x {
ss += math.Pow(v-m, 2.0)
}
return ss / (n - 1)
}
func sd(x []float64) float64 {
return math.Sqrt(variance(x))
}
func prod(x, y []float64) []float64 {
p := make([]float64, len(x))
for i, _ := range x {
p[i] = x[i] * y[i]
}
return p
}
func cor(x, y []float64) float64 {
n := float64(len(x))
xy := prod(x, y)
sx := sd(x)
sy := sd(y)
mx := mean(x)
my := mean(y)
return (sum(xy) - n*mx*my) / ((n - 1) * sx * sy)
}
func rep(val float64, times int) []float64 {
out := make([]float64, times)
for i := 0; i < times; i++ {
out[i] = val
}
return out
}
func standardize(x []float64) []float64 {
m := mean(x)
dev := sd(x)
cp := make([]float64, len(x))
copy(x, cp)
for i := 0; i < len(cp); i++ {
cp[i] -= m
cp[i] /= dev
}
return cp
}
func normalize(x []float64) []float64 {
s := sum(x)
cp := make([]float64, len(x))
for i := 0; i < len(cp); i++ {
cp[i] = x[i] / s
}
return cp
}
func subtractMean(x []float64) []float64 {
m := mean(x)
cp := make([]float64, len(x))
for i := 0; i < len(cp); i++ {
cp[i] = x[i] - m
}
return cp
}
func max(x []float64) float64 {
m := x[0]
for i, _ := range x {
if x[i] > m {
m = x[i]
}
}
return m
}
func sign(x float64) float64 {
if x > 0.0 {
return 1.0
}
return -1.0
}
func addSlice(x []float64, val float64) []float64 {
cp := make([]float64, len(x))
for i := range cp {
cp[i] = x[i] + val
}
return cp
}
func subSlice(x []float64, val float64) []float64 {
cp := make([]float64, len(x))
for i := range cp {
cp[i] = x[i] - val
}
return cp
}
func multSlice(x []float64, val float64) []float64 {
cp := make([]float64, len(x))
for i := range cp {
cp[i] = x[i] * val
}
return cp
}
func diff(x, y []float64) []float64 {
if len(x) != len(y) {
return nil
}
cp := make([]float64, len(x))
for i := range y {
cp[i] = x[i] - y[i]
}
return cp
}
func containsString(x string, values []string) bool {
for _, v := range values {
if v == x {
return true
}
}
return false
}
func containsInt(x int, values []int) bool {
for _, v := range values {
if v == x {
return true
}
}
return false
}
func seq(start, end, by int) []int {
if start > end || by > (end-start) {
fmt.Println("fuck")
return nil
}
s := make([]int, (end-start)/by+1)
for i := range s {
s[i] = start + i*by
}
return s
}