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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright (c) 2025 SynaDB Contributors
// Licensed under the SynaDB License. See LICENSE file for details.
//! Distance metrics for vector similarity search.
//!
//! This module provides distance functions for comparing vectors in similarity search.
//! All metrics are designed so that lower values indicate more similar vectors.
//!
//! # Supported Metrics
//!
//! | Metric | Range | Best For |
//! |--------|-------|----------|
//! | Cosine | [0, 2] | Text embeddings, normalized vectors |
//! | Euclidean | [0, ∞) | Image features, spatial data |
//! | DotProduct | (-∞, ∞) | Pre-normalized vectors, recommendation |
//!
//! # Performance
//!
//! All distance functions are O(n) where n is the vector dimension.
//! For 768-dimensional vectors (BERT), expect ~1μs per distance computation.
//!
//! # Examples
//!
//! ```rust
//! use synadb::distance::DistanceMetric;
//!
//! let a = vec![1.0f32, 0.0, 0.0];
//! let b = vec![0.0f32, 1.0, 0.0];
//!
//! // Cosine distance: orthogonal vectors have distance 1.0
//! let cosine_dist = DistanceMetric::Cosine.distance(&a, &b);
//! assert!((cosine_dist - 1.0).abs() < 1e-6);
//!
//! // Euclidean distance: sqrt(2) for unit orthogonal vectors
//! let euclidean_dist = DistanceMetric::Euclidean.distance(&a, &b);
//! assert!((euclidean_dist - std::f32::consts::SQRT_2).abs() < 1e-6);
//! ```
//!
//! _Requirements: 1.4_
/// Distance metric for similarity search.
///
/// Determines how vector similarity is computed. All metrics return
/// lower values for more similar vectors.
///
/// # Choosing a Metric
///
/// - **Cosine**: Best for text embeddings where magnitude doesn't matter.
/// Measures the angle between vectors. Range: [0, 2].
/// - **Euclidean**: Best for spatial data where absolute positions matter.
/// Measures straight-line distance. Range: [0, ∞).
/// - **DotProduct**: Best for pre-normalized vectors or when magnitude
/// should influence similarity. Range: (-∞, ∞), negated so lower = more similar.
///
/// # Examples
///
/// ```rust
/// use synadb::distance::DistanceMetric;
///
/// // Identical vectors have distance 0 for all metrics
/// let v = vec![1.0f32, 2.0, 3.0];
/// assert!(DistanceMetric::Cosine.distance(&v, &v) < 1e-6);
/// assert!(DistanceMetric::Euclidean.distance(&v, &v) < 1e-6);
/// ```
/// Cosine distance: `1 - (a·b)/(|a||b|)`.
///
/// Measures the angle between two vectors, ignoring magnitude.
/// Returns 0 for identical directions, 1 for orthogonal, 2 for opposite.
///
/// # Special Cases
///
/// - Zero vectors return distance 1.0 (treated as orthogonal)
/// - Identical vectors return ~0.0 (floating point precision)
/// Euclidean (L2) distance: `sqrt(Σ(a_i - b_i)²)`.
///
/// Measures the straight-line distance between two points in n-dimensional space.
/// Sensitive to both direction and magnitude of vectors.
/// Negative dot product: `-Σ(a_i * b_i)`.
///
/// Returns the negated dot product so that lower values indicate higher similarity.
/// For normalized vectors, this is equivalent to negative cosine similarity.