Numpy.linalg.inv – Python NumPy linalg.inv() Function

NumPy linalg.inv() Function:

Numpy linalg inv: The (multiplicative) inverse of a matrix is calculated using the linalg.inv() function of the NumPy module.

The inverse of a matrix is such that if it is multiplied by the original matrix, the result is an identity matrix.

Given a square matrix ‘a’, It returns the matrix ainv satisfying:

dot(a, ainv) = dot(ainv, a) = eye(a.shape[0])

Syntax:

numpy.linalg.inv(a)

Parameters

a: This is required. It is a square matrix for which the inverse is to be calculated.

Return Value:

The (Multiplicative) inverse of the matrix ‘a’ is returned.

NOTE: If “a” is not a square matrix or inversion fails, the LinAlgError exception is thrown.

NumPy linalg.inv() Function in Python

Example1:

Approach:

  • Import numpy module using the import keyword.
  • Pass some random matrix values as an argument to the array() function to create an array.
  • Store it in a variable.
  • Print the above-given array.
  • Pass the given array as an argument to the linalg.inv() function of numpy module to calculate the inverse of the given array(matrix).
  • Store it in another variable.
  • Print the Inverse of the given array(matrix).
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random matrix values as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([[5,6],[4, 3]])
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
# Pass the given array as an argument to the linalg.inv() function of numpy module 
# to calculate the inverse of the given array(matrix).
# Store it in another variable.
rslt_inverse = np.linalg.inv(gvn_arry)
# Print the Inverse of the given array(matrix).
print("The Inverse of the given array(matrix) =\n", rslt_inverse)

Output:

The above given array is:
[[5 6]
 [4 3]]
The Inverse of the given array(matrix) =
[[-0.33333333 0.66666667]
 [ 0.44444444 -0.55555556]]

Example2:

numpy.linalg.inv: The Inverse of a stack of matrices can also be calculated using this function.

Approach:

  • Import numpy module using the import keyword.
  • Pass some random stack of matrices as the arguments to the array() function to create an array.
  • Store it in a variable.
  • Print the above-given array.
  • Pass the given array as an argument to the linalg.inv() function of numpy module to calculate the inverse of the given array.
  • Store it in another variable.
  • Print the inverse of the given array(stack of matrices).
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random stack of matrices as the arguments to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([ [[5, 1], [6, 4]], [[2, 8], [4, 1]], [[1, 5], [2, 6]] ])
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
print()
# Pass the given array as an argument to the linalg.inv() function of numpy module 
# to calculate the Inverse of the given array.
# Store it in another variable.
rslt_inverse = np.linalg.inv(gvn_arry)
# Print the Inverse of the given array(stack of matrices).
print("The Inverse of the given array(stack of matrices) =\n", rslt_inverse)

Output:

The above given array is:
[[[5 1]
  [6 4]]

[[2 8]
 [4 1]]

[[1 5]
 [2 6]]]

The Inverse of the given array(stack of matrices) =
[[[ 0.28571429 -0.07142857]
 [-0.42857143 0.35714286]]

[[-0.03333333 0.26666667]
 [ 0.13333333 -0.06666667]]

[[-1.5 1.25 ]
 [ 0.5 -0.25 ]]]