-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdists.go
59 lines (48 loc) · 1.09 KB
/
dists.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
package gorbi
import (
"math"
)
// Calculates the dimensions of an hypercube which contains all points
func HypercubeDims(xs [][]float64) []float64 {
coordsMin := []float64{}
coordsMax := []float64{}
for _, x := range xs[0] {
coordsMin = append(coordsMin, x)
coordsMax = append(coordsMax, x)
}
for _, xi := range xs {
for j, xin := range xi {
if xin > coordsMax[j] {
//fmt.Println(xin,coordsMax[j])
coordsMax[j] = xin
}
if xin < coordsMin[j] {
coordsMin[j] = xin
}
}
}
dims := []float64{}
for i, min := range coordsMin {
dims = append(dims, coordsMax[i]-min)
}
return dims
}
func Cdist(xa, xb [][]float64) [][]float64 {
dists := [][]float64{}
for _, xi := range xa {
disti := []float64{}
for _, xb := range xb {
disti = append(disti, EuclideanDist(xi, xb))
}
dists = append(dists, disti)
}
return dists
}
// eucleanDist calculates the euclatean distance between two points in R^n space
func EuclideanDist(pa, pb []float64) float64 {
distSqrd := 0.0
for i, pai := range pa {
distSqrd += math.Pow(pai-pb[i], 2.)
}
return math.Sqrt(distSqrd)
}