BscMatrix

Struct BscMatrix 

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

Block Sparse Column matrix format.

Efficient for:

  • Column-oriented block-structured problems
  • Block-structured direct solvers
  • Column access patterns

§Storage

Stores sparse matrices as a collection of dense blocks arranged in CSC-like structure. Each block is r×c dense matrix.

§Example

use oxiblas_sparse::{BscMatrix, DenseBlock};

// 4×4 matrix with 2×2 blocks (column-oriented):
// [1 2 | 0 0]
// [3 4 | 0 0]
// [----+----]
// [0 0 | 5 6]
// [0 0 | 7 8]
let block1 = DenseBlock::new(2, 2, vec![1.0, 2.0, 3.0, 4.0]);
let block2 = DenseBlock::new(2, 2, vec![5.0, 6.0, 7.0, 8.0]);

let bsc = BscMatrix::new(
    4, 4,           // matrix dimensions
    2, 2,           // block dimensions
    vec![0, 1, 2],  // indptr (2 block columns)
    vec![0, 1],     // indices (block row indices)
    vec![block1, block2],
).unwrap();

assert_eq!(bsc.nblocks(), 2);

Implementations§

Source§

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

Source

pub fn new( nrows: usize, ncols: usize, block_rows: usize, block_cols: usize, indptr: Vec<usize>, indices: Vec<usize>, data: Vec<DenseBlock<T>>, ) -> Result<Self, BscError>

Creates a new BSC matrix from raw components.

§Arguments
  • nrows - Number of matrix rows
  • ncols - Number of matrix columns
  • block_rows - Number of rows per block
  • block_cols - Number of columns per block
  • indptr - Block column pointers (length nb + 1)
  • indices - Block row indices
  • data - Dense blocks
§Errors

Returns an error if the input is invalid.

Source

pub unsafe fn new_unchecked( nrows: usize, ncols: usize, block_rows: usize, block_cols: usize, indptr: Vec<usize>, indices: Vec<usize>, data: Vec<DenseBlock<T>>, ) -> Self

Creates a BSC matrix without validation (unsafe but faster).

§Safety

The caller must ensure all invariants hold.

Source

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

Creates an empty BSC matrix with given dimensions.

Source

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

Creates an identity matrix in BSC format.

Source

pub fn nrows(&self) -> usize

Returns the number of matrix rows.

Source

pub fn ncols(&self) -> usize

Returns the number of matrix columns.

Source

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

Returns the shape (nrows, ncols).

Source

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

Returns the block dimensions (block_rows, block_cols).

Source

pub fn nblock_rows(&self) -> usize

Returns the number of block rows.

Source

pub fn nblock_cols(&self) -> usize

Returns the number of block columns.

Source

pub fn nblocks(&self) -> usize

Returns the number of non-zero blocks.

Source

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

Returns the number of non-zero scalar elements.

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

Source

pub fn nstored(&self) -> usize

Returns the total stored values.

Source

pub fn indptr(&self) -> &[usize]

Returns the block column pointers.

Source

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

Returns the block row indices.

Source

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

Returns the block data.

Source

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

Returns mutable block data.

Source

pub fn get_block(&self, bi: usize, bj: usize) -> Option<&DenseBlock<T>>

Gets the block at block position (bi, bj), if present.

Source

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

Gets the scalar value at (row, col).

Source

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

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

Source

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

Returns an iterator over non-zero blocks as (block_row, block_col, &block).

Source

pub fn col_block_iter( &self, bj: usize, ) -> impl Iterator<Item = (usize, &DenseBlock<T>)> + '_

Returns an iterator over blocks in column bj.

Source

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

Returns an iterator over all non-zero scalars 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 matvec_transpose(&self, x: &[T], y: &mut [T])
where T: Field,

Transposed matrix-vector product: y = A^T * x.

Source

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

Matrix-vector product returning a new vector.

Source

pub fn to_bsr(&self) -> BsrMatrix<T>
where T: Field,

Converts to BSR format.

Source

pub fn from_bsr(bsr: &BsrMatrix<T>) -> Self
where T: Field,

Creates a BSC matrix from BSR format.

Source

pub fn to_csc(&self) -> CscMatrix<T>
where T: Field,

Converts to CSC format.

Source

pub fn from_csc( csc: &CscMatrix<T>, block_rows: usize, block_cols: usize, ) -> Self
where T: Field,

Creates a BSC matrix from CSC 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>, block_rows: usize, block_cols: usize, ) -> Self
where T: Field,

Creates a BSC matrix from a dense matrix.

§Arguments
  • dense - Source dense matrix
  • block_rows - Block row size
  • block_cols - Block column size
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.

The transpose swaps row and column indices and transposes each block.

Trait Implementations§

Source§

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

Source§

fn clone(&self) -> BscMatrix<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 BscMatrix<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 BscMatrix<T>

§

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

§

impl<T> Send for BscMatrix<T>

§

impl<T> Sync for BscMatrix<T>

§

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

§

impl<T> UnwindSafe for BscMatrix<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.