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,
)[source]#

Find k eigenvalues and eigenvectors of the real symmetric square matrix or complex Hermitian matrix A.

Solves Ax = wx, the standard eigenvalue problem for w eigenvalues with corresponding eigenvectors x.

Args:
a (spmatrix): A symmetric square sparse CSR matrix with

dimension (n, n). a must be of type cupyx.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 k largest (in magnitude) eigenvalues. ‘LA’: finds k largest (algebraic) eigenvalues. ‘SA’: finds k smallest (algebraic) eigenvalues. ‘SM’: finds k smallest (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. If None, 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||. If 0, machine

precision is used.

Returns:
tuple:

It returns w and x where w is eigenvalues and x is 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,
)[source]#

Compute the largest k singular 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 type cupyx.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": skip Vt, return (U, S, None).

  • "vh": skip U, 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) where U is left singular vectors (m, k), S is singular values (k,) in descending order, and Vt is right singular vectors (k, n). U and/or Vt may be None depending on return_singular_vectors.

Note

This function uses randomized SVD (Halko et al. 2009) with CholeskyQR2 orthogonalization (Tomas et al. 2024) for efficient GPU execution.