bundles / scipy latest / scipy / linalg / _decomp_svd / svd
function
scipy.linalg._decomp_svd:svd
source: /scipy/linalg/_decomp_svd.py :16
Signature
def svd ( a , full_matrices = True , compute_uv = True , overwrite_a = False , check_finite = True , lapack_driver = gesdd ) Summary
Singular Value Decomposition.
Extended Summary
Factorizes the matrix a into two unitary matrices U and Vh, and a 1-D array s of singular values (real, non-negative) such that a == U @ S @ Vh, where S is a suitably shaped matrix of zeros with main diagonal s.
The documentation is written assuming array arguments are of specified "core" shapes. However, array argument(s) of this function may have additional "batch" dimensions prepended to the core shape. In this case, the array is treated as a batch of lower-dimensional slices; see linalg_batch for details. Note that calls with zero-size batches are unsupported and will raise a ValueError.
Parameters
a: (M, N) array_likeMatrix to decompose.
full_matrices: bool, optionalIf True (default), U and Vh are of shape
(M, M),(N, N). If False, the shapes are(M, K)and(K, N), whereK = min(M, N).compute_uv: bool, optionalWhether to compute also
UandVhin addition tos. Default is True.overwrite_a: bool, optionalWhether to overwrite
a; may improve performance. Default is False.check_finite: bool, optionalWhether to check that the input matrix contains only finite numbers. Disabling may give a performance gain, but may result in problems (crashes, non-termination) if the inputs do contain infinities or NaNs.
lapack_driver: {'gesdd', 'gesvd'}, optionalWhether to use the more efficient divide-and-conquer approach (
'gesdd') or general rectangular approach ('gesvd') to compute the SVD. MATLAB and Octave use the'gesvd'approach. Default is'gesdd'.
Returns
U: ndarrayUnitary matrix having left singular vectors as columns. Of shape
(M, M)or(M, K), depending onfull_matrices.s: ndarrayThe singular values, sorted in non-increasing order. Of shape (K,), with
K = min(M, N).Vh: ndarrayUnitary matrix having right singular vectors as rows. Of shape
(N, N)or(K, N)depending onfull_matrices.: For ``compute_uv=False``, only ``s`` is returned.
Raises
: LinAlgErrorIf SVD computation does not converge.
Examples
import numpy as np from scipy import linalg rng = np.random.default_rng() m, n = 9, 6 a = rng.standard_normal((m, n)) + 1.j*rng.standard_normal((m, n)) U, s, Vh = linalg.svd(a) U.shape, s.shape, Vh.shape✓
sigma = np.zeros((m, n)) for i in range(min(m, n)): sigma[i, i] = s[i] a1 = np.dot(U, np.dot(sigma, Vh)) np.allclose(a, a1)✓
U, s, Vh = linalg.svd(a, full_matrices=False) U.shape, s.shape, Vh.shape S = np.diag(s) np.allclose(a, np.dot(U, np.dot(S, Vh)))✓
s2 = linalg.svd(a, compute_uv=False) np.allclose(s, s2)✓
See also
- diagsvd
Construct the Sigma matrix, given the vector s.
- svdvals
Compute singular values of a matrix.
Aliases
-
scipy.linalg.svd