Expand description
§cosine-fast
Hot-loop cosine similarity for f32 slices. Scalar core that the
compiler auto-vectorizes well on AArch64 NEON and x86 AVX2; an
optional precompute_norm lets you skip the per-call sqrt when the
same query is compared against many candidates.
§Example
use cosine_fast::{cosine, batch_cosine};
let a = vec![1.0f32, 0.0, 0.0];
let b = vec![0.0f32, 1.0, 0.0];
assert!((cosine(&a, &b) - 0.0).abs() < 1e-6);
let q = vec![1.0f32, 2.0, 3.0];
let cands = vec![
vec![1.0, 2.0, 3.0], // self
vec![0.0, 0.0, 1.0],
];
let out = batch_cosine(&q, cands.iter().map(|v| v.as_slice()));
assert!((out[0] - 1.0).abs() < 1e-6);Functions§
- batch_
cosine - Compute cosine similarity between
qand every candidate. - cosine
- Cosine similarity between two equal-length f32 slices.
- cosine_
with_ norm - Cosine similarity when you already know one side’s norm.
b_normshould equalnorm(b). - norm
- Compute the L2 norm of
v. Useful asprecompute_normfor hot-path queries.