-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaggregation.go
More file actions
56 lines (51 loc) · 1.15 KB
/
aggregation.go
File metadata and controls
56 lines (51 loc) · 1.15 KB
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
package itt
import "sort"
// Built-in aggregation functions for use with Builder.AggregationFunc().
var (
// AggMean computes the arithmetic mean of tension values.
AggMean AggregationFunc = func(tensions []float64) float64 {
if len(tensions) == 0 {
return 0
}
sum := 0.0
for _, t := range tensions {
sum += t
}
return sum / float64(len(tensions))
}
// AggMax returns the maximum tension value.
AggMax AggregationFunc = func(tensions []float64) float64 {
if len(tensions) == 0 {
return 0
}
max := tensions[0]
for _, t := range tensions[1:] {
if t > max {
max = t
}
}
return max
}
// AggMedian computes the median tension value.
AggMedian AggregationFunc = func(tensions []float64) float64 {
if len(tensions) == 0 {
return 0
}
sorted := make([]float64, len(tensions))
copy(sorted, tensions)
sort.Float64s(sorted)
n := len(sorted)
if n%2 == 0 {
return (sorted[n/2-1] + sorted[n/2]) / 2.0
}
return sorted[n/2]
}
// AggSum returns the sum of all tension values.
AggSum AggregationFunc = func(tensions []float64) float64 {
sum := 0.0
for _, t := range tensions {
sum += t
}
return sum
}
)