![](https://i0.wp.com/media.go2speed.org/brand/files/grammarly/3/20150114062043-0102correctinginstantgrammarchecker728x90.png?resize=728%2C90&ssl=1)
- What is the Determinant of a Matrix
- Multiplication of the Determinants
- Properties of Determinants
- Determinant of a Matrix Using Python
What is the Determinant of a Matrix
The determinant of a matrix is a scalar value calculated from the elements of a Square Matrix (matrix with \(m = n\)). The determinant of a matrix \(A\) is denoted as \(det(A)\), \(det A\) or \(|A|\).
$$\begin{aligned}
A = \begin{bmatrix}
a_{11} & a_{12} & a_{13} \\
a_{21} & a_{22} & a_{23} \\
a_{31} & a_{32} & a_{33}
\end{bmatrix}\hspace{2em}
|A| = \begin{vmatrix}
a_{11} & a_{12} & a_{13} \\
a_{21} & a_{22} & a_{23} \\
a_{31} & a_{32} & a_{33}
\end{vmatrix}
\end{aligned}$$
Specific properties of the determinants make them useful for different applications like solving the linear system of equations, checking the invertibility of a matrix, finding the area and volume of geometric shapes, and so on.
The determinant of a \(2^{nd}\) order square matrix is represented and evaluated as
$$\begin{aligned}
\begin{vmatrix}
a & b \\
c & d
\end{vmatrix}
= ad – bc
\end{aligned}$$
Minors and Cofactors of Matrix elements
A minor of the matrix element is evaluated by taking the determinant of a submatrix created by deleting the elements in the same row and column as that element. A minor of the element \(a_{ij}\) is denoted as \(M_{ij}\).
The minors of \(a_{12}\) and \(a_{23}\) are denoted as \(M_{12}\) and \(M_{23}\), respectively, and are evaluated as:
$$\begin{aligned}
M_{12} =
\begin{vmatrix}
a_{21} & a_{23} \\
a_{31} & a_{33}
\end{vmatrix}
= (a_{21}a_{33}-a_{23}a_{31})\\[1.5em]
M_{23} =
\begin{vmatrix}
a_{11} & a_{31} \\
a_{12} & a_{32}
\end{vmatrix}
= (a_{11}a_{32}-a_{31}a_{12})
\end{aligned}$$
In general, the determinant formed by any \(m\) rows and \(m\) columns by deleting all the other elements is the minor of order \(m\).
The matrix comprising of all the minors of the given matrix is called the Minor Matrix.
The cofactor of an element is obtained by giving an appropriate sign to the minor of that element. The sign for a particular cofactor at \(i^{th}\) row and \(j^{th}\) column is obtained by evaluating \((-1)^{i+j}\).
The corresponding capital letter denotes the cofactor of an element. For example, cofactors of \(a_{12}\) and \(a_{23}\) are denoted as \(A_{12}\) and \(A_{23}\), respectively, and are evaluated as
$$\begin{aligned}
A_{12} =
(-1)^{1+2}
\begin{vmatrix}
a_{21} & a_{23} \\
a_{31} & a_{33}
\end{vmatrix}
= -(a_{21}a_{33}-a_{23}a_{31})\\[1.5em]
A_{23} = (-1)^{2+3}
\begin{vmatrix}
a_{11} & a_{31} \\
a_{12} & a_{32}
\end{vmatrix}
= -(a_{11}a_{32}-a_{31}a_{12})
\end{aligned}$$
The matrix created by taking the cofactors of all the elements of the matrix is called the Cofactor Matrix, denoted as \(C\) and the transpose (interchanging rows with columns) of the cofactor matrix is called the Adjugate Matrix or Adjoint Matrix, denoted as \(C^T\) or \(Adj.\, A\).
The sign pattern for converting a \(3^{rd}\) order minor matrix to the cofactor matrix is:
$$\begin{aligned}
\begin{bmatrix}
+ & – & +\\
– & + & -\\
+ & – & +
\end{bmatrix}
\end{aligned}$$
The 3rd order determinant is represented as:
$$\begin{aligned}
|A| =
\begin{vmatrix}
a_{11} & a_{12} & a_{13} \\
a_{21} & a_{22} & a_{23} \\
a_{31} & a_{32} & a_{33}
\end{vmatrix}
\end{aligned}$$
We can use the Laplace’s Expansion to calculate the higher-order determinants. We can expand the determinant in terms of any particular row or column by multiplying the elements of the selected row or column by their cofactors and then adding up these multiplications.
The expansion of determinant \(|A|\) in terms of the first row is:
$$\begin{aligned}
|A| &=
a_{11}A_{11} + a_{12}A_{12} + a_{13}A_{13}\\[0.5em]
&= a_{11}
\begin{vmatrix}
a_{22} & a_{23} \\
a_{32} & a_{33}
\end{vmatrix}
– a_{12}
\begin{vmatrix}
a_{21} & a_{23} \\
a_{31} & a_{33}
\end{vmatrix}
+ a_{13}
\begin{vmatrix}
a_{21} & a_{22} \\
a_{31} & a_{32}
\end{vmatrix}
\end{aligned}$$
Similarly, we can expand the determinant \(|A|\) in terms of the second column as:
$$\begin{aligned}
|A| &=
a_{12}A_{12} + a_{22}A_{22} + a_{32}A_{32}\\[0.5em]
&= -a_{12}
\begin{vmatrix}
a_{21} & a_{23} \\
a_{31} & a_{33}
\end{vmatrix}
+ a_{22}
\begin{vmatrix}
a_{11} & a_{13} \\
a_{31} & a_{33}
\end{vmatrix}
– a_{32}
\begin{vmatrix}
a_{11} & a_{13} \\
a_{21} & a_{23}
\end{vmatrix}
\end{aligned}$$
Note that the determinant calculated using an expansion in terms of any row or column is the same.
The trick for reducing the computation effort while manually calculating the determinant is to select the row or column having the maximum number of zeros.
Let’s see a couple of examples to better understand the concepts of the determinant and the cofactors. In the first example, we will use the expansion in terms of the second column.
$$\begin{aligned}
\begin{vmatrix}
1 & 3 & 5 \\
2 & 0 & 4 \\
4 & 2 & 7
\end{vmatrix}
&= -3
\begin{vmatrix}
2 & 4 \\
4 & 7
\end{vmatrix}
+ 0 – 2
\begin{vmatrix}
1 & 5 \\
2 & 4
\end{vmatrix}\\[0.3em]
&= -3(2\times7-4\times4)-2(1\times4-5\times2)\\[0.5em]
&= -3(14-16)-2(4-10)\\[0.5em]
&= 18
\end{aligned}$$
For the second example, we will expand the determinant in terms of the first row.
$$\begin{aligned}
\begin{vmatrix}
5 & 3 & 58 \\
-4 & 23 & 11 \\
34 & 2 & -67
\end{vmatrix}
&= 5
\begin{vmatrix}
23 & 11 \\
2 & -67
\end{vmatrix}
– 3
\begin{vmatrix}
-4 & 11 \\
34 & -67
\end{vmatrix}
+ 58
\begin{vmatrix}
-4 & 23 \\
34 & 2
\end{vmatrix}\\[0.3em]
&= 5\big[23\times(-67)-11\times2\big]-3\big[(-4)\times(-67)-11\times34\big]\\
&\hspace{1cm}+58\big[(-4)\times2-23\times34\big]\\[0.5em]
&= 5(-1541-22)-3(268-374)+58(-8-782)\\[0.5em]
&= -53317
\end{aligned}$$
Shortcut Method for the Determinant
There is an alternate (so called shortcut) method to calculate the determinant of the \(3^{rd}\) order determinant. We can quickly calculate the determinant with this method.
In this method, we place the first two columns of the determinant on the right side of the determinant and add the products of the elements of three diagonals from top-left to bottom-right. Next, we subtract the products of the elements of three diagonals from top-right to bottom-left.
$$
\begin{aligned}
|A|&=
\begin{vmatrix}
a & b & c \\
d & e & f \\
g & h & i
\end{vmatrix}\\[1em]
&= aei + bfg + cdh – ceg – afh – bdi
\end{aligned}
$$
Let’s take the previous example so that you can compare the time required for both the methods and see if this is indeed a shortcut method.
$$
\begin{aligned}
&\hspace{1em}\begin{array}{|ccc|cc}
1 & 3 & 5 & 1 & 3\\
2 & 0 & 4 & 2 & 0\\
4 & 2 & 7 & 4 & 2
\end{array}\\[1.2em]
&= (1\cdot0\cdot7) + (3\cdot4\cdot4) + (5\cdot2\cdot2)\\
&\hspace{1.5em} – (5\cdot0\cdot4) – (1\cdot4\cdot2) – (3\cdot2\cdot7)\\
&= 0 + 48 + 20-0-8-42\\
&=18
\end{aligned}
$$
In general, we can represent the \(n^{th}\) order determinant as
$$\begin{aligned}
\begin{vmatrix}
a_{11} & a_{12} & \dots & a_{1n} \\
a_{21} & a_{22} & \dots & a_{2n} \\
\vdots & \vdots & \ddots & \vdots\\
a_{n1} & a_{n2} & \dots &a_{nn}
\end{vmatrix}
\end{aligned}$$
We can use the Laplace’s expansion for \(n^{th}\) order determinant in a similar way as the 3rd order determinant. We should further expand the cofactors in the first expansion until the second-order (2 x 2) cofactor is reached.
Let’s take one example of the 4th order determinant. We will first expand the determinant in terms of the second column as it has two zeros.
$$\begin{aligned}
\begin{vmatrix}
2 & 1 & 3 & 0 \\
1 & 0 & 2 & 3 \\
3 & 2 & 0 & 1 \\
2 & 0 & 1 & 3
\end{vmatrix}
&= -1
\begin{vmatrix}
1 & 2 & 3\\
3 & 0 & 1\\
2 & 1 & 3
\end{vmatrix}
+ 0 – 2
\begin{vmatrix}
2 & 3 & 0\\
1 & 2 & 3\\
2 & 1 & 3
\end{vmatrix}
+ 0\\
&\hspace{0.5cm}(Expand\, by\, Col.\, 2)\hspace{0.2cm}(Expand\, by\, Row\, 1)\\[0.5em]
&= -1\bigg(-2
\begin{vmatrix}
3 & 1 \\
2 & 3
\end{vmatrix}
+0 -1
\begin{vmatrix}
1 & 3 \\
3 & 1
\end{vmatrix}
\bigg) \\
&\hspace{0.5cm} -2\bigg(2
\begin{vmatrix}
2 & 3 \\
1 & 3
\end{vmatrix}
-3
\begin{vmatrix}
1 & 3 \\
2 & 3
\end{vmatrix}
+0\bigg)\\[0.3em]
&= -1\big[-2(3\times3-1\times2)-1(1\times1-3\times3)\big]\\
&\hspace{0.5cm}-2\big[2(2\times3-3\times1)-3(1\times3-3\times2)\big]\\[0.5em]
&= -1\big[(-2)\times7-1\times(-8)\big]-2\big[2\times3-3\times(-3)\big]\\[0.5em]
&= -1(-14+8)-2(6+9)\\[0.5em]
&= -24
\end{aligned}$$
Singular Matrix
When the determinant of a matrix is zero, i.e., \(|A|=0\), then that matrix is called as a Singular Matrix.
$$\begin{aligned}
|A&=
\begin{vmatrix}
8 & -6 & 2 \\
-6 & 7 & -4 \\
2 & -4 & 3
\end{vmatrix} \\[1em]
&= 8
\begin{vmatrix}
7 & -4 \\
-4 & 3
\end{vmatrix}
– (-6)
\begin{vmatrix}
-6 & -4 \\
2 & 3
\end{vmatrix}
+ 2
\begin{vmatrix}
-6 & 7 \\
2 & -4
\end{vmatrix}\\[1em]
&= 8\Big[7\times3-(-4)\times(-4)\Big]-(-6)\Big[(-6)\times3-(-4)\times2\Big]\\ &\hspace{2em} +2\Big[(-6)\times(-4)-7\times2\Big]\\[0.5em]
&= 8(21-16)+6(-18+8)+2(24-14)\\[0.5em]
&= 0
\end{aligned}$$
The matrix with a non-zero determinant is called the Non-singular Matrix.
All the singular matrices are Non-invertible Matrices, i.e., it is not possible to take an inverse of a matrix.
Multiplication of the Determinants
The product of two \(n^{th}\) order determinants is also a determinant of the order \(n\).
$$\begin{aligned}
|A|&=
\begin{vmatrix}
a & b & c \\
d & e & f \\
g & h & i
\end{vmatrix}\\[0.5em]
|B|&=
\begin{vmatrix}
l & m & n \\
p & q & r \\
x & y & z
\end{vmatrix}\\[0.5em]
|A|\times|B| &=
\begin{vmatrix}
al+bm+cn & ap+bq+cr & ax+by+cz \\
dl+em+fn & dp+eq+fr & dx+ey+fz \\
gl+hm+in & gp+hq+ir & gx+hy+iz
\end{vmatrix}\\[0.5em]
\end{aligned}$$
Properties of Determinants
The determinants have specific properties, which simplify the determinant. These properties also allow us to sometimes evaluate the determinant without the expansion.
The rows and columns of the matrix are collectively called lines.
- Interchanging the rows with columns (transpose of a matrix) does not alter the value of the determinant.$$\begin{aligned}
|A|&= \begin{vmatrix}
a & b & c \\
d & e & f \\
g & h & i
\end{vmatrix} \\[0.5em]
|A^{T}|&=
\begin{vmatrix}
a & d & g \\
b & e & h \\
c & f & i
\end{vmatrix}\\[0.5em]
\implies |A^{T}|&=|A|
\end{aligned}$$ - Interchanging the parallel lines (rows or columns) preserves the numerical value of determinant but the sign is changed.$$\begin{aligned}
|A|&=
\begin{vmatrix}
a & b & c \\
d & e & f \\
g & h & i
\end{vmatrix} \\[0.5em]
|A’|&=
\begin{vmatrix}
d & e & f \\
a & b & c \\
g & h & i
\end{vmatrix}\\[0.5em]
\implies |A’|&=-|A|
\end{aligned}$$Corollary: If the line is shifted by two places, i.e., it is passed over two lines then the sign of determinant remains the same.
$$\begin{aligned}
|A|&=
\begin{vmatrix}
a & b & c \\
d & e & f \\
g & h & i
\end{vmatrix}\\[0.5em]
|A’|&=
\begin{vmatrix}
d & e & f \\
g & h & i \\
a & b & c
\end{vmatrix}\\[0.5em]
\implies |A’|&=(-1)^2|A|\\[0.5em]
\implies |A’|&=|A|
\end{aligned}$$This implies that, in general, if the line is shifted by \(k\) places, then the determinant of the resulting matrix is
$$\begin{aligned}
|A’|&=(-1)^k|A|
\end{aligned}$$ - If two parallel lines (rows or columns) are the same, then the determinant of such matrix is zero.$$\begin{aligned}
|A|=
\begin{vmatrix}
a & b & c \\
a & b & c \\
g & h & i
\end{vmatrix}
\end{aligned}$$Using 2nd property, we can say that, if we shift the first row by one place or pass it over the second row, the determinant remains the same but the sign of the value of the determinant changes, i.e.,
$$\begin{aligned}
|A|&=-|A|\\[0.5em]
\implies 2|A|&=0\\[0.5em]
\implies |A|&=0
\end{aligned}$$ - If the line of a determinant is multiplied by a constant value, then the resulting determinant can be evaluated by multiplying the original determinant by the same constant value.$$\begin{aligned}
|A|&=
\begin{vmatrix}
a & b & c \\
d & e & f \\
g & h & i
\end{vmatrix}\\[0.5em]
|A’|&=
\begin{vmatrix}
a & b & c \\
pd & pe & pf\\
g & h & i
\end{vmatrix}\\[0.5em]
\implies |A’|&=p|A|
\end{aligned}$$Corollary: Using the 3rd and the 4th property we can also prove that, if a line of a determinant is a multiple of a parallel line, then the value of the determinant is zero.
$$\begin{aligned}
|A|&=
\begin{vmatrix}
a & b & c \\
pa & pb & pc \\
g & h & i
\end{vmatrix}
= p
\begin{vmatrix}
a & b & c \\
a & b & c \\
g & h & i
\end{vmatrix} \\[0.5em]
\implies |A|&=p(0)\\[0.5em]
\implies |A|&=0
\end{aligned}$$ - If any line of the determinant has each element as a sum of \(t\) terms, then the determinant can be written as the sum of \(t\) determinants.Let’s take an example of a determinant having one column consisting of elements with the sum of three terms.$$\begin{aligned}
|A|&=
\begin{vmatrix}
a & b & c+j-m \\
d & e & f+k-n \\
g & h & i+l-o
\end{vmatrix}\\[1em]
&=
\begin{vmatrix}
a & b & c \\
d & e & f \\
g & h & i
\end{vmatrix}
+
\begin{vmatrix}
a & b & j\\
d & e & k \\
g & h & l
\end{vmatrix}
–
\begin{vmatrix}
a & b & m \\
d & e & n \\
g & h & o
\end{vmatrix}
\end{aligned}$$Note that, subtraction of a term is equivalent to adding a negative of that term and hence the definition holds.
- The value of the determinant remains the same if a line is added by multiples of one or more parallel lines. We can prove this property using the corollary of the 4th property and the 5th property.$$\begin{aligned}
|A|&=
\begin{vmatrix}
a & b & c \\
d & e & f \\
g & h & i
\end{vmatrix}\\[1em]
|A’|&=
\begin{vmatrix}
a+pb-qc & b & c \\
d+pe-qf & e & f \\
g+ph-qi & h & i
\end{vmatrix}\\[0.5em]
&= \begin{vmatrix}
a & b & c \\
d & e & f \\
g & h & i
\end{vmatrix}
+
\begin{vmatrix}
pb & b & c \\
pe & e & f \\
ph & h & i
\end{vmatrix} –
\begin{vmatrix}
qc & b & c \\
qf & e & f \\
qi & h & i
\end{vmatrix}\\[1em]
\implies |A’|&=|A|+0+0 =
|A| \end{aligned}$$
Observations Using the Properties of the Determinant
Few useful observations using the properties of the determinants are:
- The product of the determinants of two matrices of the same order is equal to the determinant of the product of those matrices.$$\begin{aligned}
|AB|&=|A|\times|B|
\end{aligned}$$ - When a square matrix is multiplied by a constant, then the determinant of the resulting matrix is the same as the determinant of the original matrix multiplied by the \(n^{th}\) power of that constant, where \(n\) is the order of the matrix.$$\begin{aligned}
|A|&=
\begin{vmatrix}
a & b & c \\
d & e & f \\
g & h & i
\end{vmatrix}\\[1em]
|pA|&=
\begin{vmatrix}
pa & pb & pc \\
pd & pe & pf \\
pg & ph & pi
\end{vmatrix} \\
\end{aligned}$$From the 4th property, we can say that,
$$\begin{aligned}
|pA|&=p^3
\begin{vmatrix}
a & b & c \\
d & e & f \\
g & h & i
\end{vmatrix}
\\[1.5em]
|pA|&=p^3|A|
\end{aligned}$$One very important thing to note here is that, when we multiply the matrix with a constant, then we multiply each element of that matrix with the constant. But, when we multiply the determinant by a constant, then we multiply any one line (row or column) with that constant.
- The determinant of a Triangular Matrix (elements on one side of the principal diagonal are all zeros) is the product of all the diagonal elements.$$\begin{aligned}
|A|&=
\begin{vmatrix}
a & b & c \\
0 & d & e \\
0 & 0 & f
\end{vmatrix}\\[1em]
\implies |A|&=adf
\end{aligned}$$Here \(A\) is an Upper Triangular Matrix.
We can use the above observation to quickly evaluate the determinant of an Identity Matrix as one. But keep in mind that the Identity Matrix is not a triangular matrix.
$$\begin{aligned}
|I|&=
\begin{vmatrix}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & 1
\end{vmatrix}
= 1
\end{aligned}$$The condition of having zeros on one side of the principal diagonal is enough for using this observation.
Let’s take one example of a Diagonal Matrix (off-diagonal elements are zeros) to validate the above statement using the Laplace’s expansion.
$$\begin{aligned}
|A|&=
\begin{vmatrix}
4 & 0 & 0 \\
0 & 3 & 0 \\
0 & 0 & 7
\end{vmatrix}\\[0.5em]
&= 4
\begin{vmatrix}
3 & 0 \\
0 & 7
\end{vmatrix}\\[0.5em]
&=4\times3\times7=84
\end{aligned}$$ - The determinant of the cofactor matrix is the square of the determinant of that matrix.$$\begin{aligned}
|A|&=
\begin{vmatrix}
l & m & n \\
p & q & r \\
x & y & z
\end{vmatrix} \\[0.5em]
|C(A)| &=
\begin{vmatrix}
L & M & N \\
P & Q & R \\
X & Y & Z
\end{vmatrix}\\[0.5em]
\implies |C(A)|&=|A|^2
\end{aligned}$$where the capital letters denote the cofactors of the elements.
- The determinant of a matrix with the row-wise or column-wise elements in the arithmetic progression is zero.$$\begin{aligned}
|A|&=
\begin{vmatrix}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\end{vmatrix}
= 0\\[1.8em]
|B|&=
\begin{vmatrix}
1 & 7 & 13 \\
3 & 9 & 15 \\
5 & 11 & 17
\end{vmatrix}
= 0
\end{aligned}$$This is because we can covert these matrices to the matrices with equal rows or columns with elementary transformations.
![](https://i0.wp.com/media.go2speed.org/brand/files/grammarly/3/20150114062010-0103correctingwritingsupport728x90.png?resize=728%2C90&ssl=1)
Determinant of a Matrix Using Python
Now we will implement the above concepts using Python. You need to have the NumPy library of Python installed to follow the Python code given here. You can install the NumPy library using the package manager.
pip install numpy
Please go through the article on setting up Python for scientific computing if you are new to Python. I recommend you to use the Jupyter Notebook to follow the code below.
First, we need to import the NumPy library, so that we can use many extremely useful functions available in the library as necessary.
import numpy as np
The NumPy library of Python makes it a breeze to evaluate the determinant of a matrix of any order.
We will use the numpy.linalg.det( ) function from the linalg (linear algebra) module of the NumPy library to find the determinant of a matrix.
Let’s see a few examples.
A = np.array([[2,-5],[6,7]])
d = np.linalg.det(A)
print('Matrix A:')
print(A)
print('Determinant of Matrix A:')
print(np.around(d, decimals=4)) # Rounding the value
Output:
Matrix A:
[[ 2 -5]
[ 6 7]]
Determinant of Matrix A:
44.0
Similarly, we can evaluate the determinant of higher-order matrices easily.
P = np.array([[1,3,5],[2,0,4],[4,2,7]])
det_P = np.linalg.det(P)
Q = np.array([[5,3,58],[-4,23,11],[34,2,-67]])
det_Q = np.linalg.det(Q)
R = np.array([[2,1,3,0],[1,0,2,3],[3,2,0,1],[2,0,1,3]])
det_R = np.linalg.det(R)
print('Matrix P:')
print(P)
print('Determinant of Matrix P:')
print(np.around(det_P, decimals=4)) # Rounding the value
print('------------------------------')
print('Matrix Q:')
print(Q)
print('Determinant of Matrix Q:')
print(np.around(det_Q, decimals=4)) # Rounding the value
print('------------------------------')
print('Matrix R:')
print(R)
print('Determinant of Matrix R:')
print(np.around(det_R, decimals=4)) # Rounding the value
Output:
Matrix P:
[[1 3 5]
[2 0 4]
[4 2 7]]
Determinant of Matrix P:
18.0
------------------------------
Matrix Q:
[[ 5 3 58]
[ -4 23 11]
[ 34 2 -67]]
Determinant of Matrix Q:
-53317.0
------------------------------
Matrix R:
[[2 1 3 0]
[1 0 2 3]
[3 2 0 1]
[2 0 1 3]]
Determinant of Matrix R:
-24.0
We are rounding the values of the determinants to avoid unnecessary trailing digits. Check what values you get if you don’t round them.
Minors and Cofactors of a Matrix using Python
To find out the minor of an element of a matrix, we first need to find out the submatrix and take the determinant. This submatrix is formed by deleting the row and column containing the element.
Let’s define one function to get the minor of the matrix element. This function takes three arguments: the matrix, the row number (\(i\)) and the column number (\(j\)).
We need to delete the \(i^{th}\) row and \(j^{th}\) column to get the submatrix and then take the determinant of this matrix to get the minor of the particular element.
def minor_of_element(A,i,j):
sub_A = np.delete(A,i-1,0) # Delete i-th row
sub_A = np.delete(sub_A,j-1,1) # Delete j-th column
M_ij = np.linalg.det(sub_A) # Minor of the element at ith row and jth column
return np.around(M_ij,decimals=3) # Rounding the value
One very important thing to note here is that Python indexing starts from ‘0’ while the matrix row and column numbers (\(i\) and \(j\), resp.) start from ‘1’. We are compensating for this in our function.
Let’s use our minor_of_element( ) function to find out the minors of few elements.
P = np.array([[1,3,5],[2,0,4],[4,2,7]])
print('Matrix P:')
print(P)
print('Minor of the element at 1st row and 1st column:')
print(minor_of_element(P,1,1))
print('Minor of the element at 3rd row and 2nd column:')
print(minor_of_element(P,3,2))
Output:
Matrix P:
[[1 3 5]
[2 0 4]
[4 2 7]]
Minor of the element at 1st row and 1st column:
-8.0
Minor of the element at 3rd row and 2nd column:
-6.0
Now let’s use the function for obtaining the minor of individual element (minor_of_element( )) to get the minor matrix of any given matrix.
def minor_matrix(A):
m = np.shape(A)[0] # Order of the matrix
M_A = np.zeros([m,m]) # Initializing the minor matrix with zeros
for i in range(1,m+1):
for j in range(1,m+1):
M_A[i-1,j-1] = minor_of_element(A,i,j)
return M_A
Let’s use this function to get the minor matrix of a matrix.
P = np.array([[1,3,5],[2,0,4],[4,2,7]])
print('Matrix P:')
print(P)
print('Minor Matrix of P:')
print(minor_matrix(P))
Output:
Matrix P:
[[1 3 5]
[2 0 4]
[4 2 7]]
Minor Matrix of P:
[[ -8. -2. 4.]
[ 11. -13. -10.]
[ 12. -6. -6.]]
We can use the minor_of_element( ) function to find the cofactor matrix of the given matrix. We will make use of the formula \(C_{ij} = (-1)^{i+j}M_{ij}\).
def cofactor_matrix(A):
m = np.shape(A)[0] # Order of the matrix
C_A = np.zeros([m,m]) # Initializing the cofactor matrix with zeros
for i in range(1,m+1):
for j in range(1,m+1):
C_A[i-1,j-1] = pow(-1,i+j)*minor_of_element(A,i,j)
return C_A
We will obtain the cofactor of the same matrix to observe the changes in the sign of elements with respect to the minor matrix.
P = np.array([[1,3,5],[2,0,4],[4,2,7]])
print('Matrix P:')
print(P)
print('Cofactor Matrix of P:')
print(cofactor_matrix(P))
Output:
Matrix P:
[[1 3 5]
[2 0 4]
[4 2 7]]
Cofactor Matrix of P:
[[ -8. 2. 4.]
[-11. -13. 10.]
[ 12. 6. -6.]]
Checking for the Singularity of a Matrix Using Python
As we can not take the inverse of a singular matrix, it becomes necessary to check for the singularity of a matrix to avoid the error.
We can define a simple function to check the singularity of a matrix. We will check if the determinant of a matrix is zero.
The function takes the matrix as input and returns a boolean (True or False) value. The numpy.isclose( ) function checks if the determinant is zero within an acceptable tolerance.
def check_singular(A):
return np.isclose(np.linalg.det(A),0)
Let’s see this function in action.
P = np.array([[1,3,5],[2,0,4],[4,2,7]])
S = np.array([[8,-6,2],[-6,7,-4],[2,-4,3]])
print('Matrix P:')
print(P)
print('Is Matrix P singular:')
print(check_singular(P))
print('------------------------------')
print('Matrix S:')
print(S)
print('Is Matrix S singular:')
print(check_singular(S))
Output:
Matrix P:
[[1 3 5]
[2 0 4]
[4 2 7]]
Is Matrix P singular:
False
------------------------------
Matrix S:
[[ 8 -6 2]
[-6 7 -4]
[ 2 -4 3]]
Is Matrix S singular:
True
We can use these function before calculating the inverse of a matrix.
Properties of the Determinants Using Python
We will validate the properties of the determinants with examples to consolidate our understanding.
- The determinant of a matrix and the transpose of a matrix are equal.
P = np.array([[1,3,5],[2,0,4],[4,2,7]]) print('Matrix P:') print(P) print('Transpose of Matrix P:') print(P.T) print('Determinant of Matrix P:') print(np.around(np.linalg.det(P), decimals=4)) # Rounding the value print('Determinant of the Transpose of Matrix P:') print(np.around(np.linalg.det(P.T), decimals=4)) # Rounding the value
Output:
Matrix P: [[1 3 5] [2 0 4] [4 2 7]] Transpose of Matrix P: [[1 2 4] [3 0 2] [5 4 7]] Determinant of Matrix P: 18.0 Determinant of the Transpose of Matrix P: 18.0
- Shifting the parallel lines by one place changes the sign of the determinant keeping the absolute value the same.
A = np.array([[3,5,1],[2,4,9],[7,1,6]]) A_dash = np.array([[2,4,9],[3,5,1],[7,1,6]]) print('Matrix A:') print(A) print('Determinant of Matrix A:') print(np.around(np.linalg.det(A), decimals=4)) # Rounding the value print('------------------------------') print("Matrix A':") print(A_dash) print("Determinant of Matrix A':") print(np.around(np.linalg.det(A_dash), decimals=4)) # Rounding the value
Output:
Matrix A: [[3 5 1] [2 4 9] [7 1 6]] Determinant of Matrix A: 274.0 ------------------------------ Matrix A': [[2 4 9] [3 5 1] [7 1 6]] Determinant of Matrix A': -274.0
Similarly, the corollary can be validated.
- If any two lines of a matrix are the same, then the determinant is zero.
B = np.array([[2,3,5],[2,3,5],[7,11,13]]) print("Matrix B:") print(B) print("Determinant of Matrix B:") print(np.around(np.linalg.det(B), decimals=4)) # Rounding the value
Output:
Matrix B: [[ 2 3 5] [ 2 3 5] [ 7 11 13]] Determinant of Matrix B: 0.0
- If a line of a determinant is multiplied by a scalar, the value of the new determinant can be calculated by multiplying the value of the original determinant by the same scalar value.
R = np.array([[2,1,3,0],[1,0,2,3],[3,2,0,1],[2,0,1,3]]) # 3rd row of the determinant is multiplied by 4 R_dash = np.array([[2,1,3,0],[1,0,2,3],[12,8,0,4],[2,0,1,3]]) print('Matrix R:') print(R) print('Determinant of Matrix R:') print(np.around(np.linalg.det(R), decimals=4)) # Rounding the value print('------------------------------') print("Matrix R':") print(R_dash) print("Determinant of Matrix R':") print(np.around(np.linalg.det(R_dash), decimals=4)) # Rounding the value
Output:
Matrix R: [[2 1 3 0] [1 0 2 3] [3 2 0 1] [2 0 1 3]] Determinant of Matrix R: -24.0 ------------------------------ Matrix R': [[ 2 1 3 0] [ 1 0 2 3] [12 8 0 4] [ 2 0 1 3]] Determinant of Matrix R': -96.0
Corollary: If a line of a determinant is a scalar multiple of a parallel line, then the determinant evaluates to zero.
A = np.array([[3,1,2],[6,2,4],[8,5,9]]) # 2nd row is twice the 1st row print('Matrix A:') print(A) print('Determinant of Matrix A:') print(np.around(np.linalg.det(A), decimals=4)) # Rounding the value
Output:
Matrix A: [[3 1 2] [6 2 4] [8 5 9]] Determinant of Matrix A: 0.0
- If any line of the determinant has each element as a sum of \(t\) terms, then the determinant can be written as the sum of \(t\) determinants.
$$\begin{aligned}
&\hspace{2em}|L| =
\begin{vmatrix}
1 & 5 & 12\\
3 & 2 & 11\\
4 & 5 & 17
\end{vmatrix}\\[1.5em]
|L| &= |L_1| + |L_2| + |L_3| \\[0.5em]
&=
\begin{vmatrix}
1 & 5 & 5\\
3 & 2 & 2\\
4 & 5 & 5
\end{vmatrix}
+
\begin{vmatrix}
1 & 5 & 5\\
3 & 2 & 2\\
4 & 5 & 5
\end{vmatrix}
+
\begin{vmatrix}
1 & 5 & 2\\
3 & 2 & 7\\
4 & 5 & 7
\end{vmatrix}
\end{aligned}
$$As in \(|L_1|\) and \(|L_2|\), the \(2^{nd}\) and \(3^{rd}\) columns are the same. Hence, from the \(3^{rd}\) and \(5^{th}\) property of the determinants, we can say that
$$
|L_1| = 0 \hspace{2em} and \hspace{2em} |L_2| = 0\\[0.5em]
\Rightarrow |L| = |L_3|
$$L = np.array([[1,5,12],[3,2,11],[4,5,17]]) L1 = np.array([[1,5,5],[3,2,2],[4,5,5]]) L2 = np.array([[1,5,5],[3,2,2],[4,5,5]]) L3 = np.array([[1,5,2],[3,2,7],[4,5,7]]) print('Matrix L:') print(L) print('Matrix L1 = L2:') print(L1) print('Matrix L3:') print(L3) print('------------------------------') print('Determinant of Matrix L:') print(np.around(np.linalg.det(L), decimals=4)) # Rounding the value print('Determinant of Matrix L1 = L2:') print(np.around(np.linalg.det(L1), decimals=4)) # Rounding the value print('Determinant of Matrix L3:') print(np.around(np.linalg.det(L3), decimals=4)) # Rounding the value
Output:
Matrix L: [[ 1 5 12] [ 3 2 11] [ 4 5 17]] Matrix L1 = L2: [[1 5 5] [3 2 2] [4 5 5]] Matrix L3: [[1 5 2] [3 2 7] [4 5 7]] ------------------------------ Determinant of Matrix L: 28.0 Determinant of Matrix L1 = L2: 0.0 Determinant of Matrix L3: 28.0
- The value of the determinant remains the same if a line is added by multiples of one or more parallel lines.Let’s take one example where \(1^{st}\) column is added with 3 times the \(2^{nd}\) column and 2 times the \(3^{rd}\) column, i.e. \(C_1 = C_1 + 3C_2 + 2C_3\).
A = np.array([[1,3,2],[3,2,1],[2,1,3]]) A_dash = np.array([[14,3,2],[11,2,1],[11,1,3]]) print('Matrix A:') print(A) print("Matrix A':") print(A_dash) print('------------------------------') print('Determinant of Matrix A:') print(np.around(np.linalg.det(A), decimals=4)) # Rounding the value print("Determinant of Matrix A':") print(np.around(np.linalg.det(A_dash), decimals=4)) # Rounding the value
Output:
Matrix A: [[1 3 2] [3 2 1] [2 1 3]] Matrix A': [[14 3 2] [11 2 1] [11 1 3]] ------------------------------ Determinant of Matrix A: -18.0 Determinant of Matrix A': -18.0
Useful Observations with Determinants Using Python
- The product of the determinants of two matrices of the same order is equal to the determinant of the product of those matrices.$$\begin{aligned}
|AB|&=|A|\times|B|
\end{aligned}$$The product of matrices \(AB\) is calculated by using the matrix multiplication function from the NumPy library.
A = np.array([[1,3,2],[3,2,1],[2,1,3]]) B = np.array([[2,8,5],[1,5,7],[6,4,9]]) det_A = np.linalg.det(A) det_B = np.linalg.det(B) AB = np.matmul(A,B) # Matrix multiplication det_AB = np.linalg.det(AB) print('Matrix A:') print(A) print("Matrix B:") print(B) print('Matrix AB:') print(AB) print('------------------------------') print('Determinant of Matrix A:') print(np.around(det_A, decimals=4)) # Rounding the value print('Determinant of Matrix B:') print(np.around(det_B, decimals=4)) # Rounding the value print('------------------------------') print('Multiplication of Determinants of the matrices A and B (|A|x|B|):') print(det_A*det_B) print('Determinant of Matrix AB (|AB|):') print(np.around(det_AB, decimals=4)) # Rounding the value
Output:
Matrix A: [[1 3 2] [3 2 1] [2 1 3]] Matrix B: [[2 8 5] [1 5 7] [6 4 9]] Matrix AB: [[17 31 44] [14 38 38] [23 33 44]] ------------------------------ Determinant of Matrix A: -18.0 Determinant of Matrix B: 168.0 ------------------------------ Multiplication of Determinants of the matrices A and B (|A|x|B|): -3024.0 Determinant of Matrix AB (|AB|): -3024.0
- When a square matrix is multiplied by a constant, then the determinant of the resulting matrix is the same as the determinant of the original matrix multiplied by the \(n^{th}\) power of that constant, where \(n\) is the order of the matrix.$$\begin{aligned}
|pA|&=p^n|A|
\end{aligned}$$A = np.array([[1,3,2],[3,2,1],[2,1,3]]) A_dash = 2*np.array([[1,3,2],[3,2,1],[2,1,3]]) # A' = 2A det_A = np.linalg.det(A) det_A_dash = np.linalg.det(A_dash) print('Matrix A:') print(A) print("Matrix A':") print(A_dash) print('------------------------------') print('Determinant of Matrix A:') print(np.around(det_A, decimals=4)) # Rounding the value print("Determinant of Matrix A'(|2A|):") print(np.around(det_A_dash, decimals=4)) # Rounding the value print('Determinant of Matrix A Multiplied by 2 Cubed (8|A|):') print(np.around(pow(2,3)*det_A))
Output:
Matrix A: [[1 3 2] [3 2 1] [2 1 3]] Matrix A': [[2 6 4] [6 4 2] [4 2 6]] ------------------------------ Determinant of Matrix A: -18.0 Determinant of Matrix A'(|2A|): -144.0 Determinant of Matrix A Multiplied by 2 Cubed (8|A|): -144.0
- The determinants of a Triangular Matrix and Diagonal Matrix (elements on one side of the principal diagonal are all zeros) are the product of all the diagonal elements.
A = np.array([[1,2,3],[0,4,5],[0,0,6]]) # Upper Triangular Matrix B = np.array([[4,0,0],[0,3,0],[0,0,7]]) # Diagonal Matrix print('Matrix A:') print(A) print('Determinant of Matrix A:') print(np.around(np.linalg.det(A), decimals=4)) # Rounding the value print('Multiplication of Diagonal Elements (1x4x6):') print(1*4*6) print('------------------------------') print("Matrix B:") print(B) print('Determinant of Matrix B:') print(np.around(np.linalg.det(B), decimals=4)) # Rounding the value print('Multiplication of Diagonal Elements (4x3x7):') print(4*3*7)
Output:
Matrix A: [[1 2 3] [0 4 5] [0 0 6]] Determinant of Matrix A: 24.0 Multiplication of Diagonal Elements (1x4x6): 24 ------------------------------ Matrix B: [[4 0 0] [0 3 0] [0 0 7]] Determinant of Matrix B: 84.0 Multiplication of Diagonal Elements (4x3x7): 84
Similarly, we can calculate the determinants of the Lower Triangular Matrix, Scalar Matrix and Identity Matrix.
- The determinant of the cofactor matrix is the square of the determinant of that matrix.
P = np.array([[1,3,5],[2,0,4],[4,2,7]]) det_P = np.linalg.det(P) C_P = cofactor_matrix(P) det_C_P = np.linalg.det(C_P) print('Matrix P:') print(P) print("Cofactor Matrix of Matrix P:") print(C_P) print('Determinant of Matrix P:') print(np.around(det_P, decimals=4)) # Rounding the value print('Square of the Determinant of Matrix P:') print(np.around(pow(det_A,2), decimals=4)) # Rounding the value print('Determinant of the Cofactor Matrix of Matrix P:') print(np.around(det_C_P, decimals=4)) # Rounding the value
Output:
Matrix P: [[1 3 5] [2 0 4] [4 2 7]] Cofactor Matrix of Matrix P: [[ -8. 2. 4.] [-11. -13. 10.] [ 12. 6. -6.]] Determinant of Matrix P: 18.0 Square of the Determinant of Matrix P: 324.0 Determinant of the Cofactor Matrix of Matrix P: 324.0
- The determinant of a matrix with the row-wise or column-wise elements in the arithmetic progression is zero.
A = np.array([[1,2,3],[4,5,6],[7,8,9]]) B = np.array([[1,7,13],[3,9,15],[5,11,17]]) print('Matrix A:') print(A) print("Determinant of Matrix A:") print(np.around(np.linalg.det(A), decimals=4)) # Rounding the value print('------------------------------') print('Matrix B:') print(B) print("Determinant of Matrix B:") print(np.around(np.linalg.det(B), decimals=4)) # Rounding the value
Output:
Matrix A: [[1 2 3] [4 5 6] [7 8 9]] Determinant of Matrix A: 0.0 ------------------------------ Matrix B: [[ 1 7 13] [ 3 9 15] [ 5 11 17]] Determinant of Matrix B: 0.0
Did you find the article useful? Please rate, comment and share it with your friends. Please check other articles in the series on Linear Algebra.
This was very well written, some Linear Algebra concepts became more clear after reading this. Thank you
Thank you very much