Open In App

Compute the determinant of a given square array using NumPy in Python

Improve
Improve
Like Article
Like
Save
Share
Report

In Python, the determinant of a square array can be easily calculated using the NumPy package. This package is used to perform mathematical calculations on single and multi-dimensional arrays. numpy.linalg is an important module of NumPy package which is used for linear algebra. 

We can use det() function of numpy.linalg module to find out the determinant of a square array.

Syntax: numpy.linalg.det(array)

Parameters:

array(…, M, M) array_like: Input array to calculate determinants for.

Returns:

det(…) array_like: Determinant of array.

Example 1: Determinant of 2X2 matrix.

Python3




# Importing libraries
import numpy as np
from numpy import linalg
  
# Creating a 2X2 matrix
matrix = np.array([[1, 0], [3, 6]])
print("Original 2-D matrix")
print(matrix)
  
# Output
print("Determinant of the 2-D matrix:")
print(np.linalg.det(matrix))


Output:

Original 2-D matrix
[[1 0]
 [3 6]]
Determinant of the 2-D matrix:
6.0

Example 2: Determinant of 3X3 matrix

Python3




# Importing libraries
import numpy as np
from numpy import linalg
  
# Creating a 3X3 matrix
matrix = np.array([[1, 0, 1], [1, 2, 0], [4, 6, 2]])
print("Original 3-D matrix")
print(matrix)
  
# Output
print("Determinant of the 3-D matrix:")
print(np.linalg.det(matrix))


Output:

Original 3-D matrix
[[1 0 1]
 [1 2 0]
 [4 6 2]]
Determinant of the 3-D matrix:
2.0

Example 3: Determinant of 4X4 matrix

Python3




# Importing libraries
import numpy as np
from numpy import linalg
  
# Creating a 4X4 matrix
matrix = np.array([[1, 0, 1, 8], [1, 2, 0, 3], [4, 6, 2, 6], [0, 3, 6, 4]])
print("Original 4-D matrix")
print(matrix)
  
# Output
print("Determinant of the 4-D matrix:")
print(np.linalg.det(matrix))


Output:

Original 4-D matrix
[[1 0 1 8]
 [1 2 0 3]
 [4 6 2 6]
 [0 3 6 4]]
Determinant of the 4-D matrix:
188.0


Last Updated : 29 Aug, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads