/
util.go
198 lines (182 loc) · 3.86 KB
/
util.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package data
import (
"math"
"time"
"github.com/chewxy/math32"
)
func getCurrentTime() uint64 {
return uint64(time.Now().Unix())
}
// CalculateAverage calculates average of two arrays divided by n
func CalculateAverage(avg []float32, p []float32, n float32) []float32 {
if n == 0 {
return p
}
if len(avg) < len(p) {
avg = make([]float32, len(p))
}
for i := 0; i < len(p); i++ {
avg[i] += p[i] / n
}
return avg
}
func QuickVectorDistance(arr1 []float32, arr2 []float32) float64 {
minLen := min(len(arr1), len(arr2))
var ret float64
for i := 0; i < minLen; i++ {
tmp := arr1[i] - arr2[i]
ret += float64(math32.Abs(tmp))
}
return ret
}
// VectorDistance calculates distance of two vector by euclidean distance
func VectorDistance(arr1 []float32, arr2 []float32) float64 {
minLen := min(len(arr1), len(arr2))
d := euclideanDistance(arr1[:minLen], arr2[:minLen])
return d
}
// VectorMultiplication calculates elementwise of multiplication of two vectors
func VectorMultiplication(arr1 []float32, arr2 []float32) float64 {
minLen := min(len(arr1), len(arr2))
var ret float32
for i := 0; i < minLen; i++ {
ret += arr1[i] * arr2[i]
}
return float64(ret)
}
// AngularDistance sim(u.v) = (1 - arccos(cosine_similarity(u, v)) / pi)
func AngularDistance(a []float32, b []float32) float64 {
return 1.0 - (math.Acos(CosineSimilarity(a, b)) / math.Pi)
}
// CosineSimilarity for vector similarity
func CosineSimilarity(a []float32, b []float32) float64 {
count := 0
lengthA := len(a)
lengthB := len(b)
if lengthA > lengthB {
count = lengthA
} else {
count = lengthB
}
sumA := float32(0.0)
s1 := float32(0.0)
s2 := float32(0.0)
for k := 0; k < count; k++ {
if k >= lengthA {
s2 += b[k] * b[k] // math32.Pow(b[k], 2)
continue
}
if k >= lengthB {
s1 += a[k] * a[k] // math32.Pow(a[k], 2)
continue
}
sumA += a[k] * b[k]
s1 += a[k] * a[k] // math32.Pow(a[k], 2)
s2 += b[k] * b[k] //math32.Pow(b[k], 2)
}
if s1 == 0 || s2 == 0 {
return 0.0
}
similarity := float64(sumA / (math32.Sqrt(s1) * math32.Sqrt(s2)))
if similarity > 1 {
similarity = 1.0
} else if similarity < -1.0 {
similarity = -1.0
}
return similarity
}
// CosineSimilarity64 for vector similarity
func CosineSimilarity64(a []float64, b []float64) float64 {
count := 0
lengthA := len(a)
lengthB := len(b)
if lengthA > lengthB {
count = lengthA
} else {
count = lengthB
}
sumA := 0.0
s1 := 0.0
s2 := 0.0
for k := 0; k < count; k++ {
if k >= lengthA {
s2 += math.Pow(b[k], 2)
continue
}
if k >= lengthB {
s1 += math.Pow(a[k], 2)
continue
}
sumA += a[k] * b[k]
s1 += math.Pow(a[k], 2)
s2 += math.Pow(b[k], 2)
}
if s1 == 0 || s2 == 0 {
return 0.0
}
return sumA / (math.Sqrt(s1) * math.Sqrt(s2))
}
// CosineSimilarity for vector similarity
func CosineSimilarity32(a []float32, b []float32) float32 {
count := 0
lengthA := len(a)
lengthB := len(b)
if lengthA > lengthB {
count = lengthA
} else {
count = lengthB
}
sumA := float32(0.0)
s1 := float32(0.0)
s2 := float32(0.0)
for k := 0; k < count; k++ {
if k >= lengthA {
s2 += b[k] * b[k]
continue
}
if k >= lengthB {
s1 += a[k] * a[k]
continue
}
sumA += a[k] * b[k]
s1 += a[k] * a[k]
s2 += b[k] * b[k]
}
if s1 == 0 || s2 == 0 {
return 0.0
}
return sumA / float32(math.Sqrt(float64(s1))*math.Sqrt(float64(s2)))
}
func euclideanDistance(arr1 []float32, arr2 []float32) float64 {
var ret float32
for i := 0; i < len(arr1); i++ {
tmp := arr1[i] - arr2[i]
ret += math32.Pow(tmp, 2)
}
return math.Sqrt(float64(ret)) // Sqrt is totally unnecessary for comparisons
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
func minUint64(a, b uint64) uint64 {
if a < b {
return a
}
return b
}
func sum(arr []float64) float64 {
sum := 0.0
for _, e := range arr {
sum += e
}
return sum
}