nodedb_vector/distance/simd/
scalar.rs1pub fn scalar_l2(a: &[f32], b: &[f32]) -> f32 {
6 assert_eq!(a.len(), b.len(), "scalar_l2: length mismatch");
7 let mut sum = 0.0f32;
8 for i in 0..a.len() {
9 let d = a[i] - b[i];
10 sum += d * d;
11 }
12 sum
13}
14
15pub fn scalar_cosine(a: &[f32], b: &[f32]) -> f32 {
16 assert_eq!(a.len(), b.len(), "scalar_cosine: length mismatch");
17 let mut dot = 0.0f32;
18 let mut na = 0.0f32;
19 let mut nb = 0.0f32;
20 for i in 0..a.len() {
21 dot += a[i] * b[i];
22 na += a[i] * a[i];
23 nb += b[i] * b[i];
24 }
25 let denom = (na * nb).sqrt();
26 if denom < f32::EPSILON {
27 1.0
28 } else {
29 (1.0 - dot / denom).max(0.0)
30 }
31}
32
33pub fn scalar_ip(a: &[f32], b: &[f32]) -> f32 {
34 assert_eq!(a.len(), b.len(), "scalar_ip: length mismatch");
35 let mut dot = 0.0f32;
36 for i in 0..a.len() {
37 dot += a[i] * b[i];
38 }
39 -dot
40}