EllMatrix

Struct EllMatrix 

Source
pub struct EllMatrix<T: Scalar> { /* private fields */ }
Expand description

ELLPACK sparse matrix format.

Efficient for:

  • GPU computation
  • Vectorized operations
  • Matrices with uniform row lengths

§Storage

Each row stores exactly width entries. The width is typically the maximum number of non-zeros in any row. Rows with fewer non-zeros are padded with zeros and a special “invalid” column index (usually ncols or usize::MAX).

§Example

use oxiblas_sparse::EllMatrix;

// Create a sparse matrix:
// [1 2 0 0]
// [0 3 4 0]
// [5 0 0 6]
let width = 2; // max 2 non-zeros per row
let data = vec![
    vec![1.0, 2.0],  // row 0
    vec![3.0, 4.0],  // row 1
    vec![5.0, 6.0],  // row 2
];
let indices = vec![
    vec![0, 1],  // row 0
    vec![1, 2],  // row 1
    vec![0, 3],  // row 2
];

let ell = EllMatrix::new(3, 4, width, data, indices).unwrap();
assert_eq!(ell.width(), 2);

Implementations§

Source§

impl<T: Scalar + Clone> EllMatrix<T>

Source

pub fn new( nrows: usize, ncols: usize, width: usize, data: Vec<Vec<T>>, indices: Vec<Vec<usize>>, ) -> Result<Self, EllError>

Creates a new ELL matrix from raw components.

§Arguments
  • nrows - Number of rows
  • ncols - Number of columns
  • width - Maximum non-zeros per row
  • data - Data array (nrows × width)
  • indices - Column indices array (nrows × width)
§Errors

Returns an error if the input is invalid.

Source

pub unsafe fn new_unchecked( nrows: usize, ncols: usize, width: usize, data: Vec<Vec<T>>, indices: Vec<Vec<usize>>, ) -> Self

Creates an ELL matrix without validation (unsafe but faster).

§Safety

The caller must ensure:

  • data.len() == nrows and each row has length width
  • indices.len() == nrows and each row has length width
  • All valid column indices are < ncols
Source

pub fn zeros(nrows: usize, ncols: usize) -> Self

Creates an empty ELL matrix with given dimensions.

Source

pub fn eye(n: usize) -> Self
where T: Field,

Creates an identity matrix in ELL format.

Source

pub fn nrows(&self) -> usize

Returns the number of rows.

Source

pub fn ncols(&self) -> usize

Returns the number of columns.

Source

pub fn shape(&self) -> (usize, usize)

Returns the shape (nrows, ncols).

Source

pub fn width(&self) -> usize

Returns the width (max non-zeros per row).

Source

pub fn nnz(&self) -> usize
where T: Field,

Returns the number of non-zero elements.

Note: This counts actual non-zeros, not stored values.

Source

pub fn nstored(&self) -> usize

Returns the total stored values (including padding).

Source

pub fn efficiency(&self) -> f64
where T: Field,

Returns the storage efficiency (nnz / nstored).

Source

pub fn data(&self) -> &[Vec<T>]

Returns a reference to the data array.

Source

pub fn data_mut(&mut self) -> &mut [Vec<T>]

Returns a mutable reference to the data array.

Source

pub fn indices(&self) -> &[Vec<usize>]

Returns a reference to the indices array.

Source

pub fn get(&self, row: usize, col: usize) -> Option<&T>

Gets the value at (row, col), returning None if not present.

Source

pub fn get_or_zero(&self, row: usize, col: usize) -> T
where T: Field,

Gets the value at (row, col), returning zero if not present.

Source

pub fn row_iter(&self, row: usize) -> impl Iterator<Item = (usize, &T)>

Returns an iterator over non-zeros in a row as (col, value).

Source

pub fn iter(&self) -> impl Iterator<Item = (usize, usize, &T)> + '_

Returns an iterator over all non-zeros as (row, col, value).

Source

pub fn matvec(&self, x: &[T], y: &mut [T])
where T: Field,

Matrix-vector product: y = A * x.

Source

pub fn mul_vec(&self, x: &[T]) -> Vec<T>
where T: Field,

Matrix-vector product returning a new vector: y = A * x.

Source

pub fn to_csr(&self) -> CsrMatrix<T>
where T: Field,

Converts to CSR format.

Source

pub fn to_dense(&self) -> Mat<T>
where T: Field + Zeroable,

Converts to dense matrix.

Source

pub fn from_dense(dense: &MatRef<'_, T>, max_width: Option<usize>) -> Self
where T: Field,

Creates an ELL matrix from a dense matrix.

§Arguments
  • dense - Source dense matrix
  • max_width - Maximum width (if None, uses actual max non-zeros per row)
Source

pub fn from_csr( csr: &CsrMatrix<T>, max_width: Option<usize>, ) -> Result<Self, EllError>
where T: Field,

Creates an ELL matrix from CSR format.

§Arguments
  • csr - Source CSR matrix
  • max_width - Maximum width (if None, uses actual max non-zeros per row)
Source

pub fn scale(&mut self, alpha: T)

Scales all values by a scalar.

Source

pub fn scaled(&self, alpha: T) -> Self

Returns a scaled copy of this matrix.

Source

pub fn transpose(&self) -> Self
where T: Field,

Returns the transpose of this matrix.

Note: This is less efficient than CSR/CSC transpose.

Trait Implementations§

Source§

impl<T: Clone + Scalar> Clone for EllMatrix<T>

Source§

fn clone(&self) -> EllMatrix<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + Scalar> Debug for EllMatrix<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for EllMatrix<T>

§

impl<T> RefUnwindSafe for EllMatrix<T>
where T: RefUnwindSafe,

§

impl<T> Send for EllMatrix<T>

§

impl<T> Sync for EllMatrix<T>

§

impl<T> Unpin for EllMatrix<T>
where T: Unpin,

§

impl<T> UnwindSafe for EllMatrix<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

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

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

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

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

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

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.