Linear Algebra
Thetorch.linalg module provides linear algebra operations including matrix decompositions, solvers, and eigenvalue computations.
Matrix Properties
norm
The input tensor.
The order of norm. Options:
None, 'fro', 'nuc', inf, -inf, or any int/float.Dimensions to compute the norm over.
Whether to keep the reduced dimensions.
det
Tensor of shape
(*, n, n) where * is zero or more batch dimensions.The determinant
slogdet
The sign and log absolute value of the determinant
matrix_rank
Input matrix of shape
(*, m, n).Absolute tolerance value.
Relative tolerance value.
Whether matrix is Hermitian/symmetric.
Matrix Decompositions
cholesky
Hermitian positive-definite matrix of shape
(*, n, n).Whether to return upper triangular matrix. If False, returns lower triangular.
Lower (or upper) triangular matrix
L such that A = L @ L.T (or A = U.T @ U)qr
Matrix of shape
(*, m, n).One of
'reduced', 'complete', or 'r'.Orthogonal/unitary matrix Q and upper triangular matrix R
svd
Matrix of shape
(*, m, n).Whether to compute full or reduced SVD.
- U: Left singular vectors of shape
(*, m, k) - S: Singular values of shape
(*, k) - Vh: Right singular vectors (conjugate transposed) of shape
(*, k, n)
svdvals
eig
Square matrix of shape
(*, n, n).Complex-valued eigenvalues and eigenvectors
eigvals
eigh
Hermitian or symmetric matrix of shape
(*, n, n).Whether to use upper (
'U') or lower ('L') triangular part.Real eigenvalues (sorted in ascending order) and corresponding eigenvectors
eigvalsh
Matrix Inverse and Solvers
inv
Invertible matrix of shape
(*, n, n).The inverse matrix
pinv
Matrix of shape
(*, m, n).Whether matrix is Hermitian/symmetric.
solve
AX = B.
Square coefficient matrix of shape
(*, n, n).Right-hand side matrix of shape
(*, n, k).Whether to solve
AX = B (True) or XA = B (False).Solution tensor X
lstsq
Coefficient matrix of shape
(*, m, n).Right-hand side of shape
(*, m, k).Least-squares solution and additional information
Matrix Operations
matrix_power
Square matrix of shape
(*, m, m).The exponent (can be negative).
cross
First input tensor.
Second input tensor.
Dimension along which to compute the cross product.
Example Usage
Best Practices
Numerical Stability
Numerical Stability
- Use
torch.linalg.solveinstead of computing inverses explicitly - For ill-conditioned systems, consider using
lstsqor regularization - Use
slogdetinstead ofdetfor large determinants to avoid overflow - Check condition numbers before solving linear systems
Performance Optimization
Performance Optimization
- Batch operations are more efficient than loops
- For symmetric/Hermitian matrices, use
eighinstead ofeig - Use
svdvalsif you only need singular values, not vectors - Consider using lower precision (float32) if accuracy allows
Common Pitfalls
Common Pitfalls
- SVD returns Vh (conjugate transpose), not V
- Eigenvalues from
eigare not sorted; useeighfor sorted eigenvalues matrix_powerwith negative exponents requires invertible matrices- Always check for singular matrices before computing inverses