Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion vortex-tensor/src/scalar_fns/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
//! Row-level arithmetic shared by tensor scalar functions.
//!
//! These kernels preserve the scalar functions' left-to-right floating-point arithmetic. Keeping
//! the arithmetic here ensures that fused and single-result operations use the same result contract.
//! the arithmetic here ensures that fused and single-result operations use the same result
//! contract.

use num_traits::Float;
use vortex_array::dtype::NativePType;
Expand All @@ -21,3 +22,14 @@ pub(crate) fn l2_norm_row<T: Float + NativePType>(row: &[T]) -> T {

sum_squared.sqrt()
}

/// Computes `sum(lhs_i * rhs_i)` for one pair of rows.
///
/// Both rows must have the same length. Otherwise, trailing values are ignored and the result is
/// incorrect. Callers preserve the left-to-right multiplication and accumulation order.
pub(crate) fn inner_product_row<T: Float + NativePType>(lhs: &[T], rhs: &[T]) -> T {
lhs.iter()
.zip(rhs)
.map(|(&lhs_value, &rhs_value)| lhs_value * rhs_value)
.fold(T::zero(), |sum, product| sum + product)
}
Loading
Loading