NumPy - Determinant


Advertisements

Determinant is a very useful value in linear algebra. It calculated from the diagonal elements of a square matrix. For a 2x2 matrix, it is simply the subtraction of the product of the top left and bottom right element from the product of other two.

In other words, for a matrix [[a,b], [c,d]], the determinant is computed as ‘ad-bc’. The larger square matrices are considered to be a combination of 2x2 matrices.

The numpy.linalg.det() function calculates the determinant of the input matrix.

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

It will produce the following output −

-2.0

Example

import numpy as np 

b = np.array([[6,1,1], [4, -2, 5], [2,8,7]]) 
print b 
print np.linalg.det(b) 
print 6*(-2*7 - 5*8) - 1*(4*7 - 5*2) + 1*(4*8 - -2*2)

It will produce the following output −

[[ 6 1 1]
 [ 4 -2 5]
 [ 2 8 7]]

-306.0

-306
numpy_linear_algebra.htm
Advertisements