pub fn triu<T>(
array: &dyn SparseArray<T>,
k: isize,
format: &str,
) -> SparseResult<Box<dyn SparseArray<T>>>
Expand description
Extract upper 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 upper triangular part
§Examples
use scirs2_sparse::construct::eye_array;
use scirs2_sparse::combine::triu;
let a: Box<dyn scirs2_sparse::SparseArray<f64>> = eye_array(3, "csr").unwrap();
let b = triu(&*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(0, 1), 0.0); // No non-zero elements above diagonal
// With k=-1, include first subdiagonal
let c = triu(&*a, -1, "csr").unwrap();
assert_eq!(c.get(1, 0), 0.0); // Nothing in subdiagonal of identity matrix