-
Notifications
You must be signed in to change notification settings - Fork 6
/
distance.go
103 lines (91 loc) · 1.88 KB
/
distance.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
package functions
import (
"github.com/chewxy/math32"
"golang.org/x/exp/constraints"
"math"
)
func Dot_Go[T constraints.Float](x, y []T) T {
res := T(0)
for i := 0; i < len(x); i++ {
res += x[i] * y[i]
}
return res
}
func Norm_Go_F64(x []float64) float64 {
res := float64(0)
for i := 0; i < len(x); i++ {
res += x[i] * x[i]
}
return math.Sqrt(res)
}
func Norm_Go_F32(x []float32) float32 {
res := float32(0)
for i := 0; i < len(x); i++ {
res += x[i] * x[i]
}
return math32.Sqrt(res)
}
func Distance_Go_F64(x, y []float64) float64 {
res := float64(0)
for i := 0; i < len(x); i++ {
res += (x[i] - y[i]) * (x[i] - y[i])
}
return math.Sqrt(res)
}
func Distance_Go_F32(x, y []float32) float32 {
res := float32(0)
for i := 0; i < len(x); i++ {
res += (x[i] - y[i]) * (x[i] - y[i])
}
return math32.Sqrt(res)
}
func ManhattanNorm_Go_F64(x []float64) float64 {
res := float64(0)
for i := 0; i < len(x); i++ {
res += math.Abs(x[i])
}
return res
}
func ManhattanNorm_Go_F32(x []float32) float32 {
res := float32(0)
for i := 0; i < len(x); i++ {
res += math32.Abs(x[i])
}
return res
}
func ManhattanDistance_Go_F64(x, y []float64) float64 {
res := float64(0)
for i := 0; i < len(x); i++ {
res += math.Abs(x[i] - y[i])
}
return res
}
func ManhattanDistance_Go_F32(x, y []float32) float32 {
res := float32(0)
for i := 0; i < len(x); i++ {
res += math32.Abs(x[i] - y[i])
}
return res
}
func CosineSimilarity_Go_F64(x, y []float64) float64 {
dp := float64(0)
xdp := float64(0)
ydp := float64(0)
for i := 0; i < len(x); i++ {
dp += x[i] * y[i]
xdp += x[i] * x[i]
ydp += y[i] * y[i]
}
return dp / math.Sqrt(xdp*ydp)
}
func CosineSimilarity_Go_F32(x, y []float32) float32 {
dp := float32(0)
xdp := float32(0)
ydp := float32(0)
for i := 0; i < len(x); i++ {
dp += x[i] * y[i]
xdp += x[i] * x[i]
ydp += y[i] * y[i]
}
return dp / math32.Sqrt(xdp*ydp)
}