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>
impl<T: Scalar + Clone> BscMatrix<T>
Sourcepub 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>
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 rowsncols- Number of matrix columnsblock_rows- Number of rows per blockblock_cols- Number of columns per blockindptr- Block column pointers (length nb + 1)indices- Block row indicesdata- Dense blocks
§Errors
Returns an error if the input is invalid.
Sourcepub 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
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.
Sourcepub fn zeros(
nrows: usize,
ncols: usize,
block_rows: usize,
block_cols: usize,
) -> Self
pub fn zeros( nrows: usize, ncols: usize, block_rows: usize, block_cols: usize, ) -> Self
Creates an empty BSC matrix with given dimensions.
Sourcepub fn eye(n: usize, block_size: usize) -> Selfwhere
T: Field,
pub fn eye(n: usize, block_size: usize) -> Selfwhere
T: Field,
Creates an identity matrix in BSC format.
Sourcepub fn block_shape(&self) -> (usize, usize)
pub fn block_shape(&self) -> (usize, usize)
Returns the block dimensions (block_rows, block_cols).
Sourcepub fn nblock_rows(&self) -> usize
pub fn nblock_rows(&self) -> usize
Returns the number of block rows.
Sourcepub fn nblock_cols(&self) -> usize
pub fn nblock_cols(&self) -> usize
Returns the number of block columns.
Sourcepub fn nnz(&self) -> usizewhere
T: Field,
pub fn nnz(&self) -> usizewhere
T: Field,
Returns the number of non-zero scalar elements.
Note: This counts actual non-zeros within blocks, not stored values.
Sourcepub fn data(&self) -> &[DenseBlock<T>]
pub fn data(&self) -> &[DenseBlock<T>]
Returns the block data.
Sourcepub fn data_mut(&mut self) -> &mut [DenseBlock<T>]
pub fn data_mut(&mut self) -> &mut [DenseBlock<T>]
Returns mutable block data.
Sourcepub fn get_block(&self, bi: usize, bj: usize) -> Option<&DenseBlock<T>>
pub fn get_block(&self, bi: usize, bj: usize) -> Option<&DenseBlock<T>>
Gets the block at block position (bi, bj), if present.
Sourcepub fn get(&self, row: usize, col: usize) -> Option<T>where
T: Field,
pub fn get(&self, row: usize, col: usize) -> Option<T>where
T: Field,
Gets the scalar value at (row, col).
Sourcepub fn get_or_zero(&self, row: usize, col: usize) -> Twhere
T: Field,
pub fn get_or_zero(&self, row: usize, col: usize) -> Twhere
T: Field,
Gets the scalar value at (row, col), returning zero if not present.
Sourcepub fn block_iter(
&self,
) -> impl Iterator<Item = (usize, usize, &DenseBlock<T>)> + '_
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).
Sourcepub fn col_block_iter(
&self,
bj: usize,
) -> impl Iterator<Item = (usize, &DenseBlock<T>)> + '_
pub fn col_block_iter( &self, bj: usize, ) -> impl Iterator<Item = (usize, &DenseBlock<T>)> + '_
Returns an iterator over blocks in column bj.
Sourcepub fn iter(&self) -> impl Iterator<Item = (usize, usize, T)> + '_where
T: Field,
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).
Sourcepub fn matvec_transpose(&self, x: &[T], y: &mut [T])where
T: Field,
pub fn matvec_transpose(&self, x: &[T], y: &mut [T])where
T: Field,
Transposed matrix-vector product: y = A^T * x.
Sourcepub fn mul_vec(&self, x: &[T]) -> Vec<T>where
T: Field,
pub fn mul_vec(&self, x: &[T]) -> Vec<T>where
T: Field,
Matrix-vector product returning a new vector.
Sourcepub fn from_bsr(bsr: &BsrMatrix<T>) -> Selfwhere
T: Field,
pub fn from_bsr(bsr: &BsrMatrix<T>) -> Selfwhere
T: Field,
Creates a BSC matrix from BSR format.
Sourcepub fn from_csc(
csc: &CscMatrix<T>,
block_rows: usize,
block_cols: usize,
) -> Selfwhere
T: Field,
pub fn from_csc(
csc: &CscMatrix<T>,
block_rows: usize,
block_cols: usize,
) -> Selfwhere
T: Field,
Creates a BSC matrix from CSC format.