torch.linalg.polar#
- torch.linalg.polar(A, *, out=None)[source]#
Computes the polar decomposition of a matrix.
Letting be or , the polar decomposition of a matrix with m >= n is defined as
where has orthonormal columns (it is orthogonal in the real case and unitary in the complex case) and is symmetric positive-semidefinite in the real case and Hermitian positive-semidefinite in the complex case.
The orthogonal factor is the closest matrix with orthonormal columns to in the Frobenius norm, which makes the polar decomposition a useful tool for orthogonalization.
Note
torch.linalg.polar()computes the polar decomposition of a matrix, like SciPy’s scipy.linalg.polar. It is not related totorch.polar(), which constructs a complex tensor from absolute values and angles like C++’s std::polar.Supports input of float, double, cfloat and cdouble dtypes. Also supports batches of matrices, and if
Ais a batch of matrices then the output has the same batch dimensions.On CUDA, this is computed with the QR-based Dynamically Weighted Halley (QDWH) algorithm via cuSOLVER when nvmath-python is installed and the cuSOLVER runtime is >= 12.2 (CUDA 13.2, which introduces the required
cusolverDnXpolarroutine); otherwise (and on CPU) it falls back to an SVD-based computation.Note
This function is not differentiable. Calling it on a tensor that requires grad and backpropagating raises an error; an autograd formula may be added in a future release.
Warning
This function is “experimental” and it may change in a future PyTorch release.
- Parameters:
A (Tensor) – tensor of shape (*, m, n) with m >= n, where * is zero or more batch dimensions.
- Keyword Arguments:
out (tuple, optional) – output tuple of two tensors. Ignored if None. Default: None.
- Returns:
A named tuple (U, H).
Examples:
>>> A = torch.randn(4, 3) >>> U, H = torch.linalg.polar(A) >>> torch.dist(U @ H, A) tensor(7.1512e-07) >>> torch.dist(U.mT @ U, torch.eye(3)) tensor(4.8995e-07)