def svd_wrapper(X, rank = None):
"""
Computes the (possibly partial) SVD of a matrix. Handles the case where
X is either dense or sparse.
Parameters
----------
X: either dense or sparse
rank: rank of the desired SVD (required for sparse matrices)
Output
------
U, D, V
the columns of U are the left singular vectors
the COLUMNS of V are the left singular vectors
"""
if isinstance(X, LinearOperator):
scipy_svds = svds(convert2scipy(X), rank)
U, D, V = fix_scipy_svds(scipy_svds)
V = V.T
elif issparse(X):
scipy_svds = svds(X, rank)
U, D, V = fix_scipy_svds(scipy_svds)
V = V.T
else:
# TODO: implement partial SVD
U, D, V = full_svd(X, full_matrices=False)
V = V.T
if rank:
U = U[:, :rank]
D = D[:rank]
V = V[:, :rank]
return U, D, V
评论列表
文章目录