-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistance.go
91 lines (75 loc) · 1.46 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
//
// 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 vector
import (
"github.com/kshard/vector/internal/noasm"
"github.com/kshard/vector/internal/pure"
"github.com/kshard/vector/internal/simd"
)
//
// Euclidean
//
const (
EUCLIDEAN_WITH_PURE = iota
EUCLIDEAN_WITH_NOASM
EUCLIDEAN_WITH_SIMD
)
// Squared Euclidean distance between two vectors
func Euclidean() interface {
Equal(F32, F32) bool
Distance(F32, F32) float32
} {
switch euclideanConfig() {
case EUCLIDEAN_WITH_PURE:
return pure.Euclidean(0)
case EUCLIDEAN_WITH_NOASM:
return noasm.Euclidean(0)
case EUCLIDEAN_WITH_SIMD:
return simd.Euclidean(0)
}
return nil
}
func euclideanConfig() int {
if simd.ENABLED_EUCLIDEAN {
return EUCLIDEAN_WITH_SIMD
}
if noasm.ENABLED_EUCLIDEAN {
return EUCLIDEAN_WITH_NOASM
}
return EUCLIDEAN_WITH_PURE
}
//
// Cosine
//
const (
COSINE_WITH_PURE = iota
COSINE_WITH_NOASM
COSINE_WITH_SIMD
)
// Cosine Distance
func Cosine() interface {
Equal(F32, F32) bool
Distance(F32, F32) float32
} {
switch cosineConfig() {
case COSINE_WITH_PURE:
return pure.Cosine(0)
case COSINE_WITH_NOASM:
return noasm.Cosine(0)
}
return nil
}
func cosineConfig() int {
if simd.ENABLED_COSINE {
return COSINE_WITH_SIMD
}
if noasm.ENABLED_COSINE {
return COSINE_WITH_NOASM
}
return COSINE_WITH_PURE
}