pub fn csvec_dot_by_binary_search<N, I>(
    vec1: CsVecViewI<N, I>,
    vec2: CsVecViewI<N, I>,
) -> N
where
    I: SpIndex,
    N: Num + Copy,
{
    let (mut idx1, mut val1, mut idx2, mut val2) = if vec1.nnz() < vec2.nnz() {
        (vec1.indices(), vec1.data(), vec2.indices(), vec2.data())
    } else {
        (vec2.indices(), vec2.data(), vec1.indices(), vec1.data())
    };

    let mut sum = N::zero();
    while !idx1.is_empty() && !idx2.is_empty() {
        debug_assert_eq!(idx1.len(), val1.len());
        debug_assert_eq!(idx2.len(), val2.len());

        let (found, i) = match idx2.binary_search(&idx1[0]) {
            Ok(i) => (true, i),
            Err(i) => (false, i),
        };
        if found {
            sum = sum + val1[0] * val2[i];
        }
        idx1 = &idx1[1..];
        val1 = &val1[1..];
        idx2 = &idx2[i..];
        val2 = &val2[i..];
    }
    sum
}
/// Compute the dot product of two sparse vectors, using binary search to find matching indices.
///
/// Runs in O(MlogN) time, where M and N are the number of non-zero entries in each vector.
Compute the dot product of two sparse vectors, using binary search to find matching indices.
Runs in O(MlogN) time, where M and N are the number of non-zero entries in each vector.
Compute the dot product of two sparse vectors using binary search to find matching indices . Runs in O ( MlogN ) time where M and N are the number of non - zero entries in each vector .
pub fn csvec_dot_by_binary_search<N, I>(
    vec1: CsVecViewI<N, I>,
    vec2: CsVecViewI<N, I>,
) -> N
where
    I: SpIndex,
    N: Num + Copy,
{
    let (mut idx1, mut val1, mut idx2, mut val2) = if vec1.nnz() < vec2.nnz() {
        (vec1.indices(), vec1.data(), vec2.indices(), vec2.data())
    } else {
        (vec2.indices(), vec2.data(), vec1.indices(), vec1.data())
    };
    let mut sum = N::zero();
    while !idx1.is_empty() && !idx2.is_empty() {
        debug_assert_eq!(idx1.len(), val1.len());
        debug_assert_eq!(idx2.len(), val2.len());
        let (found, i) = match idx2.binary_search(&idx1[0]) {
            Ok(i) => (true, i),
            Err(i) => (false, i),
        };
        if found {
            sum = sum + val1[0] * val2[i];
        }
        idx1 = &idx1[1..];
        val1 = &val1[1..];
        idx2 = &idx2[i..];
        val2 = &val2[i..];
    }
    sum
}
pub fn csvec_dot_by_binary_search < N , I > ( vec1 : CsVecViewI < N , I > , vec2 : CsVecViewI < N , I > , ) -> N where I : SpIndex , N : Num + Copy , { let ( mut idx1 , mut val1 , mut idx2 , mut val2 ) = if vec1 . nnz ( ) < vec2 . nnz ( ) { ( vec1 . indices ( ) , vec1 . data ( ) , vec2 . indices ( ) , vec2 . data ( ) ) } else { ( vec2 . indices ( ) , vec2 . data ( ) , vec1 . indices ( ) , vec1 . data ( ) ) } ; let mut sum = N :: zero ( ) ; while ! idx1 . is_empty ( ) && ! idx2 . is_empty ( ) { debug_assert_eq ! ( idx1 len ( ) val1 len ( ) ) ; debug_assert_eq ! ( idx2 len ( ) val2 len ( ) ) ; let ( found , i ) = match idx2 . binary_search ( & idx1 [ 0 ] ) { Ok ( i ) => ( true , i ) , Err ( i ) => ( false , i ) , } ; if found { sum = sum + val1 [ 0 ] * val2 [ i ] ; } idx1 = & idx1 [ 1 .. ] ; val1 = & val1 [ 1 .. ] ; idx2 = & idx2 [ i .. ] ; val2 = & val2 [ i .. ] ; } sum }
Compute the dot product of two sparse vectors, using binary search to find matching indices.
Compute the dot product of two sparse vectors using binary search to find matching indices .
vec1
CsVecViewI<N, I>
vec2
CsVecViewI<N, I>
vec1
CsVecViewI<N, I>
vec2
CsVecViewI<N, I>