Skip to content

lazymatrix::RowNormalizedMatrix ​

LazyMatrix for Rust · 0.9.0

Source

rust
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.

rust
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:

rust
use lazymatrix::{MatrixErrorType, MatrixShape, RowNormalization, RowNormalizedMatrix};

fn without_dense_access<M: MatrixShape + MatrixErrorType>(matrix: M) {
    let _ = RowNormalizedMatrix::new(matrix, RowNormalization::standardize());
}

Inherent implementation ​

Source

rust
impl<M: MatrixRead<F> + MatrixErrorType, F: Scalar> RowNormalizedMatrix<M, F>

new ​

rust
pub fn new(
    data: M,
    spec: RowNormalization
) -> Result<Self, <M as MatrixErrorType>::Error>

Source

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 ​

Source

rust
impl<M: MatrixShape, F: Scalar> RowNormalizedMatrix<M, F>

centers ​

rust
pub fn centers(&self) -> Option<&[F]>

Source

Fitted row centers, if centering is active.

data ​

rust
pub fn data(&self) -> &M

Source

Borrow the underlying unnormalized matrix.

from_normalization ​

rust
pub fn from_normalization(
    data: M,
    normalization: RowNormalizationParams<F>
) -> Self

Source

Wrap 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 ​

rust
pub fn from_parts(
    data: M,
    centers: Option<Vec<F>>,
    scales: Option<Vec<F>>
) -> Self

Source

Construct 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 ​

rust
pub fn into_parts(self) -> (M, Option<Vec<F>>, Option<Vec<F>>)

Source

Recover the underlying matrix and parameter vectors without copying.

ncols ​

rust
pub fn ncols(&self) -> usize

Source

Number of columns of the logical normalized matrix.

normalization ​

rust
pub fn normalization(&self) -> &RowNormalizationParams<F>

Source

Borrow the fitted parameters without copying.

nrows ​

rust
pub fn nrows(&self) -> usize

Source

Number of rows of the logical normalized matrix.

scales ​

rust
pub fn scales(&self) -> Option<&[F]>

Source

Fitted row scales, if scaling is active.

with_centers ​

rust
pub fn with_centers(data: M, centers: Vec<F>) -> Self

Source

Wrap a matrix with explicit row centering only.

Panics ​

Panics if centers.len() != nrows.

with_scales ​

rust
pub fn with_scales(data: M, scales: Vec<F>) -> Self

Source

Wrap 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 ​

Source

rust
impl<M: core::clone::Clone, F: core::clone::Clone> Clone for RowNormalizedMatrix<M, F>

clone ​

rust
fn clone(&self) -> Self

Source

Implementation of Debug ​

Source

rust
impl<M: core::fmt::Debug, F: core::fmt::Debug> Debug for RowNormalizedMatrix<M, F>

fmt ​

rust
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result

Source

Implementation of MatTransposeVec ​

Source

rust
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 ​

rust
fn mat_transpose_vec(
    &self,
    x: &V
) -> Result<V, <Self as MatrixErrorType>::Error>

Source

Implementation of MatTransposeVecInto ​

Source

rust
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 ​

rust
fn mat_transpose_vec_into(
    &self,
    x: &X,
    out: &mut Y
) -> Result<(), <Self as MatrixErrorType>::Error>

Source

Implementation of MatVec ​

Source

rust
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 ​

rust
fn matvec(&self, x: &V) -> Result<V, <Self as MatrixErrorType>::Error>

Source

Implementation of MatVecInto ​

Source

rust
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 ​

rust
fn matvec_into(
    &self,
    x: &X,
    out: &mut Y
) -> Result<(), <Self as MatrixErrorType>::Error>

Source

Implementation of MatrixErrorType ​

Source

rust
impl<M: MatrixErrorType, F> MatrixErrorType for RowNormalizedMatrix<M, F>

Error ​

rust
type Error = <M as MatrixErrorType>::Error;

Source

Implementation of MatrixShape ​

Source

rust
impl<M: MatrixShape, F> MatrixShape for RowNormalizedMatrix<M, F>

ncols ​

rust
fn ncols(&self) -> usize

Source

nrows ​

rust
fn nrows(&self) -> usize

Source

Implementation of Freeze ​

rust
impl<M, F> Freeze for RowNormalizedMatrix<M, F> where M: Freeze, RowNormalizationParams<F>: Freeze

Implementation of RefUnwindSafe ​

rust
impl<M, F> RefUnwindSafe for RowNormalizedMatrix<M, F> where M: RefUnwindSafe, RowNormalizationParams<F>: RefUnwindSafe

Implementation of Send ​

rust
impl<M, F> Send for RowNormalizedMatrix<M, F> where M: Send, RowNormalizationParams<F>: Send

Implementation of Sync ​

rust
impl<M, F> Sync for RowNormalizedMatrix<M, F> where M: Sync, RowNormalizationParams<F>: Sync

Implementation of Unpin ​

rust
impl<M, F> Unpin for RowNormalizedMatrix<M, F> where M: Unpin, RowNormalizationParams<F>: Unpin

Implementation of UnsafeUnpin ​

rust
impl<M, F> UnsafeUnpin for RowNormalizedMatrix<M, F> where M: UnsafeUnpin, RowNormalizationParams<F>: UnsafeUnpin

Implementation of UnwindSafe ​

rust
impl<M, F> UnwindSafe for RowNormalizedMatrix<M, F> where M: UnwindSafe, RowNormalizationParams<F>: UnwindSafe

Implementation of Any ​

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

Implementation of Borrow ​

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

Implementation of BorrowMut ​

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

Implementation of ByRef ​

rust
impl<T> ByRef<T> for T

Implementation of CastableFrom ​

rust
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT where ST: ?Sized, DT: ?Sized

Implementation of CastableFrom ​

rust
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT where ST: ?Sized, DT: ?Sized

Implementation of CloneToUninit ​

rust
impl<T> CloneToUninit for T where T: Clone

Implementation of From ​

rust
impl<T> From<T> for T

Implementation of Imply ​

rust
impl<T, U> Imply<T> for U where T: ?Sized, U: ?Sized

Implementation of Into ​

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

Implementation of IntoEither ​

rust
impl<T> IntoEither for T

Implementation of Pointable ​

rust
impl<T> Pointable for T

Implementation of Read ​

rust
impl<T> Read<Exclusive, BecauseExclusive> for T where T: ?Sized

Implementation of Same ​

rust
impl<T> Same for T

Implementation of SupersetOf ​

rust
impl<SS, SP> SupersetOf<SS> for SP where SS: SubsetOf<SP>

Implementation of ToOwned ​

rust
impl<T> ToOwned for T where T: Clone

Implementation of TryFrom ​

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

Implementation of TryInto ​

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

Released under the MIT and Apache 2.0 licenses.