numpy.linalg.svd

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

Singular Value Decomposition.

Factors the matrix a as u * np.diag(s) * v, where u and v are unitary and s is a 1-d array of a‘s singular values.

Parameters :

a : array_like

A real or complex matrix of shape (M, N) .

full_matrices : bool, optional

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

compute_uv : bool, optional

Whether or not to compute u and v 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 : ndarray

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

Raises :

LinAlgError :

If SVD computation does not converge.

Notes

The SVD is commonly written as a = U S V.H. The v returned by this function is V.H and u = U.

If U is a unitary matrix, it means that it satisfies U.H = inv(U).

The rows of v are the eigenvectors of a.H a. The columns of u are the eigenvectors of a a.H. For row i in v and column i in u, the corresponding eigenvalue is s[i]**2.

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)

Reconstruction based on full SVD:

>>> U, s, V = np.linalg.svd(a, full_matrices=True)
>>> U.shape, V.shape, s.shape
((9, 6), (6, 6), (6,))
>>> S = np.zeros((9, 6), dtype=complex)
>>> S[:6, :6] = np.diag(s)
>>> np.allclose(a, np.dot(U, np.dot(S, V)))
True

Reconstruction based on reduced SVD:

>>> U, s, V = np.linalg.svd(a, full_matrices=False)
>>> U.shape, V.shape, s.shape
((9, 6), (6, 6), (6,))
>>> S = np.diag(s)
>>> np.allclose(a, np.dot(U, np.dot(S, V)))
True

Previous topic

numpy.linalg.qr

Next topic

numpy.linalg.eig

This Page