Skip to main content

MaskedCsMat

Struct MaskedCsMat 

Source
pub struct MaskedCsMat<'a, T, I = u32, Iptr = u64>
where I: SpIndex, Iptr: SpIndex,
{ /* 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>
where T: SvdFloat, I: SpIndex, Iptr: SpIndex,

Source

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.

Source

pub fn with_columns(matrix: &'a CsMatI<T, I, Iptr>, columns: &[usize]) -> Self

A view of the given columns, every row.

Source

pub fn with_rows(matrix: &'a CsMatI<T, I, Iptr>, rows: &[usize]) -> Self

A view of the given rows, every column.

Source

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.

Source

pub fn selected_rows(&self) -> Option<&[usize]>

Selected original row indices, ascending. Empty slice when every row is kept.

Source

pub fn selected_columns(&self) -> Option<&[usize]>

Selected original column indices, ascending. None when every column is kept.

Source

pub fn is_identity(&self) -> bool

Whether the view is the identity, in which case products delegate straight to the underlying matrix.

Source

pub fn uses_all_columns(&self) -> bool

Whether every column is retained.

Source

pub fn inner(&self) -> &'a CsMatI<T, I, Iptr>

The matrix being viewed.

Source

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>
where T: SvdFloat, I: SpIndex, Iptr: SpIndex,

Source§

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

Selected entries only, summed per entry so a column offset can’t cancel it away.

Source§

fn rows(&self) -> usize

Source§

fn cols(&self) -> usize

Source§

fn nnz(&self) -> usize

Source§

fn mul_vec(&self, x: &[T], y: &mut [T], trans: bool)

y = A·x when trans is false, y = Aᵀ·x when true. Read more
Source§

impl<T, I, Iptr> SparseMatDense<T> for MaskedCsMat<'_, T, I, Iptr>
where T: SvdFloat, I: SpIndex, Iptr: SpIndex,

Source§

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, )

out = A·rhs when trans is false, out = Aᵀ·rhs when true. Read more
Source§

fn mul_dense_centered( &self, rhs: ArrayView2<'_, T>, out: ArrayViewMut2<'_, T>, trans: bool, means: ArrayView1<'_, T>, )

The product against 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 more

Auto 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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.