VectorOps

Struct VectorOps 

Source
pub struct VectorOps;
Expand description

Vector operations

Implementations§

Source§

impl VectorOps

Source

pub fn add(a: &[f32], b: &[f32]) -> Result<Vec<f32>>

Add two vectors element-wise

Source

pub fn subtract(a: &[f32], b: &[f32]) -> Result<Vec<f32>>

Subtract two vectors element-wise

Source

pub fn scale(v: &[f32], scalar: f32) -> Vec<f32>

Multiply vector by scalar

Source

pub fn centroid(vectors: &[Vec<f32>]) -> Result<Vec<f32>>

Calculate mean/centroid of multiple vectors

Source

pub fn normalize(v: &[f32]) -> Vec<f32>

Normalize vector to unit length (L2 norm = 1)

Source

pub fn norm(v: &[f32]) -> f32

L2 norm (Euclidean length)

Source

pub fn dot(a: &[f32], b: &[f32]) -> Result<f32>

Dot product

Source

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 a
  • a_values - Values at those indices in vector a
  • b_indices - Indices of non-zero elements in vector b
  • b_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);
Source

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) = 5
Source

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 vector
  • b_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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more