Sparse#
This page provides pylibraft class references for the publicly-exposed elements of the pylibraft.sparse.linalg package.
- pylibraft.sparse.linalg.eigsh(
- A,
- k=6,
- which='LM',
- v0=None,
- ncv=None,
- maxiter=None,
- tol=0,
- seed=None,
- handle=None,
Find
keigenvalues and eigenvectors of the real symmetric square matrix or complex Hermitian matrixA.Solves
Ax = wx, the standard eigenvalue problem forweigenvalues with corresponding eigenvectorsx.- Args:
- a (spmatrix): A symmetric square sparse CSR matrix with
dimension
(n, n).amust be of typecupyx.scipy.sparse._csr.csr_matrix- k (int): The number of eigenvalues and eigenvectors to compute. Must be
1 <= k < n.- which (str): ‘LM’ or ‘LA’ or ‘SA’.
‘LM’: finds
klargest (in magnitude) eigenvalues. ‘LA’: findsklargest (algebraic) eigenvalues. ‘SA’: findsksmallest (algebraic) eigenvalues. ‘SM’: findsksmallest (in magnitude) eigenvalues.- v0 (ndarray): Starting vector for iteration. If
None, a random unit vector is used.
- ncv (int): The number of Lanczos vectors generated. Must be
k + 1 < ncv < n. IfNone, default value is used.- maxiter (int): Maximum number of Lanczos update iterations.
If
None, default value is used.- tol (float): Tolerance for residuals
||Ax - wx||. If0, machine precision is used.
- Returns:
- tuple:
It returns
wandxwherewis eigenvalues andxis eigenvectors.
See also
scipy.sparse.linalg.eigsh()cupyx.scipy.sparse.linalg.eigsh()Note
This function uses the thick-restart Lanczos methods (https://sdm.lbl.gov/~kewu/ps/trlan.html).
- pylibraft.sparse.linalg.svds(
- A,
- k=6,
- n_oversamples=10,
- n_power_iters=2,
- seed=None,
- return_singular_vectors=True,
- handle=None,
- solver='randomized',
- ncv=None,
- tol=1e-4,
- maxiter=None,
- orthogonalization='cgs2',
Compute the largest
ksingular values and corresponding singular vectors of a sparse matrix.Computes the truncated SVD:
A ~ U @ diag(S) @ Vt.- Args:
- A (cupyx.scipy.sparse.csr_matrix): Sparse CSR matrix of shape
(m, n). Must be of typecupyx.scipy.sparse.csr_matrix.- k (int): Number of singular values and vectors to compute. Must be
1 <= k < min(m, n). Default 6.- n_oversamples (int): Number of extra random vectors for better
approximation. Total subspace dimension is
k + n_oversamples. Used only whensolver="randomized". Default 10.- n_power_iters (int): Number of power iteration passes. More
iterations improve accuracy for matrices with slowly decaying singular values. Used only when
solver="randomized". Default 2.- seed (int or None): Random seed for reproducibility. If
None, a non-deterministic seed is used.
- return_singular_vectors (bool or {“u”, “vh”}): Controls which
singular vectors are returned (matches
scipy.sparse.linalg.svds()).True(default): return(U, S, Vt).False: skip both vector matrices, return(None, S, None)."u": skipVt, return(U, S, None)."vh": skipU, return(None, S, Vt).
Skipping a side avoids the corresponding output buffer. Depending on the selected solver, some intermediate vector products may still be required for numerical refinement.
handle: RAFT resource handle. If
None, a default is created. solver ({ “randomized”, “lanczos” }): Solver to use. The default"randomized"preserves the existing approximate randomized SVD behavior."lanczos"uses Lanczos bidiagonalization and is the higher-accuracy, ARPACK-like sparse SVD path.- ncv (int or None): Number of Lanczos vectors. Used only when
solver="lanczos". IfNone, a default is selected. Larger values can improve convergence margin and orthogonality for difficult spectra, but increase sparse matrix-vector work. The value is clamped to[k + 10, min(A.shape) - 1]; the extra subspace slack is required for reliable convergence, in particular with repeated or tightly clustered singular values.- tol (float): Lanczos residual tolerance. Used only when
solver="lanczos".- maxiter (int or None): Maximum Lanczos restart iterations. Used only
when
solver="lanczos". IfNone, defaults to 100. The Lanczos solver raises an error if all requested singular triplets do not converge within this limit.- orthogonalization ({ “cgs2”, “mgs2” }): Lanczos reorthogonalization
method.
"cgs2"is the GPU-efficient default."mgs2"is more launch-heavy and intended as an alternate path for difficult float32 spectra.
- Returns:
- tuple:
(U, S, Vt)whereUis left singular vectors(m, k),Sis singular values(k,)in descending order, andVtis right singular vectors(k, n).Uand/orVtmay beNonedepending onreturn_singular_vectors.
See also
Note
With
solver="randomized", this function uses randomized SVD (Halko et al. 2009) with CholeskyQR2 orthogonalization (Tomas et al. 2024). Withsolver="lanczos", it uses implicitly restarted Lanczos bidiagonalization with full reorthogonalization and finalA @ Vpost-refinement of the returned left singular vectors.