Skip to main content

SparseScaling

Trait SparseScaling 

Source
pub trait SparseScaling<R: Runtime> {
    // Required methods
    fn row_norms<T: Element + Default + Copy>(
        &self,
        norm: NormType,
    ) -> Result<Tensor<R>>;
    fn col_norms<T: Element + Default + Copy>(
        &self,
        norm: NormType,
    ) -> Result<Tensor<R>>;
    fn scale_rows<T: Element + Default + Copy + Mul<Output = T>>(
        &self,
        scales: &[T],
    ) -> Result<Self>
       where Self: Sized;
    fn scale_cols<T: Element + Default + Copy + Mul<Output = T>>(
        &self,
        scales: &[T],
    ) -> Result<Self>
       where Self: Sized;
    fn equilibrate<T: Element + Default + Copy + Float>(
        &self,
    ) -> Result<(Self, Vec<T>, Vec<T>)>
       where Self: Sized;
}
Expand description

Sparse matrix scaling and equilibration operations

These utilities improve numerical stability for sparse linear systems, particularly important for ODE/DAE Jacobians which may be badly scaled.

§Example

// Create a sample CSC matrix
// Equilibrate a badly-scaled matrix
let (scaled, row_scales, col_scales) = csc.equilibrate::<f64>()?;

// Scaling factors can be applied to solve Ax = b
// becomes (R A C)(C⁻¹x) = Rb

Required Methods§

Source

fn row_norms<T: Element + Default + Copy>( &self, norm: NormType, ) -> Result<Tensor<R>>

Compute row-wise norms of a sparse matrix.

§Arguments
  • norm - Type of norm to compute (L1, L2, or Linf)
§Returns

Tensor of shape `[nrows]` containing the norm of each row.

Source

fn col_norms<T: Element + Default + Copy>( &self, norm: NormType, ) -> Result<Tensor<R>>

Compute column-wise norms of a sparse matrix.

§Arguments
  • norm - Type of norm to compute (L1, L2, or Linf)
§Returns

Tensor of shape `[ncols]` containing the norm of each column.

Source

fn scale_rows<T: Element + Default + Copy + Mul<Output = T>>( &self, scales: &[T], ) -> Result<Self>
where Self: Sized,

Scale rows of a sparse matrix by given factors.

Computes `B[i,j] = scales[i] * A[i,j]` for all nonzeros.

§Arguments
  • scales - Row scaling factors `[nrows]`
§Returns

New sparse matrix with scaled rows (same sparsity pattern).

Source

fn scale_cols<T: Element + Default + Copy + Mul<Output = T>>( &self, scales: &[T], ) -> Result<Self>
where Self: Sized,

Scale columns of a sparse matrix by given factors.

Computes `B[i,j] = A[i,j] * scales[j]` for all nonzeros.

§Arguments
  • scales - Column scaling factors `[ncols]`
§Returns

New sparse matrix with scaled columns (same sparsity pattern).

Source

fn equilibrate<T: Element + Default + Copy + Float>( &self, ) -> Result<(Self, Vec<T>, Vec<T>)>
where Self: Sized,

Equilibrate the matrix for better numerical stability.

Computes row and column scalings to make all row and column infinity-norms approximately 1. Uses iterative scaling: row-scale, then column-scale, then row-scale again (one iteration is usually sufficient).

§Returns

Tuple of:

  • Scaled matrix (same sparsity pattern)
  • Row scales `[nrows]`
  • Column scales `[ncols]`

To undo the scaling on the solution:

  • If solving Ax = b, the scaled system is (RAC) * (C⁻¹x) = Rb
  • After solving, recover x = C * scaled_x

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<R: Runtime<DType = DType>> SparseScaling<R> for CscData<R>