Skip to main content

Crate single_svdlib

Crate single_svdlib 

Source
Expand description

Sparse singular value decomposition.

Three solvers over sprs matrices, all returning the same SvdRec:

modulemethoduse when
irlbathick-restarted Lanczos bidiagonalizationdefault. Accurate, memory bounded by the requested rank
randomizedrandomized range finder, power iteration or block Krylovvery large inputs where an approximation is acceptable
lanczosLAS2 from SVDLIBCdeprecated, numerically unreliable — behind the off-by-default las2 feature

§Quick start

use single_svdlib::{sprs::TriMatI, SvdMat};

// A 4x3 matrix in triplet form, converted to CSR with u32 indices.
let mut tri = TriMatI::<f64, u32>::new((4, 3));
tri.add_triplet(0, 0, 1.0);
tri.add_triplet(1, 1, 2.0);
tri.add_triplet(2, 2, 3.0);
tri.add_triplet(3, 0, 4.0);
let a: SvdMat<f64> = tri.to_csr::<u64>();

// Two largest singular triplets.
let svd = single_svdlib::svd(&a, 2)?;

assert_eq!(svd.s.len(), 2);
assert_eq!(svd.u.dim(), (4, 2));   // left vectors are columns
assert_eq!(svd.vt.dim(), (2, 3));  // right vectors are rows
assert!(svd.s[0] >= svd.s[1]);

§Index widths

SvdMat<T> defaults to u32 column indices with u64 row pointers, which is 12 bytes per non-zero for f64 data against the 16 that usize-everywhere costs (8 against 16 for f32). Name the parameters to widen: SvdMat<f64, u64, u64>.

§Orientation

A ≈ u · diag(s) · vt, matching numpy.linalg.svd: u is m × d with left vectors as columns, s is descending, vt is d × n with right vectors as rows. 1.x was inconsistent between solvers on this point.

Re-exports§

pub use error::Result;
pub use error::SvdLibError;
pub use matrix::MaskedCsMat;
pub use matrix::SparseMat;
pub use matrix::SparseMatDense;
pub use matrix::SvdMat;
pub use matrix::SvdMatView;
pub use matrix::DEFAULT_SCRATCH_BUDGET;
pub use types::Algorithm;
pub use types::Detail;
pub use types::Diagnostics;
pub use types::SvdFloat;
pub use types::SvdRec;
pub use sprs;

Modules§

dense
Dense helpers: tall-skinny QR, and small factorizations on the reduced matrices the Krylov and randomized methods produce.
error
irlba
Thick-restarted Lanczos bidiagonalization.
lanczos
LAS2, from SVDLIBC. Deprecated and numerically unreliable — enable las2 only to keep a 1.x caller compiling while it moves to irlba. Single-vector Lanczos with selective reorthogonalization — a port of LAS2 from Doug Rohde’s SVDLIBC.
matrix
Sparse operands and the traits the solvers consume.
randomized
Randomized SVD: range finding by random projection.
types
Result and scalar types shared by every algorithm in the crate.

Functions§

svd
The rank largest singular triplets.
svd_centered
PCA: the rank largest singular triplets of the implicitly mean-centered matrix.
svd_seed
The rank largest singular triplets, reproducibly.