-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmath.go
180 lines (148 loc) · 3.94 KB
/
math.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
/*
* gouda/point: an n-dimensional point & tools
*
* Copyright (C) 2018 Pawel Foremski <[email protected]>
* Licensed to you under GNU GPL v3
*/
package point
import "math"
import "sort"
// Zero() sets all coordinates to zero
func (p *Point) Zero() {
for i := range p.V { p.V[i] = 0 }
}
// Add() adds p2 to p1
func (p1 *Point) Add(p2 *Point) {
for i := 0; i < len(p1.V) && i < len(p2.V); i++ {
p1.V[i] += p2.V[i]
}
}
// Mul() multiplies all coordinates in p by fact
func (p *Point) Mul(fact float64) {
for i := 0; i < len(p.V); i++ {
p.V[i] *= fact
}
}
// Min() finds the minimum value on any axis
func (p *Point) Min() float64 {
min := p.V[0]
for i := 1; i < len(p.V); i++ {
if p.V[i] < min { min = p.V[i] }
}
return min
}
// Max() finds the maximum value on any axis
func (p *Point) Max() float64 {
max := p.V[0]
for i := 1; i < len(p.V); i++ {
if p.V[i] > max { max = p.V[i] }
}
return max
}
// Sum() computes the sum of values on all axes
func (p *Point) Sum() float64 {
sum := 0.0
for i := 0; i < len(p.V); i++ {
sum += p.V[i]
}
return sum
}
/*************************************************************/
// Percentile() finds given percentile on each axis
func (points Points) Percentile(percentile float64) *Point {
if len(points) == 0 { return New() }
axes := points[0].Axes()
p := NewZero(axes)
for axis := 0; axis < axes; axis++ {
vals := make([]float64, len(points))
for i := range points {
vals[i] = points[i].V[axis]
}
sort.Float64s(vals)
// use precise location
l := float64(len(vals) - 1) // last val
i, f := math.Modf(l * percentile)
if f < 0.01 || i == l {
p.V[axis] = vals[int(i)]
} else {
p.V[axis] = (1.0-f)*vals[int(i)] + f*vals[int(i)+1]
}
}
return p
}
// Median() finds median using Percentile(0.5)
func (points Points) Median() *Point { return points.Percentile(0.5); }
// Mean() computes the arithmetic mean along each axis in points
func (points Points) Mean() *Point {
if len(points) == 0 { return New() }
axes := points[0].Axes()
p := NewZero(axes)
for i := range points {
for axis := 0; axis < axes; axis++ {
p.V[axis] += points[i].V[axis]
}
}
fact := 1.0 / float64(len(points))
for axis := 0; axis < axes; axis++ {
p.V[axis] *= fact
}
return p
}
// Errors() computes the sums of squared error vs. given reference; for example, it can be
// used to compute the within-cluster sum of squares, a common metric for kmeans clustering
func (points Points) Errors(ref *Point) *Point {
if len(points) == 0 { return New() }
axes := points[0].Axes()
p := NewZero(axes)
for i := range points {
for axis := 0; axis < axes; axis++ {
p.V[axis] += math.Pow(points[i].V[axis] - ref.V[axis], 2)
}
}
return p
}
// Variance() computes the variance on each axis vs. given mean
func (points Points) Variance(mean *Point) *Point {
p := points.Errors(mean)
p.Mul(1.0 / float64(len(points)))
return p
}
// Stddev() computes the standard deviation on each axis vs. given mean
func (points Points) Stddev(mean *Point) *Point {
p := points.Errors(mean)
fact := 1.0 / float64(len(points))
for axis := 0; axis < len(mean.V); axis++ {
p.V[axis] = math.Sqrt(p.V[axis] * fact)
}
return p
}
// Min() finds the minimum value on each axis; it is not "the minimal point" of all points
func (points Points) Min() *Point {
if len(points) == 0 { return New() }
// use first point as start
p := points[0].Copy()
p.D = nil
for i := 1; i < len(points); i++ {
for axis := 0; axis < len(p.V); axis++ {
if points[i].V[axis] < p.V[axis] {
p.V[axis] = points[i].V[axis]
}
}
}
return p
}
// Max() finds the maximum value on each axis; it is not "the maximum point" of all points
func (points Points) Max() *Point {
if len(points) == 0 { return New() }
// use first point as start
p := points[0].Copy()
p.D = nil
for i := 1; i < len(points); i++ {
for axis := 0; axis < len(p.V); axis++ {
if points[i].V[axis] > p.V[axis] {
p.V[axis] = points[i].V[axis]
}
}
}
return p
}