{ } Raw JSON

bundles / scipy 1.18.0 / scipy / linalg / _decomp_svd / svdvals

function

scipy.linalg._decomp_svd:svdvals

source: /scipy/linalg/_decomp_svd.py:215

Signature

def svdvals(a, overwrite_a = False, check_finite = True)

Summary

Compute singular values of a matrix.

Parameters

a : (M, N) array_like

Matrix to decompose.

overwrite_a : bool, optional

Whether to overwrite data in a (may improve performance). Default is False. See tutorial_linalg_overwrite for details.

check_finite : bool, optional

Whether 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.

Returns

s : (min(M, N),) ndarray

The singular values, sorted in decreasing order.

Raises

: LinAlgError

If SVD computation does not converge.

Notes

Array argument of this function, a, 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.

Examples

import numpy as np
from scipy.linalg import svdvals
m = np.array([[1.0, 0.0],
              [2.0, 3.0],
              [1.0, 1.0],
              [0.0, 2.0],
              [1.0, 0.0]])
✓
svdvals(m)
✗
array([4.281, 1.635])
If the input matrix has more than two dimensions, it is interpreted as a batch of two-dimensional matrices:
mm = np.stack((m, 2*m))
✓
svdvals(mm)
✗
array([[4.281, 1.635],
       [8.562, 3.27 ]])
We can verify the maximum singular value of `m` by computing the maximum length of `m @ u` over all the unit vectors `u` in the (x,y) plane. We approximate "all" the unit vectors with a large sample. Because of linearity, we only need the unit vectors with angles in ``[0, pi]``.
t = np.linspace(0, np.pi, 2000)
u = np.array([np.cos(t), np.sin(t)])
✓
np.linalg.norm(m @ u, axis=0).max()
✗
np.float64(4.2809152422538475)
`p` is a projection matrix with rank 1. With exact arithmetic, its singular values would be ``[1, 0, 0, 0]``.
v = np.array([0.1, 0.3, 0.9, 0.3])
p = np.outer(v, v)
✓
svdvals(p)
✗
array([1., 0., 0., 0.])
The singular values of an orthogonal matrix are all 1. Here, we create a random orthogonal matrix by using the ``rvs()`` method of `scipy.stats.ortho_group`.
from scipy.stats import ortho_group
orth = ortho_group.rvs(4)
✓
svdvals(orth)
✗
array([1., 1., 1., 1.])

See also

diagsvd

Construct the Sigma matrix, given the vector s.

svd

Compute the full singular value decomposition of a matrix.

Aliases

  • scipy.linalg.svdvals