pub fn tril<T>(
array: &dyn SparseArray<T>,
k: isize,
format: &str,
) -> SparseResult<Box<dyn SparseArray<T>>>
Expand description
Extract lower triangular part of a sparse array
§Arguments
array
- The input sparse arrayk
- Diagonal offset (0 = main diagonal, >0 = above main, <0 = below main)format
- Format of the output array (“csr” or “coo”)
§Returns
A sparse array containing the lower triangular part
§Examples
use scirs2_sparse::construct::eye_array;
use scirs2_sparse::combine::tril;
let a: Box<dyn scirs2_sparse::SparseArray<f64>> = eye_array(3, "csr").unwrap();
let b = tril(&*a, 0, "csr").unwrap();
assert_eq!(b.shape(), (3, 3));
assert_eq!(b.get(0, 0), 1.0);
assert_eq!(b.get(1, 1), 1.0);
assert_eq!(b.get(2, 2), 1.0);
assert_eq!(b.get(1, 0), 0.0); // No non-zero elements below diagonal
// With k=1, include first superdiagonal
let c = tril(&*a, 1, "csr").unwrap();
assert_eq!(c.get(0, 1), 0.0); // Nothing in superdiagonal of identity matrix