numpy.linalg.svd

numpy.linalg.svd(a, full_matrices=1, compute_uv=1)

Singular Value Decomposition.

Factors the matrix a into u * np.diag(s) * v.H, where u and v are unitary (i.e., u.H = inv(u) and similarly for v), .H is the conjugate transpose operator (which is the ordinary transpose for real-valued matrices), and s is a 1-D array of a‘s singular values.

Parameters:

a : array_like

Matrix of shape (M, N) to decompose.

full_matrices : bool, optional

If True (default), u and v.H have the shapes (M, M) and (N, N), respectively. Otherwise, the shapes are (M, K) and (K, N), resp., where K = min(M, N).

compute_uv : bool, optional

Whether or not to compute u and v.H in addition to s. True by default.

Returns:

u : ndarray

Unitary matrix. The shape of U is (M, M) or (M, K) depending on value of full_matrices.

s : ndarray

The singular values, sorted so that s[i] >= s[i+1]. S is a 1-D array of length min(M, N)

v.H : ndarray

Unitary matrix of shape (N, N) or (K, N), depending on full_matrices.

Raises:

LinAlgError :

If SVD computation does not converge.

Notes

If a is a matrix object (as opposed to an ndarray), then so are all the return values.

Examples

>>> a = np.random.randn(9, 6) + 1j*np.random.randn(9, 6)
>>> U, s, Vh = np.linalg.svd(a)
>>> U.shape, Vh.shape, s.shape
((9, 9), (6, 6), (6,))
>>> U, s, Vh = np.linalg.svd(a, full_matrices=False)
>>> U.shape, Vh.shape, s.shape
((9, 6), (6, 6), (6,))
>>> S = np.diag(s)
>>> np.allclose(a, np.dot(U, np.dot(S, Vh)))
True
>>> s2 = np.linalg.svd(a, compute_uv=False)
>>> np.allclose(s, s2)
True

Previous topic

numpy.linalg.qr

Next topic

numpy.linalg.eig

This Page