Eigenvalue problem#

Demo by Christian Mikkelstrup og Hans Henrik Hermansen, Karl Johan Måstrup Kristiansen and Magnus Troen

from sympy import*
from dtumathtools import*
init_printing()

In this demo we show how to manipulate a matrix when trying to solve the eigenvalue problem, (getting the eigenvectors and eigenvalues). First “simulated by-hand calculations” are shown before showing the proper commands.

We are going to look at the matrix

\[\begin{split} _eF_e = \begin{bmatrix}6&3&12\\4&-5&4\\-4&-1&-10\end{bmatrix} \end{split}\]

for the linear function \(f:\mathbb{R}^3\to \mathbb{R}^3\) with regards to the standard basis.

eFe = Matrix([[6,3,12],[4,-5,4],[-4,-1,-10]])

Eigenvalueproblem#

Finding eigenvalues#

The character matrix \(K(\lambda)\) is constructed as such

lamb = symbols('\lambda')
K = eFe - lamb * eye(3)
K
\[\begin{split}\displaystyle \left[\begin{matrix}6 - \lambda & 3 & 12\\4 & - \lambda - 5 & 4\\-4 & -1 & - \lambda - 10\end{matrix}\right]\end{split}\]

The character polynomial is then given by

charpoly = K.det()
charpoly
../_images/f97be801d45b695da573600e4152d30d955b65da64e3c47ecac323f87b543e7d.png

A SymPy command can also be used here to reach the same result, but do note that the sign here is different. This is not a problem, as we are interested in the roots of this polynomial.

eFe.charpoly()
../_images/bc0c12acf698d1d15cd0f72c8b2029da14fb51699c49c7ee12e1845fa5063fdb.png

The eigenvalues are then found by either of the following methods

char_eq = Eq(charpoly, 0)
roots(charpoly), solveset(char_eq, lamb)
../_images/c64b38156ce2e3415683631044e45090c632459932b56d1f1909b168d0783bac.png

Though \(\verb|solveset()|\) does not catch the fact that \(-6\) has multiplicity 2! It can be found manually by asking for the factorisation, but in most cases it is much more convenient to use \(\verb|roots()|\)

factor(charpoly)
../_images/b2066a77d7a327f68c319f5c3b630edb23f05506b52ee3567d875329ea9f9bfa.png

Finding the eigenvectors#

Finding the eigenvectors belonging to the eignevalue \(-6\) means solving the equation \(K(-6)\,x=\mathbf{0}\),

K.subs(lamb, -6)
\[\begin{split}\displaystyle \left[\begin{matrix}12 & 3 & 12\\4 & 1 & 4\\-4 & -1 & -4\end{matrix}\right]\end{split}\]

Solutions are found with previously learned commands

K.subs(lamb, -6).gauss_jordan_solve(zeros(3,1))
\[\begin{split}\displaystyle \left( \left[\begin{matrix}- \frac{\tau_{0}}{4} - \tau_{1}\\\tau_{0}\\\tau_{1}\end{matrix}\right], \ \left[\begin{matrix}\tau_{0}\\\tau_{1}\end{matrix}\right]\right)\end{split}\]

The eigenspace for the eigenvalue \(-6\) is therefore

\[\begin{split} E_{-6}=\text{span}\left\{\begin{bmatrix}-\frac{1}{4} \\ 1\\ 0 \end{bmatrix}, \begin{bmatrix} -1\\ 0\\ 1\end{bmatrix}\right\} \end{split}\]

Here it is also the case the the geometric multiplicity, like the algebraic multiplicity, is 2 for the eigenvalue \(-6\)

Eignevectors for the eigenvalue 3 is again found by solving \(K(3)\,x=\mathbf{0}\), which some will will have noted is the same is finding $\(\ker K(3)\)\( (this would of course also work for \)K(-6)$)

K.subs(lamb, 3).nullspace()
\[\begin{split}\displaystyle \left[ \left[\begin{matrix}-3\\-1\\1\end{matrix}\right]\right]\end{split}\]

From this we see that \(E_3=\text{span}\{(-3,-1,1)\}\) and that the geometric multiplicity is equal to the algebraic multiplicity of 1.

If the last method weren’t good enough for you, fear not! Because these last couple of commands makes it seem like a piece of cake!

The quick SymPy commands for eigenvalues and -vectors#

Having your original matrix, no \(K\)’s or anything use these commands:

eFe.eigenvals()
../_images/8ccf4ca7a51e99951d01f9837347e7f525b99389efd7e422e5e88b0b1c5f60eb.png
eFe.eigenvects()
\[\begin{split}\displaystyle \left[ \left( -6, \ 2, \ \left[ \left[\begin{matrix}- \frac{1}{4}\\1\\0\end{matrix}\right], \ \left[\begin{matrix}-1\\0\\1\end{matrix}\right]\right]\right), \ \left( 3, \ 1, \ \left[ \left[\begin{matrix}-3\\-1\\1\end{matrix}\right]\right]\right)\right]\end{split}\]

\(\verb|.eigenvects()|\) provides all data you need (eigenvalue, algebraic multiplicity, eigenvectors) as such $\(\left[\Bigl(\lambda_1, \quad \operatorname {am}(\lambda_1),\quad \verb|eigenvectors for |\lambda_1\Bigr), \Bigl(\lambda_2, \quad \operatorname {am}(\lambda_2),\quad \verb|eigenvectors for |\lambda_2\Bigr),\ \ldots\right]\)$ for each eigenvalue in a list. Which lets one draw out information.

Remember also that the amount of vectors in \(\verb|eigenvectors for |\lambda\), tells you the geometric multiplicity!

eigen_info = eFe.eigenvects()

for lmb, am_lmb, list_of_v in eigen_info:
    print(f'For the eigenvalue {lmb} with algebraic multiplicity {am_lmb}, the eigenvectors are')
    display(list_of_v)
    print(f'and has geomtric multiplicity {len(list_of_v)}' + '\n\n')
For the eigenvalue -6 with algebraic multiplicity 2, the eigenvectors are
\[\begin{split}\displaystyle \left[ \left[\begin{matrix}- \frac{1}{4}\\1\\0\end{matrix}\right], \ \left[\begin{matrix}-1\\0\\1\end{matrix}\right]\right]\end{split}\]
and has geomtric multiplicity 2


For the eigenvalue 3 with algebraic multiplicity 1, the eigenvectors are
\[\begin{split}\displaystyle \left[ \left[\begin{matrix}-3\\-1\\1\end{matrix}\right]\right]\end{split}\]
and has geomtric multiplicity 1

or accessing it directly by indexing. Be careful though! Sometimes this gets pretty confusing

# eigenvectors for eigenvalue -6
eigen_info[0][2]
\[\begin{split}\displaystyle \left[ \left[\begin{matrix}- \frac{1}{4}\\1\\0\end{matrix}\right], \ \left[\begin{matrix}-1\\0\\1\end{matrix}\right]\right]\end{split}\]

Awesome!

Sometimes the eigenvectors are chosen differently by SymPy, than if you yourself does the calculations, but the eigenspace spanned is always the same