torch.linalg.inv
- torch.linalg.inv(A, *, out=None) → Tensor
Computes the inverse of a square matrix if it exists. Throws a RuntimeError if the matrix is not invertible.
Letting be or , for a matrix , its (if it exists) is defined as
Computes the inverse of a square matrix if it exists. Throws a RuntimeError if the matrix is not invertible.
Letting be or , for a matrix , its (if it exists) is defined as
where is the n-dimensional identity matrix.
The inverse matrix exists if and only if is invertible. In this case, the inverse is unique.
Supports input of float, double, cfloat and cdouble dtypes.
Also supports batches of matrices, and if A is a batch of matrices
then the output has the same batch dimensions.
A (Tensor) – tensor of shape (*, n, n) where * is zero or more batch dimensions consisting of invertible matrices.
out (Tensor, optional) – output tensor. Ignored if None. Default: None.
RuntimeError – if the matrix A or any matrix in the batch of matrices A is not invertible.
Examples:
>>> A = torch.randn(4, 4)
>>> Ainv = torch.linalg.inv(A)
>>> torch.dist(A @ Ainv, torch.eye(4))
tensor(1.1921e-07)
>>> A = torch.randn(2, 3, 4, 4) # Batch of matrices
>>> Ainv = torch.linalg.inv(A)
>>> torch.dist(A @ Ainv, torch.eye(4))
tensor(1.9073e-06)
>>> A = torch.randn(4, 4, dtype=torch.complex128) # Complex matrix
>>> Ainv = torch.linalg.inv(A)
>>> torch.dist(A @ Ainv, torch.eye(4))
tensor(7.5107e-16, dtype=torch.float64)