This repository has been archived by the owner on Aug 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
116 lines (103 loc) · 2.04 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
package gago
import (
"math"
)
func newInts(n int) []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 ints.
func min(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
// 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
}