Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Compute the determinant of an array in linear algebra in Python
The determinant is a scalar value that provides important information about a square matrix in linear algebra. In Python NumPy, we use np.linalg.det() to compute the determinant of an array.
Syntax
numpy.linalg.det(a)
Parameters:
- a ? Input array (must be square matrix)
Returns: The determinant of the input array as a scalar value.
Basic Example
Let's compute the determinant of a 2x2 matrix ?
import numpy as np
# Create a 2x2 array
arr = np.array([[5, 10], [12, 18]])
print("Array:")
print(arr)
# Compute the determinant
det = np.linalg.det(arr)
print(f"\nDeterminant: {det}")
Array: [[ 5 10] [12 18]] Determinant: -30.000000000000014
3x3 Matrix Example
Computing determinant for a larger matrix ?
import numpy as np
# Create a 3x3 array
matrix_3x3 = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
print("3x3 Matrix:")
print(matrix_3x3)
det_3x3 = np.linalg.det(matrix_3x3)
print(f"\nDeterminant: {det_3x3}")
3x3 Matrix: [[1 2 3] [4 5 6] [7 8 9]] Determinant: 0.0
Identity Matrix
The determinant of an identity matrix is always 1 ?
import numpy as np
# Create identity matrix
identity = np.eye(3)
print("Identity Matrix:")
print(identity)
det_identity = np.linalg.det(identity)
print(f"\nDeterminant: {det_identity}")
Identity Matrix: [[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]] Determinant: 1.0
Key Points
- Determinant is only defined for square matrices
- If determinant is 0, the matrix is singular (not invertible)
- For 2x2 matrix [[a,b],[c,d]], determinant = ad - bc
- Result may have small floating-point errors due to numerical computation
Conclusion
Use np.linalg.det() to compute matrix determinants in Python. A zero determinant indicates the matrix is singular, while non-zero values indicate the matrix is invertible.
Advertisements
