📅  最后修改于: 2023-12-03 15:19:13.815000             🧑  作者: Mango
The getI()
function is a part of numpy.matrix
module of Python programming language. This function is used to compute the (multiplicative) inverse of a matrix. It raises LinAlgError if the matrix is not invertible.
numpy.matrix.getI()
This method does not take any parameters.
This method returns a new matrix which is the (multiplicative) inverse of the current matrix.
import numpy as np
# Creating a matrix
matrix = np.matrix([[1, 2], [3, 4]])
# Displaying the original matrix
print("Original Matrix:\n", matrix)
# Computing the inverse of the matrix
matrix_inv = matrix.getI()
# Displaying the inverse of the matrix
print("Inverse of the Matrix:\n", matrix_inv)
Original Matrix:
[[1 2]
[3 4]]
Inverse of the Matrix:
[[-2. 1. ]
[ 1.5 -0.5]]
Here, we have imported the numpy
module and created a matrix using the np.matrix()
method. Then, we have computed the inverse of the matrix using the getI()
method and displayed it using the print()
function.
If the input matrix is not invertible, it returns a LinAlgError
exception. Let's see an example:
import numpy as np
# A matrix with determinant zero
matrix = np.matrix([[1, 2], [2, 4]])
# Computing the inverse of the matrix
try:
matrix_inv = matrix.getI()
except np.linalg.LinAlgError as e:
print(e)
Singular matrix
Here, we have created a matrix with determinant zero and passed it to the getI()
method. This returns a LinAlgError
exception with the message 'Singular matrix'.