-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheuclidean.go
51 lines (40 loc) · 980 Bytes
/
euclidean.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
//
// 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
const ENABLED_EUCLIDEAN = true
// Squared Euclidean distance between two vectors
func EuclideanF32(a, b []float32) (d float32) {
if len(a) != len(b) {
panic("vectors must have equal lengths")
}
if len(a)%4 != 0 {
panic("vectors length must be multiple of 4")
}
for i := 0; i < len(a); i += 4 {
av := a[i : i+4 : i+4]
bv := b[i : i+4 : i+4]
x1 := av[0] - bv[0]
x2 := av[1] - bv[1]
x3 := av[2] - bv[2]
x4 := av[3] - bv[3]
d1 := x1 * x1
d2 := x2 * x2
d3 := x3 * x3
d4 := x4 * x4
d += d1 + d2 + d3 + d4
}
return
}
// Type Class for Euclidean distance
type Euclidean int
func (Euclidean) Distance(a, b []float32) float32 {
return EuclideanF32(a, b)
}
func (Euclidean) Equal(a, b []float32) bool {
return EqualF32(a, b)
}