lazymatrix::RowNormalizedMatrix
LazyMatrix for Rust · 0.9.0
pub struct RowNormalizedMatrix<M, F = f64>A matrix presented with lazy row normalization A = D⁻¹(X − r1ᵀ).
Each logical entry is (X[i, j] - r[i]) / d[i], with independently optional centering and scaling. The underlying matrix is never modified or copied. Parameters occupy O(nrows) storage. Fitting uses at most one working row for exact quantiles; vector products use O(1) additional workspace beyond their output and normalize entries before accumulation.
Fitting and products require MatrixRead, so all supported dense faer, nalgebra, and ndarray matrices and their borrowed, potentially strided views work. Products support owned vectors and reusable output views. Sparse and storage-backed inputs, combined row and column normalization, fused and batched products, borrowed logical rows and columns, and eager conversion are not yet supported.
Fitted parameters describe particular rows. New samples need independently fitted row parameters, rather than the row parameters of training samples. crate::Transpose can transpose the fitted operator without refitting.
use lazymatrix::{Centering, MatVec, RowNormalization, RowNormalizedMatrix, Scaling};
use ndarray::array;
let x = array![[3.0, 4.0], [0.0, 0.0]];
let spec = RowNormalization::new(Centering::None, Scaling::L2);
let normalized = RowNormalizedMatrix::new(x.view(), spec).unwrap();
let product = normalized.matvec(&array![1.0, 1.0]).unwrap();
assert_eq!(product, array![1.4, 0.0]);
assert_eq!(normalized.scales(), Some([5.0, 1.0].as_slice()));Computed normalization requires dense indexed access:
use lazymatrix::{MatrixErrorType, MatrixShape, RowNormalization, RowNormalizedMatrix};
fn without_dense_access<M: MatrixShape + MatrixErrorType>(matrix: M) {
let _ = RowNormalizedMatrix::new(matrix, RowNormalization::standardize());
}Inherent implementation
impl<M: MatrixRead<F> + MatrixErrorType, F: Scalar> RowNormalizedMatrix<M, F>new
pub fn new(
data: M,
spec: RowNormalization
) -> Result<Self, <M as MatrixErrorType>::Error>Fit row centers and scales independently according to spec.
Norms use centered rows when centering is active. Standard deviations use a two-pass calculation with ncols - df as the variance divisor. Exact quantiles use one reusable working row and linear interpolation. Fitting takes O(nrows * ncols) time, or O(nrows * ncols * log(ncols)) when sorting for quantiles, with O(nrows + ncols) storage at most.
Exact zero scales become one, leaving zero rows unchanged under norm scaling. Nonfinite statistics retain their IEEE values. Zero-column rows have undefined means, standard deviations, minima, and ranges (NaN), and zero norms, matching column normalization along the other axis.
The return type shares the backend error type for consistency with crate::LazyMatrix::new. Current dense fitting is infallible.
Inherent implementation
impl<M: MatrixShape, F: Scalar> RowNormalizedMatrix<M, F>centers
pub fn centers(&self) -> Option<&[F]>Fitted row centers, if centering is active.
data
pub fn data(&self) -> &MBorrow the underlying unnormalized matrix.
from_normalization
pub fn from_normalization(
data: M,
normalization: RowNormalizationParams<F>
) -> SelfWrap data using parameters fitted for the same rows, without refitting.
Only the row count is checked. The caller determines whether reusing these row-specific parameters is meaningful for the supplied data.
Panics
Panics if the matrix row count differs from the fitted row count.
from_parts
pub fn from_parts(
data: M,
centers: Option<Vec<F>>,
scales: Option<Vec<F>>
) -> SelfConstruct from explicit row centers and/or scales.
Negative scales and nonfinite values are preserved. Unlike Self::new, this constructor rejects exact zero scales rather than replacing them.
Panics
Panics if either vector does not have length nrows, or if a scale is positive or negative zero. A zero-scale panic reports the row index.
into_parts
pub fn into_parts(self) -> (M, Option<Vec<F>>, Option<Vec<F>>)Recover the underlying matrix and parameter vectors without copying.
ncols
pub fn ncols(&self) -> usizeNumber of columns of the logical normalized matrix.
normalization
pub fn normalization(&self) -> &RowNormalizationParams<F>Borrow the fitted parameters without copying.
nrows
pub fn nrows(&self) -> usizeNumber of rows of the logical normalized matrix.
scales
pub fn scales(&self) -> Option<&[F]>Fitted row scales, if scaling is active.
with_centers
pub fn with_centers(data: M, centers: Vec<F>) -> SelfWrap a matrix with explicit row centering only.
Panics
Panics if centers.len() != nrows.
with_scales
pub fn with_scales(data: M, scales: Vec<F>) -> SelfWrap a matrix with explicit row scaling only.
Panics
Panics if scales.len() != nrows, or if a scale is positive or negative zero.
Implementation of Clone
impl<M: core::clone::Clone, F: core::clone::Clone> Clone for RowNormalizedMatrix<M, F>clone
fn clone(&self) -> SelfImplementation of Debug
impl<M: core::fmt::Debug, F: core::fmt::Debug> Debug for RowNormalizedMatrix<M, F>fmt
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::ResultImplementation of MatTransposeVec
impl<M, V, F> MatTransposeVec<V> for RowNormalizedMatrix<M, F> where M: MatrixRead<F> + MatrixErrorType, F: Scalar, V: VectorOwned<F, Owned = V> + VectorViewMut<F>mat_transpose_vec
fn mat_transpose_vec(
&self,
x: &V
) -> Result<V, <Self as MatrixErrorType>::Error>Implementation of MatTransposeVecInto
impl<M, X, Y, F> MatTransposeVecInto<X, Y> for RowNormalizedMatrix<M, F> where M: MatrixRead<F> + MatrixErrorType, F: Scalar, X: VectorView<F>, Y: VectorViewMut<F>mat_transpose_vec_into
fn mat_transpose_vec_into(
&self,
x: &X,
out: &mut Y
) -> Result<(), <Self as MatrixErrorType>::Error>Implementation of MatVec
impl<M, V, F> MatVec<V> for RowNormalizedMatrix<M, F> where M: MatrixRead<F> + MatrixErrorType, F: Scalar, V: VectorOwned<F, Owned = V> + VectorViewMut<F>matvec
fn matvec(&self, x: &V) -> Result<V, <Self as MatrixErrorType>::Error>Implementation of MatVecInto
impl<M, X, Y, F> MatVecInto<X, Y> for RowNormalizedMatrix<M, F> where M: MatrixRead<F> + MatrixErrorType, F: Scalar, X: VectorView<F>, Y: VectorViewMut<F>matvec_into
fn matvec_into(
&self,
x: &X,
out: &mut Y
) -> Result<(), <Self as MatrixErrorType>::Error>Implementation of MatrixErrorType
impl<M: MatrixErrorType, F> MatrixErrorType for RowNormalizedMatrix<M, F>Error
type Error = <M as MatrixErrorType>::Error;Implementation of MatrixShape
impl<M: MatrixShape, F> MatrixShape for RowNormalizedMatrix<M, F>ncols
fn ncols(&self) -> usizenrows
fn nrows(&self) -> usizeImplementation of Freeze
impl<M, F> Freeze for RowNormalizedMatrix<M, F> where M: Freeze, RowNormalizationParams<F>: FreezeImplementation of RefUnwindSafe
impl<M, F> RefUnwindSafe for RowNormalizedMatrix<M, F> where M: RefUnwindSafe, RowNormalizationParams<F>: RefUnwindSafeImplementation of Send
impl<M, F> Send for RowNormalizedMatrix<M, F> where M: Send, RowNormalizationParams<F>: SendImplementation of Sync
impl<M, F> Sync for RowNormalizedMatrix<M, F> where M: Sync, RowNormalizationParams<F>: SyncImplementation of Unpin
impl<M, F> Unpin for RowNormalizedMatrix<M, F> where M: Unpin, RowNormalizationParams<F>: UnpinImplementation of UnsafeUnpin
impl<M, F> UnsafeUnpin for RowNormalizedMatrix<M, F> where M: UnsafeUnpin, RowNormalizationParams<F>: UnsafeUnpinImplementation of UnwindSafe
impl<M, F> UnwindSafe for RowNormalizedMatrix<M, F> where M: UnwindSafe, RowNormalizationParams<F>: UnwindSafeImplementation of Any
impl<T> Any for T where T: 'static + ?SizedImplementation of Borrow
impl<T> Borrow<T> for T where T: ?SizedImplementation of BorrowMut
impl<T> BorrowMut<T> for T where T: ?SizedImplementation of ByRef
impl<T> ByRef<T> for TImplementation of CastableFrom
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT where ST: ?Sized, DT: ?SizedImplementation of CastableFrom
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT where ST: ?Sized, DT: ?SizedImplementation of CloneToUninit
impl<T> CloneToUninit for T where T: CloneImplementation of From
impl<T> From<T> for TImplementation of Imply
impl<T, U> Imply<T> for U where T: ?Sized, U: ?SizedImplementation of Into
impl<T, U> Into<U> for T where U: From<T>Implementation of IntoEither
impl<T> IntoEither for TImplementation of Pointable
impl<T> Pointable for TImplementation of Read
impl<T> Read<Exclusive, BecauseExclusive> for T where T: ?SizedImplementation of Same
impl<T> Same for TImplementation of SupersetOf
impl<SS, SP> SupersetOf<SS> for SP where SS: SubsetOf<SP>Implementation of ToOwned
impl<T> ToOwned for T where T: CloneImplementation of TryFrom
impl<T, U> TryFrom<U> for T where U: Into<T>Implementation of TryInto
impl<T, U> TryInto<U> for T where U: TryFrom<T>
