pub struct SparseEmbedding { /* private fields */ }Expand description
A sparse vector embedding for use with pgvector’s sparsevec type.
Sparse vectors are efficient for high-dimensional data where most values are zero, common in text embeddings, bag-of-words representations, and learned sparse retrievers.
§Examples
use prax_pgvector::SparseEmbedding;
// From a dense vector (zeros are stripped)
let sparse = SparseEmbedding::from_dense(vec![1.0, 0.0, 2.0, 0.0, 3.0]);
// From indices and values
let sparse = SparseEmbedding::from_parts(&[0, 2, 4], &[1.0, 2.0, 3.0], 5).unwrap();Implementations§
Source§impl SparseEmbedding
impl SparseEmbedding
Sourcepub fn from_dense(values: Vec<f32>) -> Self
pub fn from_dense(values: Vec<f32>) -> Self
Create a sparse embedding from a dense vector.
Zero values are automatically removed.
Sourcepub fn from_parts(
indices: &[i32],
values: &[f32],
dimensions: usize,
) -> VectorResult<Self>
pub fn from_parts( indices: &[i32], values: &[f32], dimensions: usize, ) -> VectorResult<Self>
Create a sparse embedding from indices, values, and total dimensions.
§Errors
Returns an error if indices and values have different lengths, or if any index is out of bounds.
Sourcepub fn dimensions(&self) -> i32
pub fn dimensions(&self) -> i32
Get the total number of dimensions.
Sourcepub fn into_inner(self) -> SparseVector
pub fn into_inner(self) -> SparseVector
Get the inner pgvector type.
Sourcepub fn inner(&self) -> &SparseVector
pub fn inner(&self) -> &SparseVector
Get a reference to the inner pgvector type.
Trait Implementations§
Source§impl Clone for SparseEmbedding
impl Clone for SparseEmbedding
Source§fn clone(&self) -> SparseEmbedding
fn clone(&self) -> SparseEmbedding
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for SparseEmbedding
impl Debug for SparseEmbedding
Source§impl<'de> Deserialize<'de> for SparseEmbedding
impl<'de> Deserialize<'de> for SparseEmbedding
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl Display for SparseEmbedding
impl Display for SparseEmbedding
Source§impl From<SparseEmbedding> for SparseVector
impl From<SparseEmbedding> for SparseVector
Source§fn from(e: SparseEmbedding) -> Self
fn from(e: SparseEmbedding) -> Self
Converts to this type from the input type.
Source§impl From<SparseVector> for SparseEmbedding
impl From<SparseVector> for SparseEmbedding
Source§fn from(v: SparseVector) -> Self
fn from(v: SparseVector) -> Self
Converts to this type from the input type.
Source§impl PartialEq for SparseEmbedding
impl PartialEq for SparseEmbedding
Source§impl Serialize for SparseEmbedding
impl Serialize for SparseEmbedding
impl StructuralPartialEq for SparseEmbedding
Auto Trait Implementations§
impl Freeze for SparseEmbedding
impl RefUnwindSafe for SparseEmbedding
impl Send for SparseEmbedding
impl Sync for SparseEmbedding
impl Unpin for SparseEmbedding
impl UnsafeUnpin for SparseEmbedding
impl UnwindSafe for SparseEmbedding
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