How to calculate the determinant of a square matrix with numpy in python ?


Examples of how to calculate the determinant of a square matrix with numpy in python:

Calculate the determinant of a matrix (method 1)

To calculate a determinant in python a solution is to use the numpy function called det(), example

>>> import numpy as np
>>> a = np.array(([-1,2],[-3,4]))
>>> np.linalg.det(a)
2.0000000000000004

Another example

>>> a = np.array(([-2,2,-3],[-1,1,3],[2,0,-1]))
>>> a
array([[-2,  2, -3],
       [-1,  1,  3],
       [ 2,  0, -1]])
>>> np.linalg.det(a)
17.999999999999996

Calculate the determinant of a matrix (method 2)

Note: Another solution using slogdet()

>>> sign, logdet = np.linalg.slogdet(a)
>>> np.exp(logdet)
2.0000000000000004

References