Free cookie consent management tool by TermsFeed Finding the determinant of a matrix using NumPy | Pythontic.com

Finding the determinant of a matrix using NumPy

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:

# 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:

Matrix:
[[-4 -3 -2]
 [-1  1  1]
 [ 2  3  4]]
Determinant of the matrix:
-12.0

 


Copyright 2025 © pythontic.com