How to compute the inverse of a square matrix in PyTorch Last Updated : 13 Jun, 2022 Comments Improve Suggest changes Like Article Like Report In this article, we are going to cover how to compute the inverse of a square matrix in PyTorch. torch.linalg.inv() method we can compute the inverse of the matrix by using torch.linalg.inv() method. It accepts a square matrix and a batch of the square matrices as input. If the input is a batch of the square matrices then the output will also have the same batch dimensions. This method returns the inverse matrix. Syntax: torch.linalg.inv(M) Parameters: M - This is our square matrix or a batch of square matrix. Returns: it will returns the inverse matrix. Example 1: In this example, we will understand how to compute the inverse of a 4x4 square matrix in PyTorch. Python3 # import required library import torch # define a 4x4 square matrix mat = torch.tensor([[ 1.00, -0.000, -0.00, 0.00], [ 4.00, 1.000, 2.00, 0.00], [ -9.00, -3.00, 1.00, 8.00], [ -2.00, -0.00, -0.00, 1.00]]) print("Input Matrix M: \n", mat) # compute the inverse of matrix Mat_inv = torch.linalg.inv(mat) # display result print("\nInverse Matrix: \n", Mat_inv) Output: Example 2: In this example, we will compute the inverse of a batch of square matrices in PyTorch. Python3 # import required library import torch # define a batch of two 3x3 square matrix mat = torch.tensor([[[1.0, 2.0, 3.0], [4.0, 1.0, 6.0], [1.0, 1.0, 1.0]], [[2.0, 2.0, 3.0], [4.0, 5.0, 6.0], [2.0, 2.0, 2.0]]]) print("Input Matrix M: \n", mat) # compute the inverse of matrix Mat_inv = torch.linalg.inv(mat) # display result print("\nInverse Matrix: \n", Mat_inv) Output: Comment More infoAdvertise with us Next Article How to compute the inverse of a square matrix in PyTorch mukulsomukesh Follow Improve Article Tags : Python Python-PyTorch Practice Tags : python Similar Reads How to compute the eigenvalues and eigenvectors of a square matrix in PyTorch? In this article, we are going to discuss how to compute the eigenvalues and eigenvectors of a square matrix in PyTorch. Compute the eigenvalues and eigenvectors of a square matrix in PyTorch torch.linalg.eig() method computes the eigenvalue decomposition of a square matrix or a batch of matrices. Th 2 min read How to get the rank of a matrix in PyTorch In this article, we are going to discuss how to get the rank of a matrix in PyTorch. we can get the rank of a matrix by using torch.linalg.matrix_rank() method.torch.linalg.matrix_rank() methodmatrix_rank() method accepts a matrix and a batch of matrices as the input. This method returns a new tenso 2 min read How to Compute the Pseudoinverse of a Matrix in PyTorch In this article, we are going to discuss how to compute the pseudoinverse of a matrix in Python using PyTorch. torch.linalg.pinv() method torch.linalg.pinv() method accepts a matrix and a batch of matrices as input and returns a new tensor with the pseudoinverse of the input matrix. if the input is 2 min read How to compute the inverse hyperbolic sine in PyTorch? In this article, we are going to discuss how to compute the inverse hyperbolic sine in PyTorch. torch.asinh() method: The torch.asinh() method is used to compute the inverse hyperbolic sine of each element present in a given input tensor. This method accepts both real and complex-valued as input. I 3 min read How to compute QR decomposition of a matrix in Pytorch? In this article, we are going to discuss how to compute the QR decomposition of a matrix in Python using PyTorch. torch.linalg.qr() method accepts a matrix and a batch of matrices as input. This method also supports the input of float, double, cfloat, and cdouble data types. It will return a named t 2 min read Compute the square root of negative input with emath in Python In this article, we will cover how to compute the square root of negative inputs with emath in Python using NumPy. Example:Input: [-3,-4] Output: [0.+1.73205081j 0.+2.j ] Explanation: Square root of a negative input.NumPy.emath.sqrt method: The np.emath.sqrt() method from the NumPy library calculate 2 min read Compute the inverse of a matrix using NumPy The inverse of a matrix is just a reciprocal of the matrix as we do in normal arithmetic for a single number which is used to solve the equations to find the value of unknown variables. The inverse of a matrix is that matrix which when multiplied with the original matrix will give as an identity mat 2 min read Compute the inverse cosine with scimath in Python In this article, we will compute the inverse cosine with scimath in Python using NumPy. numpy.arccos method A NumPy array can be created in different ways like, by various numbers, and by defining the size of the Array. It can also be created with the use of various data types such as lists, tuples, 2 min read How to compute the element-wise angle of given input tensor in PyTorch? In this article, we are going to see how to compute the element-wise angle of a given input tensor in PyTorch. torch.angle() method Pytorch is an open-source deep learning framework available with a Python and C++ interface. Pytorch resides inside the torch module. In PyTorch, we will use torch.angl 3 min read Compute the square root of complex inputs with scimath in Python In this article, we will cover how to compute the square root of complex inputs with scimath in Python using NumPy. ExampleInput: [-1 -2] Output: [0.+1.j 0.+1.41421356j] Explanation: Square root of complex input.NumPy.emath.sqrt method The np.emath.sqrt() method from the NumPy library calculates the 2 min read Like