-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcosine.go
74 lines (57 loc) · 1.54 KB
/
cosine.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
//
// Copyright (C) 2024 Dmitry Kolesnikov
//
// This file may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
// https://github.com/kshard/vector
//
package noasm
import "github.com/chewxy/math32"
const ENABLED_COSINE = true
// Cosine distance between two vectors
func CosineF32(a, b []float32) (d float32) {
if len(a) != len(b) {
panic("vectors must have equal lengths")
}
if len(a)%4 != 0 {
panic("vector length must be multiple of 4")
}
ab := float32(0.0)
aa := float32(0.0)
bb := float32(0.0)
for i := 0; i < len(a); i += 4 {
asl := a[i : i+4 : i+4]
bsl := b[i : i+4 : i+4]
ax0, ax1, ax2, ax3 := asl[0], asl[1], asl[2], asl[3]
bx0, bx1, bx2, bx3 := bsl[0], bsl[1], bsl[2], bsl[3]
ab0 := ax0 * bx0
ab1 := ax1 * bx1
ab2 := ax2 * bx2
ab3 := ax3 * bx3
ab += ab0 + ab1 + ab2 + ab3
aa0 := ax0 * ax0
aa1 := ax1 * ax1
aa2 := ax2 * ax2
aa3 := ax3 * ax3
aa += aa0 + aa1 + aa2 + aa3
bb0 := bx0 * bx0
bb1 := bx1 * bx1
bb2 := bx2 * bx2
bb3 := bx3 * bx3
bb += bb0 + bb1 + bb2 + bb3
}
s := math32.Sqrt(aa) * math32.Sqrt(bb)
// Note: two proportional vectors have a cosine similarity of 1 |d|=0
// two orthogonal vectors have a similarity of 0 |d|=0.5
// and two opposite vectors have a similarity of -1. |d|=1.0
d = (1 - ab/s) / 2
return
}
// Type Class for Cosine distance
type Cosine int
func (Cosine) Distance(a, b []float32) float32 {
return CosineF32(a, b)
}
func (Cosine) Equal(a, b []float32) bool {
return EqualF32(a, b)
}