Calculate The Determinant Of A Matrix

Goal

This post aims to show how to calculate the determinant of a matrix using numpy i.e., $|A|$

For example, if we have

$$A = \begin{bmatrix} a & b \\ c & d \end{bmatrix} $$

Then, $|A|$ is defined as

$$|A| = \begin{vmatrix} a & b \\ c & d \end{vmatrix} = ab - bc $$

Reference:

Libraries

In [3]:
import numpy as np

Create a matrix

In [6]:
a = np.array([[1,2], [3,4]]) 
a
Out[6]:
array([[1, 2],
       [3, 4]])

Calculate the determinant

In [7]:
print(np.linalg.det(a))
-2.0000000000000004
In [8]:
a[0, 0] * a[1, 1] - a[0, 1] * a[1, 0]
Out[8]:
-2

Comments

Comments powered by Disqus