pub struct MaskedCsMat<'a, T, I = u32, Iptr = u64>{ /* private fields */ }Expand description
A view exposing a subset of the rows and/or columns of a CSR matrix.
Both selections keep ascending original order, so masked index i is original index
selected_rows()[i] (resp. selected_columns()[i]).
use single_svdlib::{sprs::TriMatI, MaskedCsMat, SparseMat, SvdMat};
let mut t = TriMatI::<f64, u32>::new((6, 5));
for i in 0..6 { for j in 0..5 { t.add_triplet(i, j, (i * 5 + j) as f64); } }
let a: SvdMat<f64> = t.to_csr::<u64>();
// Cells 0, 2, 4 by genes 1, 3 — a 3x2 matrix, without touching `a`.
let view = MaskedCsMat::submatrix(&a, Some(&[0, 2, 4]), Some(&[1, 3]));
assert_eq!((view.rows(), view.cols()), (3, 2));Implementations§
Source§impl<'a, T, I, Iptr> MaskedCsMat<'a, T, I, Iptr>
impl<'a, T, I, Iptr> MaskedCsMat<'a, T, I, Iptr>
Sourcepub fn submatrix(
matrix: &'a CsMatI<T, I, Iptr>,
rows: Option<&[usize]>,
cols: Option<&[usize]>,
) -> Self
pub fn submatrix( matrix: &'a CsMatI<T, I, Iptr>, rows: Option<&[usize]>, cols: Option<&[usize]>, ) -> Self
A view of the given rows and/or columns. None keeps that axis whole.
Index lists may be in any order and may repeat; the view presents each selected index once, ascending.
§Panics
If any index is out of bounds, or the matrix is not CSR.
Sourcepub fn with_columns(matrix: &'a CsMatI<T, I, Iptr>, columns: &[usize]) -> Self
pub fn with_columns(matrix: &'a CsMatI<T, I, Iptr>, columns: &[usize]) -> Self
A view of the given columns, every row.
Sourcepub fn with_rows(matrix: &'a CsMatI<T, I, Iptr>, rows: &[usize]) -> Self
pub fn with_rows(matrix: &'a CsMatI<T, I, Iptr>, rows: &[usize]) -> Self
A view of the given rows, every column.
Sourcepub fn from_masks(
matrix: &'a CsMatI<T, I, Iptr>,
row_mask: Option<&[bool]>,
col_mask: Option<&[bool]>,
) -> Self
pub fn from_masks( matrix: &'a CsMatI<T, I, Iptr>, row_mask: Option<&[bool]>, col_mask: Option<&[bool]>, ) -> Self
A view built from boolean masks, one entry per original row/column.
§Panics
If a mask’s length does not match the corresponding dimension.
Sourcepub fn selected_rows(&self) -> Option<&[usize]>
pub fn selected_rows(&self) -> Option<&[usize]>
Selected original row indices, ascending. Empty slice when every row is kept.
Sourcepub fn selected_columns(&self) -> Option<&[usize]>
pub fn selected_columns(&self) -> Option<&[usize]>
Selected original column indices, ascending. None when every column is kept.
Sourcepub fn is_identity(&self) -> bool
pub fn is_identity(&self) -> bool
Whether the view is the identity, in which case products delegate straight to the underlying matrix.
Sourcepub fn uses_all_columns(&self) -> bool
pub fn uses_all_columns(&self) -> bool
Whether every column is retained.
Sourcepub fn to_sparse(&self) -> CsMatI<T, I, Iptr>
pub fn to_sparse(&self) -> CsMatI<T, I, Iptr>
Copy the view into an owned sparse matrix. Still sparse — a subset copy, not a densification — and the source is untouched.
A view doesn’t make products cheaper: each one still walks every source non-zero
and checks it against the column table. Since a solver issues hundreds of
products, a restrictive mask usually repays the one-off O(nnz) copy almost
immediately. Stay with the view if the mask keeps most columns, if the copy
wouldn’t fit alongside the source, or if you only need a few products.
§Panics
If the extracted index range would overflow the index type I.
Trait Implementations§
Source§impl<T, I, Iptr> SparseMat<T> for MaskedCsMat<'_, T, I, Iptr>
impl<T, I, Iptr> SparseMat<T> for MaskedCsMat<'_, T, I, Iptr>
Source§fn squared_frobenius(&self) -> f64
fn squared_frobenius(&self) -> f64
Only the selected entries, so the norm describes the view and not the matrix it borrows from.
Source§fn centered_squared_frobenius(&self, means: ArrayView1<'_, T>) -> f64
fn centered_squared_frobenius(&self, means: ArrayView1<'_, T>) -> f64
Selected entries only, summed per entry so a column offset can’t cancel it away.
fn rows(&self) -> usize
fn cols(&self) -> usize
fn nnz(&self) -> usize
Source§impl<T, I, Iptr> SparseMatDense<T> for MaskedCsMat<'_, T, I, Iptr>
impl<T, I, Iptr> SparseMatDense<T> for MaskedCsMat<'_, T, I, Iptr>
Source§fn col_means(&self) -> Array1<T>
fn col_means(&self) -> Array1<T>
Column means of the view — averaged over the selected rows only, so PCA on a row subset centers on that subset’s means rather than the whole matrix’s.
Source§fn mul_dense(
&self,
rhs: ArrayView2<'_, T>,
out: ArrayViewMut2<'_, T>,
trans: bool,
)
fn mul_dense( &self, rhs: ArrayView2<'_, T>, out: ArrayViewMut2<'_, T>, trans: bool, )
Source§fn mul_dense_centered(
&self,
rhs: ArrayView2<'_, T>,
out: ArrayViewMut2<'_, T>,
trans: bool,
means: ArrayView1<'_, T>,
)
fn mul_dense_centered( &self, rhs: ArrayView2<'_, T>, out: ArrayViewMut2<'_, T>, trans: bool, means: ArrayView1<'_, T>, )
A - 1·meansᵀ, without ever forming it — centering a sparse
matrix would destroy its sparsity, so it goes in as the rank-1 update it is: Read moreAuto Trait Implementations§
impl<'a, T, I, Iptr> Freeze for MaskedCsMat<'a, T, I, Iptr>
impl<'a, T, I, Iptr> RefUnwindSafe for MaskedCsMat<'a, T, I, Iptr>
impl<'a, T, I, Iptr> Send for MaskedCsMat<'a, T, I, Iptr>where
T: Sync,
impl<'a, T, I, Iptr> Sync for MaskedCsMat<'a, T, I, Iptr>where
T: Sync,
impl<'a, T, I, Iptr> Unpin for MaskedCsMat<'a, T, I, Iptr>
impl<'a, T, I, Iptr> UnsafeUnpin for MaskedCsMat<'a, T, I, Iptr>
impl<'a, T, I, Iptr> UnwindSafe for MaskedCsMat<'a, T, I, Iptr>
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
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>
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>
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