Python program to find the determinant of a Matrix

0/5 No votes

Report this app

Description

Today in this article you will see different ways to implement a Python program to find the determinant of an nxn matrix. We will start by explaining what is the determinant of a matrix and where it is used.

What is determinant of a Matrix?

The determinant of a matrix is a special number which is defined only for the square matrices i.e. the matrices which has same number of rows and columns. It allows to define some important properties of the matrix and the linear map represented by the matrix.

The determinant of a matrix lets say matrix A is denoted as det(A), det A or |A|.

Usage of determinant of a matrix

The determinant has a lot of usage some are –

  • To find the inverse of a matrix
  • In solving Linear equations
  • Used in solving problems in calculus

Compute the determinant of a matrix

The method to calculate the determinant of a matrix having differnt dimensions are given below.

Determinant of 2×2 matrix

The determinant for a 2×2 matrix is defined as –

2x2

Determinant of 3×3 matrix

The determinant of 3×3 matrix can be defined as –

determinant 3x3 matrix

Similary you can find the determinant of an nxn matrix.

Program in Python to find the determinant of a Matrix

There are multiple ways to implement a program in Python for the calculation of determinant of a matrix. So lets see –

Python program to find determinant using NumPy

In this we will use the linalg.det() method of NumPy library to find the determinant of a matrix.

# Using NumPy library to find the determinant of a matrix

import numpy

# Taking a 2x2 matrix
myMatrix= numpy.matrix('7 2; -3 0')
print("The matrix is:")
print(myMatrix)
# Computing determinant
d=numpy.linalg.det(myMatrix)
print("The determinant of the given matrix is:")
print(d)

On executing this program it will display the determinatnt of entered matrix.

determinant of matrix

Python program to find determinant using SymPy

SymPy is another opensource python library for symbolic computation. We will use det( ) method to find the determinant of a matrix.

But before you use it in your program first you need to download SymPy package on your system, for that use –

sudo pip3 install sympy

Once it get installed you can import this library in your program.

# Using sympy library to find the determinant of a matrix in Python

import sympy
# Take a 3x3 matrix
myMatrix=sympy.Matrix([[3 , 3, 2],[1, -2, 4],[8, 5, 7]])
print("The matrix is:")
print(myMatrix)
# Calculating determinant
d=myMatrix.det()
print("Determinant of a matrix is:")
print(d)

When you execute this program you will see the determinant of a 3×3 matrix that you have entered.

Python program to find determinant using PyMatrix

PyMatrix is a lightweight module in Python which supports a range of basic linear algebra operations. We will use det() function of this library to calculate the determinant of matrix.

First use the given command to install this library on your system –

sudo pip3 install pymatrix

Once it gets installed you can import it to your program.

# Using PyMatrix library to find the determinant of a matrix

import pymatrix
# Taking a 4x4 matrix
myMatrix=pymatrix.matrix([[1 , 2, 8, 3],[3, 2, 3, 4],[2, 0, 3, 8],[1, 6, 5, 3]])
print("The matrix is:")
print(myMatrix)
# Computing the determinant of the matrix
d=myMatrix.det()
print("The determinant of the given matrix is:")
print(d)

On executing this program you will see the determinant of 4×4 matrix that you have entered.

matrix

Python program to find determinant using SciPy

SciPy is a free and opensource Python library which used for scientific computing it contains modules for optimization, linear algebra, integration, interpolation, signal and image processing, etc.

We will use det() function of this library for computing determinant of a matrix.

Use the following command to install SciPy libaray package on your system.

sudo pip3 install scipy

Now you can import scipy to your program –

# Using SciPy library to find the determinant of a matrix

import numpy
from scipy import linalg
#Take a 3x3 matrix
myMatrix= numpy.matrix('1 2 8; 3 4 7; 0 0 0')
print("The matrix is:")
print(myMatrix)
# Compute determinant
d=linalg.det(myMatrix)
print("The determinant of the given matrix is:")
print(d)

When you execute this program it will display the determinant of the matrix that you have entered.

Conclusion

I hope this article i helpful to you now if you have a query then write us in the comments below.

Leave a Reply

Your email address will not be published. Required fields are marked *