-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbraycurtis.go
36 lines (32 loc) · 871 Bytes
/
braycurtis.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
package disfun
import (
"github.com/gonum/matrix"
"github.com/gonum/matrix/mat64"
"math"
)
// BrayCurtis finds the distance by the ratio of the absolute sum of all differences of points in vectors [u,v]:
//
// Example:
// dist((u, v) = dist(v, u)) =
// |v_1 - u_1| + |v_2 - u_2| + ... + |v_n - u_n| / |v_1 + u_1| + |v_2 + u_2| + ... + |v_n + u_n|
//
// References:
// http://mathworld.wolfram.com/TaxicabMetric.html
// http://demonstrations.wolfram.com/TaxicabGeometry/
func BrayCurtis(x, y *mat64.Dense) (result float64) {
var dividend float64
var divisor float64
r1, c1 := x.Dims()
r2, c2 := y.Dims()
if r1 != r2 || c1 != c2 {
panic(matrix.ErrShape)
}
for i := 0; i < r1; i++ {
for j := 0; j < r2; j++ {
dividend += math.Abs(x.At(i, j) - y.At(i, j))
divisor += math.Abs(x.At(i, j) + y.At(i, j))
}
}
result = (dividend / divisor)
return
}