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,
Compute the largest
ksingular values and corresponding singular vectors of a sparse matrix using randomized SVD.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. Default 10.- n_power_iters (int): Number of power iteration passes. More
iterations improve accuracy for matrices with slowly decaying singular values. 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 and final matrix multiplication.
handle: RAFT resource handle. If
None, a default is created.- 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
This function uses randomized SVD (Halko et al. 2009) with CholeskyQR2 orthogonalization (Tomas et al. 2024) for efficient GPU execution.