Inverse of a 3x3 matrix in python

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    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 matrix. The inverse of a matrix exists only if the matrix is non-singular i.e., determinant should not be 0. Using determinant and adjoint, we can easily find the inverse of a square matrix using below formula,

    if det(A) != 0
        A-1 = adj(A)/det(A)
    else
        "Inverse doesn't exist"  
    

    Matrix Equation

    Inverse of a 3x3 matrix in python

    where,

    A-1: The inverse of matrix A

    x: The unknown variable column

    B: The solution matrix

    We can find out the inverse of any square matrix with the function numpy.linalg.inv(array). 

    Syntax: numpy.linalg.inv(a)

    Parameters:

    a: Matrix to be inverted

    Returns: Inverse of the matrix a.

    Example 1:

    Python3

    import numpy as np

    arr = np.array([[1, 2], [5, 6]])

    inverse_array = np.linalg.inv(arr)

    print("Inverse array is ")

    print(inverse_array)

    print()

    arr = np.array([[1, 2, 3], 

                    [4, 9, 6], 

                    [7, 8, 9]])

    inverse_array = np.linalg.inv(arr)

    print("Inverse array is ")

    print(inverse_array)

    print()

    arr = np.array([[1, 2, 3, 4], 

                    [10, 11, 14, 25],

                    [20, 8, 7, 55], 

                    [40, 41, 42, 43]])

    inverse_array = np.linalg.inv(arr)

    print("Inverse array is ")

    print(inverse_array)

    print()

    arr = np.array([[1]])

    inverse_array = np.linalg.inv(arr)

    print("Inverse array is ")

    print(inverse_array)

    Output:

    Inverse array is 
    [[-1.5   0.5 ]
     [ 1.25 -0.25]]
    
    Inverse array is 
    [[-0.6875     -0.125       0.3125    ]
     [-0.125       0.25       -0.125     ]
     [ 0.64583333 -0.125      -0.02083333]]
    
    Inverse array is 
    [[-15.07692308   4.9         -0.8         -0.42307692]
     [ 32.48717949 -10.9          1.8          1.01282051]
     [-20.84615385   7.1         -1.2         -0.65384615]
     [  3.41025641  -1.1          0.2          0.08974359]]
    
    Inverse array is 
    [[1.]]
    

    Example 2:

    Python3

    import numpy as np 

    A = np.array([[[1., 2.], [3., 4.]], 

                  [[1, 3], [3, 5]]]) 

    print(np.linalg.inv(A))

    Output:

    [[[-2.    1.  ]
      [ 1.5  -0.5 ]]
    
     [[-1.25  0.75]
      [ 0.75 -0.25]]]
    

    How do you define an inverse of a matrix in Python?

    Python provides a very easy method to calculate the inverse of a matrix. The function numpy. linalg. inv() which is available in the python NumPy module is used to compute the inverse of a matrix.

    How do you do inverse in Python?

    Inverse of Matrix in Python.
    Use the numpy.linalg.inv() Function to Find the Inverse of a Matrix in Python..
    Use the numpy.matrix Class to Find the Inverse of a Matrix in Python..
    Use the scipy.linalg.inv() Function to Find the Inverse of a Matrix in Python..

    How do you invert a NumPy matrix in Python?

    We use numpy. linalg. inv() function to calculate the inverse of a matrix. The inverse of a matrix is such that if it is multiplied by the original matrix, it results in identity matrix.