Finding the determinant of a matrix using NumPy

The det() function of numpy.linalg module finds the determinant of a matrix using LU decomposition method. It is found by multiplying the diagonal elements of the triangular matrices.

Linear Algebra Updated 12 Apr 2025 1 min read

Overview

  • The NumPy function det() from the linalg module finds the determinant of a given matrix using the LU decomposition or the LU factorization method.
  • Using LU decomposition the determinant of a matrix can be found by factoring the matrix A into a lower triangular matrix and upper triangular matrix and taking the product of the diagonal elements of both the matrices.
  • The determinant is only defined for square matrices which are singular. i.e.,square matrices that have an inverse.

Example

python
# Example Python program that finds the determinant

            # of a square matrix using det() function of the

            # linalg module

            import numpy

# Create a square matrix of order 3x3

            elems     = numpy.arange(-4, 5)

            matrix  = elems.reshape(3, 3)

# Make the matrix non-singular

            # Avoid getting a determinant value of zero

            matrix[1][1] = 1

determinant = numpy.linalg.det(matrix)

print("Matrix:")

            print(matrix)

print("Determinant of the matrix:")

            print(determinant)

Output

Output
Matrix:

            [[-4 -3 -2]

             [-1  1  1]

             [ 2  3  4]]

            Determinant of the matrix:

            -12.0