0
0
PythonProgramBeginner · 2 min read

Python Program to Find Determinant of Matrix

You can find the determinant of a matrix in Python using numpy.linalg.det(matrix) or by writing a recursive function that expands by minors to calculate it manually.
📋

Examples

Input[[4]]
Output4.0
Input[[1, 2], [3, 4]]
Output-2.0
Input[[6, 1, 1], [4, -2, 5], [2, 8, 7]]
Output-306.0
🧠

How to Think About It

To find the determinant, think of it as a special number that tells you about the matrix's properties. For a 1x1 matrix, it's just the single number. For bigger matrices, you break it down into smaller parts by removing one row and one column at a time, multiply those smaller determinants by the right numbers and signs, then add them all up.
📐

Algorithm

1
If the matrix is 1x1, return the single element as the determinant.
2
If the matrix is 2x2, calculate determinant as ad - bc.
3
For larger matrices, pick the first row and for each element:
4
Calculate the minor matrix by removing the current row and column.
5
Recursively find the determinant of the minor matrix.
6
Multiply the element by the determinant of the minor and the correct sign (+/-) and add to total.
7
Return the total as the determinant.
💻

Code

python
def determinant(matrix):
    size = len(matrix)
    if size == 1:
        return matrix[0][0]
    if size == 2:
        return matrix[0][0]*matrix[1][1] - matrix[0][1]*matrix[1][0]
    det = 0
    for c in range(size):
        minor = [row[:c] + row[c+1:] for row in matrix[1:]]
        det += ((-1) ** c) * matrix[0][c] * determinant(minor)
    return det

# Example usage
matrix = [[6, 1, 1], [4, -2, 5], [2, 8, 7]]
print(determinant(matrix))
Output
-306.0
🔍

Dry Run

Let's trace the determinant calculation for matrix [[1, 2], [3, 4]] through the code

1

Check matrix size

Matrix size is 2, so use formula ad - bc

2

Calculate determinant

det = 1*4 - 2*3 = 4 - 6 = -2

3

Return result

Return -2 as determinant

StepOperationValue
1Matrix size2
2Calculate 1*4 - 2*3-2
3Return determinant-2
💡

Why This Works

Step 1: Base cases for small matrices

For 1x1 and 2x2 matrices, the determinant is calculated directly using simple formulas to avoid unnecessary recursion.

Step 2: Recursive expansion by minors

For larger matrices, the determinant is found by expanding along the first row, calculating determinants of smaller 'minor' matrices recursively.

Step 3: Sign adjustment

Each minor's determinant is multiplied by (-1) raised to the column index to alternate signs, which is essential for correct calculation.

🔄

Alternative Approaches

Using numpy library
python
import numpy as np
matrix = np.array([[6, 1, 1], [4, -2, 5], [2, 8, 7]])
det = np.linalg.det(matrix)
print(round(det))
This method is faster and simpler but requires installing numpy.
Using LU decomposition (advanced)
python
import numpy as np
from scipy.linalg import lu
matrix = np.array([[6, 1, 1], [4, -2, 5], [2, 8, 7]])
P, L, U = lu(matrix)
det = np.prod(np.diag(U)) * np.linalg.det(P)
print(round(det))
This method is efficient for large matrices but needs scipy and more complex understanding.

Complexity: O(n!) time, O(n^2) space

Time Complexity

The recursive method expands minors for each element, leading to factorial time complexity as matrix size grows.

Space Complexity

Extra space is used to store minor matrices during recursion, roughly proportional to the square of matrix size.

Which Approach is Fastest?

Using numpy's built-in determinant function is fastest and most efficient for practical use, especially for large matrices.

ApproachTimeSpaceBest For
Recursive expansionO(n!)O(n^2)Learning and small matrices
Numpy.linalg.detO(n^3)O(n^2)Practical use and large matrices
LU decompositionO(n^3)O(n^2)Efficient large matrix computations
💡
Use numpy.linalg.det for quick and reliable determinant calculation in Python.
⚠️
Beginners often forget to alternate signs when expanding by minors, leading to incorrect results.