To compute the value of a determinant in Python using the numerical package NumPy, use the np.linalg.det() function. But what is the determinant of a Matrix: It is calculated from the subtraction of the product of the two diagonal elements (left diagonal – right diagonal).
np.linalg.det
The np.linalg.det() is a numpy library function used to determine a square matrix’s determinant. The np.linalg.det() function takes an array as an argument and returns the determinant of the given array.
For example, if we have matrix of 2×2 [ [1, 2], [2, 4]] then answer will be (4*1)-(2*2) = 0.
The determinant is an instrumental value in linear algebra. It is calculated from the diagonal items of a square matrix. For a 2×2 matrix, it is merely the subtraction of the product of the top left and bottom right item from the product of the other two.
Syntax
numpy.linalg.det(array)
The np.linalg.det() function takes only one argument as a parameter, the array name.
Return Value
The np.linalg.det() function returns the determinant of the given array. The return value will be in the float data type.
Programming Example
Program to show working of linalg.det() in 2×2 Matrix:
# Importing numpy import numpy as np # This will create a 2D array of shape 2x2 with values 5 to 8 arr = np.arange(5, 9).reshape(2, 2) print("The array is:\n", arr) print("Shape of the array is : ", np.shape(arr)) # Now we will print determinant using det() function print("Determinant of the given array: ", np.linalg.det(arr)) # Verify with the manual caculation detr = (5*8)-(7*6) print("Determinant using manual method: ", detr)
Output
The array is: [[5 6] [7 8]] Shape of the array is : (2, 2) Determinant of the given array: -2.000000000000005 Determinant using manual method: -2
Explanation:
In this example, we first made one array of shapes 2×2, and we printed that. Then we have called numpy.linalg.det() function to calculate the determinant of the given array.
Then we calculated the determinant manually. We can see that both values are almost the same.
Program to show the working of linalg.det() in 3×3 Matrix:
See the following code.
# Importing numpy import numpy as np # This will create a 2D array of shape 3x3 with values 1 to 9 arr = np.arange(1, 10).reshape(3, 3) print("The array is:\n", arr) print("Shape of the array is : ", np.shape(arr)) # Now we will print determinant using det() function print("Determinant of the given array: ", np.linalg.det(arr)) # Verify with the manual caculation detr = 1*(5*9 - 6*8) + 2*(4*9 - 6*7) - 3*(4*8 - 5*7) print("Determinant using manual method: ", detr)
Output
The array is: [[1 2 3] [4 5 6] [7 8 9]] Shape of the array is : (3, 3) Determinant of the given array: 0.0 Determinant using manual method: -6
Explanation
In this example, we first made one array of shapes 3×3, and we printed that. Then we have called numpy.linalg.det() function to calculate the determinant of the given array. Then we calculated the determinant manually. We can see that both values are almost the same.
Conclusion
In other words, for a matrix [[w,x], [c,d]], the determinant is computed as ‘ad-bc’. The larger square matrices are considered to be a combination of 2×2 matrices. The numpy.linalg.det() function calculates the determinant of an input matrix.
That’s it.