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
import numpy as np
from numpy import linalg
matrix = np.array([[1, 0], [3, 6]])
print("Original 2-D matrix")
print(matrix)
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
import numpy as np
from numpy import linalg
matrix = np.array([[1, 0, 1], [1, 2, 0], [4, 6, 2]])
print("Original 3-D matrix")
print(matrix)
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
import numpy as np
from numpy import linalg
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)
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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
29 Aug, 2020
Like Article
Save Article