dpnp.linalg.svd
- dpnp.linalg.svd(a, full_matrices=True, compute_uv=True, hermitian=False)[source]
Singular Value Decomposition.
For full documentation refer to
numpy.linalg.svd
.- Parameters:
a ((..., M, N) {dpnp.ndarray, usm_ndarray}) -- Input array with
a.ndim >= 2
.full_matrices ({bool}, optional) -- If
True
, it returns u and Vh with full-sized matrices. IfFalse
, the matrices are reduced in size. Default:True
.compute_uv ({bool}, optional) -- If
False
, it only returns singular values. Default:True
.hermitian ({bool}, optional) -- If True, a is assumed to be Hermitian (symmetric if real-valued), enabling a more efficient method for finding singular values. Default:
False
.
- Returns:
u ({ (…, M, M), (…, M, K) } dpnp.ndarray) -- Unitary matrix, where M is the number of rows of the input array a. The shape of the matrix u depends on the value of full_matrices. If full_matrices is
True
, u has the shape (…, M, M). If full_matrices isFalse
, u has the shape (…, M, K), where K = min(M, N), and N is the number of columns of the input array a. If compute_uv isFalse
, neither u or Vh are computed.s ((…, K) dpnp.ndarray) -- Vector containing the singular values of a, sorted in descending order. The length of s is min(M, N).
Vh ({ (…, N, N), (…, K, N) } dpnp.ndarray) -- Unitary matrix, where N is the number of columns of the input array a. The shape of the matrix Vh depends on the value of full_matrices. If full_matrices is
True
, Vh has the shape (…, N, N). If full_matrices isFalse
, Vh has the shape (…, K, N). If compute_uv isFalse
, neither u or Vh are computed.
Examples
>>> import dpnp as np >>> a = np.random.randn(9, 6) + 1j*np.random.randn(9, 6) >>> b = np.random.randn(2, 7, 8, 3) + 1j*np.random.randn(2, 7, 8, 3)
Reconstruction based on full SVD, 2D case:
>>> u, s, vh = np.linalg.svd(a, full_matrices=True) >>> u.shape, s.shape, vh.shape ((9, 9), (6,), (6, 6)) >>> np.allclose(a, np.dot(u[:, :6] * s, vh)) array([ True]) >>> smat = np.zeros((9, 6), dtype=complex) >>> smat[:6, :6] = np.diag(s) >>> np.allclose(a, np.dot(u, np.dot(smat, vh))) array([ True])
Reconstruction based on reduced SVD, 2D case:
>>> u, s, vh = np.linalg.svd(a, full_matrices=False) >>> u.shape, s.shape, vh.shape ((9, 6), (6,), (6, 6)) >>> np.allclose(a, np.dot(u * s, vh)) array([ True]) >>> smat = np.diag(s) >>> np.allclose(a, np.dot(u, np.dot(smat, vh))) array([ True])
Reconstruction based on full SVD, 4D case:
>>> u, s, vh = np.linalg.svd(b, full_matrices=True) >>> u.shape, s.shape, vh.shape ((2, 7, 8, 8), (2, 7, 3), (2, 7, 3, 3)) >>> np.allclose(b, np.matmul(u[..., :3] * s[..., None, :], vh)) array([ True]) >>> np.allclose(b, np.matmul(u[..., :3], s[..., None] * vh)) array([ True])
Reconstruction based on reduced SVD, 4D case:
>>> u, s, vh = np.linalg.svd(b, full_matrices=False) >>> u.shape, s.shape, vh.shape ((2, 7, 8, 3), (2, 7, 3), (2, 7, 3, 3)) >>> np.allclose(b, np.matmul(u * s[..., None, :], vh)) array([ True]) >>> np.allclose(b, np.matmul(u, s[..., None] * vh)) array([ True])