📜  Python| numpy matrix.getI()(1)

📅  最后修改于: 2023-12-03 15:19:13.815000             🧑  作者: Mango

Python | numpy.matrix.getI()

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.

Syntax
numpy.matrix.getI()
Parameters

This method does not take any parameters.

Return Value

This method returns a new matrix which is the (multiplicative) inverse of the current matrix.

Example
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)
Output
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)
Output
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'.