SymPy demo 5: Diagonalisation#
Demo by Christian Mikkelstrup, Hans Henrik Hermansen, Karl Johan Måstrup Kristiansen and Magnus Troen. Revised 22-10-26 by shsp.
from sympy import *
init_printing()
According to the course textbook, a matrix \(\mathbf A\) is diagonalisable if it has enough linearly independent eigenvectors to form a basis from. The diagonalisation then takes place by switching to coordinates with respect to such ordered eigenbasis:
Here \(\mathbf V\) contains the eigenbasis vectors as columns. The result will then always be a diagonal matrix, here denoted by \(\mathbf \Lambda\), containing the associated eigenvalues in the diagonal.
In this demo we will showcase how to find these components and carry out the diagonalisation.
Simulated manually#
As an example, we are given the following matrix \(\mathbf B\), which we wish to diagonalise:
B = Matrix([[0,1,0],[-2,3,0],[0,0,2]])
B
The Diagonal Matrix \(\mathbf \Lambda\)#
We find the eigenvalues and eigenvectors by:
B.eigenvects()
We see that the eigenvalue \(\lambda_1=1\) has an algebraic multiplicity of \(1\) and has one corresponding eigenvector. The eigenvalue \(\lambda_2=2\) has an algebraic multiplicity of \(2\) and two corresponding (linearly independent) eigenvectors. Since the sum of geometric multiplicities and the sum of algebraic multiplicities are equal, the matrix is diagonalisable (see the theorem in the course textbook). Hence, by simply placing the eigenvalues in the diagonal of a diagonal matrix which we can call \(\mathbf \Lambda\), then \(\mathbf \Lambda\) is the diagonalisation of \(\mathbf B\):
Lambda = Matrix.diag([1,2,2])
Lambda
The Eigenbasis#
We do not know with respect to which ordered eigenbasis this is the case, though. So, let us find that eigenbasis and then confirm that the equation \(\mathbf V^{-1}\,\mathbf B\,\mathbf V=\mathbf \Lambda\) fits. First, we can write out the eigenspaces for an overview:
Let us define the eigenvectors:
v1 = Matrix([1,1,0])
v2 = Matrix([S(1)/2,1,0])
v3 = Matrix([0,0,1])
v1,v2,v3
A theorem in the course textbook ensures that the eigenvectors \(\mathbf v_1,\mathbf v_2,\mathbf v_3\) form a linearly independent sequence. Hence they constitute a basis, which is an eigenbasis. The theorem further ensures that \(\mathbf V = [\mathbf v_1\ \mathbf v_2\ \mathbf v_3]\) is invertible. Let us define \(\mathbf V\) and determine its inverse matrix \(\mathbf V^{-1}\):
V = Matrix.hstack(v1,v2,v3)
V, V.inv()
Have we placed the vectors as columns in \(\mathbf V\) in the right order? Yes, because \(\mathbf v_1\) belongs to \(\lambda_1\), \(\mathbf v_2\) belongs to \(\lambda_2\), and \(\mathbf v_3\) belongs to \(\lambda_2\). Let us check if the equation is fulfilled:
V.inv()*B*V , Lambda
V.inv()*B*V==Lambda
True
As expected!
Using SymPy commands#
As an example, the following matrix \(\mathbf C\) is given:
C = Matrix([[6,2,4],[2,9,-2],[4,-2,6]])
C
We notice that \(\mathbf C\) is symmetric. Hence (according to a theorem in the course textbook) we immediately know that \(\mathbf C\) is diagonalisable. We can check with the following command, should we be in doubt:
C.is_diagonalizable()
True
Since \(\mathbf C\) is diagonalisable, then we can determine matrices \(\mathbf V\) and \(\mathbf \Lambda\) using .diagonalize():
V, Lambda = C.diagonalize()
V, Lambda
Let us confirm that \(\mathbf C = \mathbf V\mathbf \Lambda \mathbf V^{-1}\) and that \(\mathbf \Lambda = \mathbf V^{-1} \mathbf C \mathbf V:\)
V * Lambda * V.inv()
V * Lambda * V.inv()==C
True
V.inv() * C * V , Lambda
V.inv() * C * V==Lambda
True