fn

svd

Tensor
svd(x: Tensor, full_matrices: bool = True)
source

Singular value decomposition of a matrix.

Factorizes any (possibly rectangular) matrix ARm×nA \in \mathbb{R}^{m \times n} into

A=UΣV,A \,=\, U \,\Sigma\, V^\top,

where UU and VV are orthogonal and Σ=diag(σ1,,σk)\Sigma = \mathrm{diag}(\sigma_1, \ldots, \sigma_k) with σ1σ2σk0\sigma_1 \ge \sigma_2 \ge \cdots \ge \sigma_k \ge 0 and k=min(m,n)k = \min(m, n). The SVD is the foundation of low-rank approximation, pseudo-inverse, matrix rank, condition number, and PCA.

Parameters

xTensor
Input matrix of shape (*, m, n) (batch dims allowed).
full_matricesbool= True
If True (default), UU is (m, m) and VV^\top is (n, n) — the full orthogonal factors. If False, returns the reduced SVD with UU shaped (m, k) and VV^\top shaped (k, n).

Returns

Tensor

Left singular vectors, shape (*, m, m) or (*, m, k).

Notes

Backward is implemented via three separate Function wrappers — one per output — so gradients from UU, SS, and VV^\top accumulate into the input via Giles' (2008) formula using the Loewner matrix Fij=σi/(σi2σj2)F_{ij} = \sigma_i / (\sigma_i^2 - \sigma_j^2). Gradients blow up when singular values are nearly repeated — degenerate or rank-deficient inputs are not differentiation-friendly.

Cost is O(min(m,n)2max(m,n))O(\min(m,n)^2 \cdot \max(m,n)).

Examples

>>> import lucid
>>> from lucid.linalg import svd
>>> A = lucid.tensor([[1.0, 0.0], [0.0, 2.0], [0.0, 0.0]])
>>> U, S, Vh = svd(A, full_matrices=False)
>>> S
Tensor([2.0000, 1.0000])