-
Notifications
You must be signed in to change notification settings - Fork 22
/
vector.go
62 lines (50 loc) · 1.3 KB
/
vector.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
package chromem
import (
"errors"
"math"
)
const isNormalizedPrecisionTolerance = 1e-6
// dotProduct calculates the dot product between two vectors.
// It's the same as cosine similarity for normalized vectors.
// The resulting value represents the similarity, so a higher value means the
// vectors are more similar.
func dotProduct(a, b []float32) (float32, error) {
// The vectors must have the same length
if len(a) != len(b) {
return 0, errors.New("vectors must have the same length")
}
var dotProduct float32
for i := range a {
dotProduct += a[i] * b[i]
}
return dotProduct, nil
}
func normalizeVector(v []float32) []float32 {
var norm float32
for _, val := range v {
norm += val * val
}
norm = float32(math.Sqrt(float64(norm)))
res := make([]float32, len(v))
for i, val := range v {
res[i] = val / norm
}
return res
}
// subtractVector subtracts vector b from vector a in place.
func subtractVector(a, b []float32) []float32 {
res := make([]float32, len(a))
for i := range a {
res[i] = a[i] - b[i]
}
return res
}
// isNormalized checks if the vector is normalized.
func isNormalized(v []float32) bool {
var sqSum float64
for _, val := range v {
sqSum += float64(val) * float64(val)
}
magnitude := math.Sqrt(sqSum)
return math.Abs(magnitude-1) < isNormalizedPrecisionTolerance
}