np.linalg.det – Python NumPy linalg.det() Function

np.linalg.det: In linear algebra, the determinant is a very useful value. The diagonal elements of a square matrix are being used to calculate it. It’s as simple as subtracting the product of the top left and bottom right elements from the product of the other two elements in a 2×2 matrix.

In other terms, the determinant of a matrix [[a,b], [c,d]] is computed as ‘ad-bc’. The larger square matrices are supposed to be made up of a combination of 2×2 matrices.

NumPy linalg.det() Function:

The determinant of an array is calculated using the linalg.det() function of the NumPy module.

Syntax:

numpy.linalg.det(array)

Parameters

array: This is required. It is an input array for which the determinant is to be calculated.

Return Value:

The determinant of an array is returned.

NumPy linalg.det() Function in Python

Example1:

Approach:

  • Import numpy module using the import keyword.
  • Pass some random list as an argument to the array() function to create an array.
  • Store it in a variable.
  • Print the above-given array.
  • Pass the given array as an argument to the linalg.det() function of numpy module to calculate the determinant of the given array.
  • Store it in another variable.
  • Print the determinant of the given array.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([[5,6],[4, 3]])
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
# Pass the given array as an argument to the linalg.det() function of numpy module 
# to calculate the determinant of the given array.
# Store it in another variable.
rslt_determnt = np.linalg.det(gvn_arry)
# Print the determinant of the given array.
print("The determinant of the given array =", rslt_determnt)

Output:

The above given array is:
[[5 6]
 [4 3]]
The determinant of the given array = -9.000000000000002

Example2:

The determinants of a stack of matrices can also be calculated using this function.

Approach:

  • Import numpy module using the import keyword.
  • Pass some random stack of matrices as the arguments to the array() function to create an array.
  • Store it in a variable.
  • Print the above-given array.
  • Pass the given array as an argument to the linalg.det() function of numpy module to calculate the determinant of the given array.
  • Store it in another variable.
  • Print the determinant of the given array(stack of matrices).
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random stack of matrices as the arguments to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([ [[5, 1], [6, 4]], [[2, 8], [4, 1]], [[1, 5], [2, 6]] ])
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
# Pass the given array as an argument to the linalg.det() function of numpy module 
# to calculate the determinant of the given array.
# Store it in another variable.
rslt_determnt = np.linalg.det(gvn_arry)
# Print the determinant of the given array(stack of matrices).
print("The determinant of the given array(stack of matrices) =", rslt_determnt)

Output:

The above given array is:
[[[5 1]
 [6 4]]

[[2 8]
 [4 1]]

[[1 5]
 [2 6]]]
The determinant of the given array(stack of matrices) = [ 14. -30. -4.]