NumPy: Determinant of a Matrix
In this tutorial, we will learn how to compute the value of a determinant in Python using its numerical package NumPy's numpy.linalg.det()
function.
We consider a couple of homogeneous linear equations in two variables $x$ and $y$
$$ a_{1}x + b_{1}y = 0 \\ a_{2}x + b_{2}y = 0 $$
Multiplying the first equation by $b_{2}$ and the second by $b_{1}$ we get
$$ a_{1}b_{2}x + b_{1}b_{2}y = 0 \\ a_{2}b_{1}x + b_{2}b_{1}y = 0 $$
Subtracting the second equation from the first, we get
$$ a_{1}b_{2}x - a_{2}b_{1}x = 0 $$
Dividing by $x$, the equation becomes
$$ a_{1}b_{2} - a_{2}b_{1} = 0 $$
This result is often denoted as
$$ \begin{vmatrix} a_{1} & b_{1} \\ a_{2} & b_{2} \\ \end{vmatrix} = 0 $$
and the expression on the left is known as the determinant. As you can see, it results to a single number.
The above determinant consists of two rows and two columns, and on expansion each of its term is the product of two quantities. This determinant is thus said to be of the second order. The quantities $a_{1}$, $b_{1}$, $a_{2}$ and $b_{2}$ are known as constituents of the determinant and the product terms $a_{1}b_{2}$ and $a_{2}b_{1}$ are called elements.
Python has a numerical library called NumPy which has a function called numpy.linalg.det()
to compute the value of a determinant.
We will compute the value of the second order determinant below in NumPy
$$ \begin{vmatrix} 1 & 2 \\ 3 & 4 \\ \end{vmatrix} $$
import numpy as np
a = np.array([[1, 2], [3, 4]])
d = np.linalg.det(a)
print(d)
On running the Python script, we get the value
-2.0
Determinants of the Third Order
We now consider a set of homogenous linear equations in three variables $x$, $y$ and $z$.
$$ a_{1}x + b_{1}y + c_{1}z = 0 \\ a_{2}x + b_{2}y + c_{2}z = 0 \\ a_{3}x + b_{3}y + c_{3}z = 0 $$
By eliminating $x$, $y$ and $z$, we get
$$ a_{1}(b_{2}c_{3} - b_{3}c_{2}) + b_{1}(c_{2}a_{3} - c_{3}a_{2}) + c_{1}(a_{2}b_{3} - a_{3}b_{2}) = 0 $$
or
$$ a_{1} \begin{vmatrix} b_{2} & c_{2} \\ b_{3} & c_{3} \\ \end{vmatrix} + b_{1} \begin{vmatrix} c_{2} & a_{2} \\ c_{3} & a_{3} \\ \end{vmatrix} + c_{1} \begin{vmatrix} a_{2} & b_{2} \\ a_{3} & b_{3} \\ \end{vmatrix} = 0 $$
This eliminant is often written as
$$ \begin{vmatrix} a_{1} & b_{1} & c_{1} \\ a_{2} & b_{2} & c_{2} \\ a_{3} & b_{3} & c_{3} \end{vmatrix} = 0 $$
and the expression on the left consisting of three rows and three columns is the determinant of third order.
Below we pick a third order determinant from the classic Algebra text Higher Algebra1 by Hall & Knight
$$ \begin{vmatrix} 67 & 19 & 21 \\ 39 & 13 & 14 \\ 81 & 24 & 26 \end{vmatrix} $$
and evaluate its value using NumPy's numpy.linalg.det()
function
import numpy as np
a = np.array([[67, 19, 21], [39, 13, 14], [81,24,26]])
d = np.linalg.det(a)
print(d)
Executing the above script, we get the value
-43.0
Notes
- 1) H. S. Hall & S. R. Knight, Higher Algebra. London: Macmillan & Co., Ltd., 1891. Chapter XXXIII: Determinants, p. 414, Ex. 2