Determinant of a matrix in Python
In this post, you will learn how to find the determinant of a 2x2 matrix and a 3x3 matrix in the Python programming language using the numpy module.
NumPy is the fundamental package for scientific computing with Python. It is a highly optimised library for numerical operations. The support of NumPy makes the task easier. It provides us with the feature to calculate the determinant of a square matrix using the numpy.linalg.det() function.
The determinant of a matrix is a number that is specially defined only for square matrices. Determinants are mathematical objects that are very useful in the analysis and solution of systems of linear equations. The determinant is useful for solving linear equations, capturing how linear transformation change area or volume. Determinants also have wide applications in engineering, science, social science, and economics as well.
The determinant of a matrix A is denoted det(A), det A, or |A|.
Python Program to find the determinant of a Matrix 2x2
In the given Python program, we are calculating the determinant of a 2X2 numpy matrix using the numpy.linalg.det() function.
# importing Numpy package
import numpy as np
# Creating a 2X2 numpy matrix
arr = np.array([[30, 12], [20, 11]])
# Displaying the Matrix
print("Numpy Matrix =")
print(arr)
# Calculating the determinant of matrix
determinant = np.linalg.det(arr)
print("\nThe Determinant of 2 * 2 Matrix =")
print(int(determinant))
Output of the above code:
Numpy Matrix =
[[30 12]
[20 11]]
The Determinant of 2 * 2 Matrix =
90
Python Program to find the determinant of a Matrix 3x3
In the given Python program, we are calculating the determinant of a 3X3 numpy matrix using the numpy.linalg.det() function.
# importing Numpy package
import numpy as np
# Creating a 3X3 numpy matrix
arr = np.array([[32, 22, 11],
[12, 5, 2],
[21, 32, 7]])
# Displaying the Matrix
print("Numpy Matrix =")
print(arr)
# Calculating the determinant of matrix
determinant = np.linalg.det(arr)
print("\nThe Determinant of 3 * 3 Matrix =")
print(int(determinant))
Output of the above code:
Numpy Matrix =
[[32 22 11]
[12 5 2]
[21 32 7]]
The Determinant of 3 * 3 Matrix =
1216
Related Articles
Transpose of a matrix in python
Python program to multiply two matrices
Matrix addition in Python
Python program to multiply two matrices
Python Numpy Array Shape
Python Pandas Dataframe to CSV
Inverse of a matrix in Python
Remove element from list Python
Python iterate list with index
Python program to sum all the numbers in a list
Python print without newline
2d arrays in Python
Python add list to list
Python convert xml to dict
Python dict inside list
Multiply all elements in list Python
Python heap implementation using heapq module
numpy dot product
Python convert dict to xml
Python weather api
Python raise keyword