-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheuclidean_test.go
69 lines (58 loc) · 1.54 KB
/
euclidean_test.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
package disfun
import (
"github.com/gonum/matrix/mat64"
"math"
"testing"
)
var m = mat64.NewDense(3, 1, []float64{1, 2, 3})
var n = mat64.NewDense(3, 1, []float64{2, 4, 6})
func BenchmarkEuclideanDistance(b *testing.B) {
euclidean := NewEuclidean()
for i := 0; i < b.N; i++ {
euclidean.Distance(n, m)
}
}
func TestEuclideanDistanceStruct(t *testing.T) {
euclidean := NewEuclidean()
var expected float64
x := mat64.NewDense(3, 1, []float64{1, 2, 3})
y := mat64.NewDense(3, 1, []float64{2, 4, 6})
innerResult := euclidean.InnerProduct(x, y)
expected = 28.0
if innerResult != expected {
t.Log("Expected: ", expected, "but got ", innerResult)
t.Fail()
}
distance := euclidean.Distance(x, y)
expected = math.Sqrt(14)
if distance != expected {
t.Log("Expected: ", expected, "but got ", distance)
t.Fail()
}
}
func TestEuclideanDistanceStructZero(t *testing.T) {
euclidean := NewEuclidean()
var expected float64
x := floatsToMatrix([]float64{12, 45, 78, 23, 45, 97})
innerResult := euclidean.InnerProduct(x, x)
expected = 20216.0
if innerResult != expected {
t.Log("Expected: ", expected, "but got ", innerResult)
t.Fail()
}
distance := euclidean.Distance(x, x)
expected = 0.0
if distance != expected {
t.Log("Expected: ", expected, "but got ", distance)
t.Fail()
}
}
/*
func TestEuclideanDistanceZero(t *testing.T) {
x := FloatsToMatrix([]float64{12, 45, 78, 23, 45, 97})
y := FloatsToMatrix([]float64{12, 45, 78, 23, 45, 97})
distance := EuclideanDistance(x, y)
expected := 0.0
DistanceTestCheck(t, distance, expected)
}
*/