NumPy linalg.det – Compute the determinant of the given array

Linalg Det Cover Image

Hello and welcome to this tutorial on Numpy linalg.det. In this tutorial, we will be learning about the NumPy linalg.det() method and also seeing a lot of examples regarding the same. So let us begin!

Also check: Numpy linalg.eig – Compute the eigenvalues and right eigenvectors of a square array


What is numpy.linalg.det?

The numpy.linalg.det() method in NumPy is used to compute the determinant of a given square matrix.

If we have a 2×2 matrix of the form:

2 D Array
2×2 Array

Its determinant is calculated as:

2x2 Array Determinant
2×2 Array Determinant

For a 3×3 matrix like

3x3 Array
3×3 Array

The determinant is computed as:

3x3 Array Determinant
3×3 Array Determinant

Similarly, we can calculate the determinant of higher-order arrays.


Syntax of NumPy linalg.det

numpy.linalg.det(a)
  • Parameter: a, an MxM array. Input array to compute the determinant of.
  • Returns: Determinant of a.

Examples of NumPy linalg.det

Let’s look at some examples of the NumPy linalg.det function to learn how it works.

1. Using NumPy linalg.det on a 2×2 array

import numpy as np

arr = [[2, 1], [3, 5]]
# using np.linalg.det() method to compute the determinant
print("array = ", arr)
print("Determinant of array = ", np.linalg.det(arr))
det = 5*2 - 3*1
print("Determinant of array using manual calculation = ", det)

Output:

array =  [[2, 1], [3, 5]]
Determinant of array =  6.999999999999999
Determinant of array using manual calculation =  7

Using NumPy linalg.det on a 2×2 array with negative numbers

import numpy as np

arr = [[-5, 2], [-4, 8]]
# using np.linalg.det() method to compute the determinant
print("array = ", arr)
print("Determinant of array = ", np.linalg.det(arr))
det = (-5*8) - (-4*2)
print("Determinant of array using manual calculation = ", det)

Output:

array =  [[-5, 2], [-4, 8]]
Determinant of array =  -32.0
Determinant of array using manual calculation =  -32

NumPy linalg.det calculation for a 3×3 array

import numpy as np

arr = [[2, 1, 3], [5, 3, 4], [1, 0, 1]]
# using np.linalg.det() method to compute the determinant
print("array = ", arr)
print("Determinant of array = ", np.linalg.det(arr))
det = 2*(3*1 -0*4) -1*(5*1 - 1*4) + 3*(5*0 - 3*1)
print("Determinant of array using manual calculation = ", det)

Output:

array =  [[2, 1, 3], [5, 3, 4], [1, 0, 1]]
Determinant of array =  -4.0
Determinant of array using manual calculation =  -4

In all the above examples, we have used the numpy.linalg.det method to compute the determinant as well as calculated the determinant using the manual method i.e. through the formulas discussed above. Through this, we can conclude that both methods return the same answer.


Conclusion

That’s all! In this tutorial, we learned about the Numpy linalg.det method and practiced different types of examples using the same.  

If you want to learn more about NumPy, feel free to go through our NumPy tutorials.


Reference