pub struct BsrArray<T>where
T: Float + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T> + Debug + Copy + 'static + AddAssign,{ /* private fields */ }
Expand description
BSR Array format
The BSR (Block Sparse Row) format stores a sparse matrix as a sparse matrix of dense blocks. It’s particularly efficient for matrices with block-structured sparsity patterns, such as those arising in finite element methods.
§Notes
- Very efficient for matrices with block structure
- Fast matrix-vector products for block-structured matrices
- Reduced indexing overhead compared to CSR for block-structured problems
- Not efficient for general sparse matrices
- Difficult to modify once constructed
Implementations§
Source§impl<T> BsrArray<T>
impl<T> BsrArray<T>
Sourcepub fn new(
data: Vec<Vec<Vec<T>>>,
indices: Vec<Vec<usize>>,
indptr: Vec<usize>,
shape: (usize, usize),
block_size: (usize, usize),
) -> SparseResult<Self>
pub fn new( data: Vec<Vec<Vec<T>>>, indices: Vec<Vec<usize>>, indptr: Vec<usize>, shape: (usize, usize), block_size: (usize, usize), ) -> SparseResult<Self>
Create a new BSR array from raw data
§Arguments
data
- Block data (blocks stored row by row)indices
- Column indices for each blockindptr
- Row pointersshape
- Tuple containing the array dimensions (rows, cols)block_size
- Tuple containing the block dimensions (r, c)
§Returns
- A new BSR array
§Examples
use scirs2_sparse::bsr_array::BsrArray;
use scirs2_sparse::sparray::SparseArray;
// Create a 4x4 sparse array with 2x2 blocks
// [1 2 0 0]
// [3 4 0 0]
// [0 0 5 6]
// [0 0 7 8]
let block1 = vec![vec![1.0, 2.0], vec![3.0, 4.0]];
let block2 = vec![vec![5.0, 6.0], vec![7.0, 8.0]];
let data = vec![block1, block2];
let indices = vec![vec![0], vec![1]];
let indptr = vec![0, 1, 2];
let array = BsrArray::new(data, indices, indptr, (4, 4), (2, 2)).unwrap();
assert_eq!(array.shape(), (4, 4));
assert_eq!(array.nnz(), 8); // All elements in the blocks are non-zero
Sourcepub fn from_triplets(
row: &[usize],
col: &[usize],
data: &[T],
shape: (usize, usize),
block_size: (usize, usize),
) -> SparseResult<Self>
pub fn from_triplets( row: &[usize], col: &[usize], data: &[T], shape: (usize, usize), block_size: (usize, usize), ) -> SparseResult<Self>
Trait Implementations§
Source§impl<T> SparseArray<T> for BsrArray<T>
impl<T> SparseArray<T> for BsrArray<T>
Source§fn to_coo(&self) -> SparseResult<Box<dyn SparseArray<T>>>
fn to_coo(&self) -> SparseResult<Box<dyn SparseArray<T>>>
Returns a sparse array in COO format.
Source§fn to_csr(&self) -> SparseResult<Box<dyn SparseArray<T>>>
fn to_csr(&self) -> SparseResult<Box<dyn SparseArray<T>>>
Returns a sparse array in CSR format.
Source§fn to_csc(&self) -> SparseResult<Box<dyn SparseArray<T>>>
fn to_csc(&self) -> SparseResult<Box<dyn SparseArray<T>>>
Returns a sparse array in CSC format.
Source§fn to_dok(&self) -> SparseResult<Box<dyn SparseArray<T>>>
fn to_dok(&self) -> SparseResult<Box<dyn SparseArray<T>>>
Returns a sparse array in DOK format.
Source§fn to_lil(&self) -> SparseResult<Box<dyn SparseArray<T>>>
fn to_lil(&self) -> SparseResult<Box<dyn SparseArray<T>>>
Returns a sparse array in LIL format.
Source§fn to_dia(&self) -> SparseResult<Box<dyn SparseArray<T>>>
fn to_dia(&self) -> SparseResult<Box<dyn SparseArray<T>>>
Returns a sparse array in DIA format.
Source§fn to_bsr(&self) -> SparseResult<Box<dyn SparseArray<T>>>
fn to_bsr(&self) -> SparseResult<Box<dyn SparseArray<T>>>
Returns a sparse array in BSR format.
Source§fn add(
&self,
other: &dyn SparseArray<T>,
) -> SparseResult<Box<dyn SparseArray<T>>>
fn add( &self, other: &dyn SparseArray<T>, ) -> SparseResult<Box<dyn SparseArray<T>>>
Element-wise addition.
Source§fn sub(
&self,
other: &dyn SparseArray<T>,
) -> SparseResult<Box<dyn SparseArray<T>>>
fn sub( &self, other: &dyn SparseArray<T>, ) -> SparseResult<Box<dyn SparseArray<T>>>
Element-wise subtraction.
Source§fn mul(
&self,
other: &dyn SparseArray<T>,
) -> SparseResult<Box<dyn SparseArray<T>>>
fn mul( &self, other: &dyn SparseArray<T>, ) -> SparseResult<Box<dyn SparseArray<T>>>
Element-wise multiplication.
Source§fn div(
&self,
other: &dyn SparseArray<T>,
) -> SparseResult<Box<dyn SparseArray<T>>>
fn div( &self, other: &dyn SparseArray<T>, ) -> SparseResult<Box<dyn SparseArray<T>>>
Element-wise division.
Source§fn dot(
&self,
other: &dyn SparseArray<T>,
) -> SparseResult<Box<dyn SparseArray<T>>>
fn dot( &self, other: &dyn SparseArray<T>, ) -> SparseResult<Box<dyn SparseArray<T>>>
Matrix multiplication.
Source§fn dot_vector(&self, other: &ArrayView1<'_, T>) -> SparseResult<Array1<T>>
fn dot_vector(&self, other: &ArrayView1<'_, T>) -> SparseResult<Array1<T>>
Matrix-vector multiplication.
Source§fn transpose(&self) -> SparseResult<Box<dyn SparseArray<T>>>
fn transpose(&self) -> SparseResult<Box<dyn SparseArray<T>>>
Transpose the sparse array.
Source§fn copy(&self) -> Box<dyn SparseArray<T>>
fn copy(&self) -> Box<dyn SparseArray<T>>
Return a copy of the sparse array with the specified elements.
Source§fn set(&mut self, i: usize, j: usize, value: T) -> SparseResult<()>
fn set(&mut self, i: usize, j: usize, value: T) -> SparseResult<()>
Set a value at the specified position.
Source§fn eliminate_zeros(&mut self)
fn eliminate_zeros(&mut self)
Eliminate zeros from the sparse array.
Source§fn sort_indices(&mut self)
fn sort_indices(&mut self)
Sort indices of the sparse array.
Source§fn sorted_indices(&self) -> Box<dyn SparseArray<T>>
fn sorted_indices(&self) -> Box<dyn SparseArray<T>>
Return a sorted copy of this sparse array.
Source§fn has_sorted_indices(&self) -> bool
fn has_sorted_indices(&self) -> bool
Check if indices are sorted.
Source§fn sum(&self, axis: Option<usize>) -> SparseResult<SparseSum<T>>
fn sum(&self, axis: Option<usize>) -> SparseResult<SparseSum<T>>
Sum the sparse array elements. Read more
Source§fn find(&self) -> (Array1<usize>, Array1<usize>, Array1<T>)
fn find(&self) -> (Array1<usize>, Array1<usize>, Array1<T>)
Return the indices and values of the nonzero elements.
Source§fn slice(
&self,
row_range: (usize, usize),
col_range: (usize, usize),
) -> SparseResult<Box<dyn SparseArray<T>>>
fn slice( &self, row_range: (usize, usize), col_range: (usize, usize), ) -> SparseResult<Box<dyn SparseArray<T>>>
Return a slice of the sparse array.
Auto Trait Implementations§
impl<T> Freeze for BsrArray<T>
impl<T> RefUnwindSafe for BsrArray<T>where
T: RefUnwindSafe,
impl<T> Send for BsrArray<T>where
T: Send,
impl<T> Sync for BsrArray<T>where
T: Sync,
impl<T> Unpin for BsrArray<T>where
T: Unpin,
impl<T> UnwindSafe for BsrArray<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more