VOOZH about

URL: https://www.geeksforgeeks.org/dsa/determinant-of-a-matrix/

⇱ Program to find Determinant of a Matrix - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to find Determinant of a Matrix

Last Updated : 23 Jul, 2025

The determinant of a Matrix is defined as a special number that is defined only for square matrices (matrices that have the same number of rows and columns). A determinant is used in many places in calculus and other matrices related to algebra, it actually represents the matrix in terms of a real number which can be used in solving a system of a linear equation and finding the inverse of a matrix.

Determinant of 2 x 2 Matrix:

👁 1
determinant of 2 x 2 matrix

Determinant of 3 x 3 Matrix:

👁 2
determinant of 3 x 3 matrix

How to calculate?

The value of the determinant of a matrix can be calculated by the following procedure: 

  • For each element of the first row or first column get the cofactor of those elements.
  • Then multiply the element with the determinant of the corresponding cofactor. 
  • Finally, add them with alternate signs. As a base case, the value of the determinant of a 1*1 matrix is the single value itself. 

The cofactor of an element is a matrix that we can get by removing the row and column of that element from that matrix.


Output
30

Time Complexity: O(n3)
Space Complexity: O(n2), Auxiliary space used for storing cofactors.

Note: In the above recursive approach when the size of the matrix is large it consumes more stack size.

Determinant of a Matrix using Determinant properties

We calculates the determinant of an N x N matrix using Gaussian elimination and a series of transformations that reduce the matrix to upper triangular form.

  • Converting the given matrix into an upper triangular matrix using determinant properties 
  • The determinant of the upper triangular matrix is the product of all diagonal elements. 
  • Iterating every diagonal element and making all the elements down the diagonal as zero using determinant properties 
  • If the diagonal element is zero then search for the next non-zero element in the same column.

There exist two cases:

  • Case 1: If there is no non-zero element. In this case, the determinant of a matrix is zero 
  • Case 2: If there exists a non-zero element there exist two cases 
    • Case A: If the index is with a respective diagonal row element. Using the determinant properties make all the column elements down to it zero
    • Case B: Swap the row with respect to the diagonal element column and continue the Case A operation.

Output
30

Time complexity: O(n3
Auxiliary Space: O(n), Space used for storing row.

Determinant of a Matrix using NumPy package in Python

There is a built-in function or method in linalg module of NumPy package in python. It can be called numpy.linalg.det(mat) which returns the determinant value of the matrix mat passed in the argument.


Comment
Article Tags: