Skip to main content

Crate cosine_fast

Crate cosine_fast 

Source
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 q and 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_norm should equal norm(b).
norm
Compute the L2 norm of v. Useful as precompute_norm for hot-path queries.