xxxxxxxxxx
import numpy as np
# Define the matrix
matrix = np.array([[1, 2], [3, 4]])
# Calculate the inverse of the matrix
inverse_matrix = np.linalg.inv(matrix)
# Print the inverse matrix
print(inverse_matrix)
xxxxxxxxxx
# Using the 'inv' function from 'numpy.linalg'
import numpy as np
# Inverse of square matrix M:
M_inverse = np.linalg.inv(M)
# (Notice the result is an numeric approximation
# and might not yield 'perfect' results sometimes)
xxxxxxxxxx
>>> import numpy as np
>>> A = np.array(([1,3,3],[1,4,3],[1,3,4]))
>>> A
array([[1, 3, 3],
[1, 4, 3],
[1, 3, 4]])
>>> A_inv = np.linalg.inv(A)
>>> A_inv
array([[ 7., -3., -3.],
[-1., 1., 0.],
[-1., 0., 1.]])
xxxxxxxxxx
from numpy.linalg import inv
a = np.array([[1., 2.], [3., 4.]])
ainv = inv(a)
np.allclose(np.dot(a, ainv), np.eye(2))
True
np.allclose(np.dot(ainv, a), np.eye(2))
True