pub struct VectorOps;Expand description
Vector operations
Implementations§
Source§impl VectorOps
impl VectorOps
Sourcepub fn centroid(vectors: &[Vec<f32>]) -> Result<Vec<f32>>
pub fn centroid(vectors: &[Vec<f32>]) -> Result<Vec<f32>>
Calculate mean/centroid of multiple vectors
Sourcepub fn sparse_dot(
a_indices: &[usize],
a_values: &[f32],
b_indices: &[usize],
b_values: &[f32],
) -> f32
pub fn sparse_dot( a_indices: &[usize], a_values: &[f32], b_indices: &[usize], b_values: &[f32], ) -> f32
Sparse dot product
Computes dot product between two sparse vectors represented as (indices, values). Much more efficient than materializing to dense when vectors are sparse.
§Arguments
a_indices- Indices of non-zero elements in vector aa_values- Values at those indices in vector ab_indices- Indices of non-zero elements in vector bb_values- Values at those indices in vector b
§Returns
Dot product of the two sparse vectors
§Performance
O(n + m) where n and m are the number of non-zero elements. For vectors with k non-zero elements in d dimensions: O(k) vs O(d) for dense.
§Example
use vecstore::vectors::VectorOps;
// Sparse vectors in 1000-dimensional space
// a has non-zero values at indices [5, 100, 500]
// b has non-zero values at indices [5, 200, 500]
let a_indices = vec![5, 100, 500];
let a_values = vec![1.0, 2.0, 3.0];
let b_indices = vec![5, 200, 500];
let b_values = vec![1.5, 2.5, 1.0];
let dot = VectorOps::sparse_dot(&a_indices, &a_values, &b_indices, &b_values);
// Result: 1.0*1.5 + 3.0*1.0 = 4.5 (indices 5 and 500 match)
assert_eq!(dot, 4.5);Sourcepub fn sparse_norm(values: &[f32]) -> f32
pub fn sparse_norm(values: &[f32]) -> f32
Sparse vector L2 norm (Euclidean length)
§Example
use vecstore::vectors::VectorOps;
let indices = vec![0, 5, 10];
let values = vec![3.0, 4.0, 0.0]; // [3, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, ...]
let norm = VectorOps::sparse_norm(&values);
assert_eq!(norm, 5.0); // sqrt(3^2 + 4^2) = 5Sourcepub fn sparse_cosine(
a_indices: &[usize],
a_values: &[f32],
b_indices: &[usize],
b_values: &[f32],
) -> f32
pub fn sparse_cosine( a_indices: &[usize], a_values: &[f32], b_indices: &[usize], b_values: &[f32], ) -> f32
Cosine similarity for sparse vectors
§Arguments
a_indices,a_values- First sparse vectorb_indices,b_values- Second sparse vector
§Returns
Cosine similarity in [-1, 1] range. Returns 0.0 if either vector has zero norm.
§Example
use vecstore::vectors::VectorOps;
let a_indices = vec![0, 1, 2];
let a_values = vec![1.0, 0.0, 1.0];
let b_indices = vec![0, 1];
let b_values = vec![1.0, 0.0];
let sim = VectorOps::sparse_cosine(&a_indices, &a_values, &b_indices, &b_values);
// Should be close to 1.0 / sqrt(2) ≈ 0.707
assert!((sim - 0.707).abs() < 0.01);Auto Trait Implementations§
impl Freeze for VectorOps
impl RefUnwindSafe for VectorOps
impl Send for VectorOps
impl Sync for VectorOps
impl Unpin for VectorOps
impl UnsafeUnpin for VectorOps
impl UnwindSafe for VectorOps
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more