Problem 27, Section 5.5

We'll be walking through the problem in the title, which serves as a proxy for your actual homework problem 28 on the same section.

Luckily for us, the text of the problem also happens to tell us exactly what to do.. I'll be commenting on the individual steps. First, the boring bit where we just prep Python.

In [2]:
from sympy import *
init_printing(use_latex='mathjax')

Next, let's define our main matrix the book calls $A$, being careful to avoind rounding errors by defining our entries via SymPy's Rational(a,b) construct for the fraction $\frac ab$.

In [3]:
A=Matrix([[Rational(7,10),Rational(11,10),2,Rational(17,10)],
          [-2,-4,-Rational(86,10),-Rational(74,10)],
          [0,-Rational(5,10),-1,-1],
          [1,Rational(28,10),6,Rational(53,10)]])
A
Out[3]:
$\displaystyle \left[\begin{matrix}\frac{7}{10} & \frac{11}{10} & 2 & \frac{17}{10}\\-2 & -4 & - \frac{43}{5} & - \frac{37}{5}\\0 & - \frac{1}{2} & -1 & -1\\1 & \frac{14}{5} & 6 & \frac{53}{10}\end{matrix}\right]$

Next, the problem says something about eigenvalues; let's have a look at those.

In [5]:
A.eigenvals()
Out[5]:
$\displaystyle \left\{ \frac{1}{5} - \frac{i}{2} : 1, \ \frac{1}{5} + \frac{i}{2} : 1, \ \frac{3}{10} - \frac{i}{10} : 1, \ \frac{3}{10} + \frac{i}{10} : 1\right\}$

So they come in two pairs of mutually conjugate complex numbers. That is, the eigenvalues are $z$ and $\overline{z}$ together with $w$ and $\overline{w}$ where

$$ z=\frac 15-\frac i2\text{ and }w=\frac 3{10}-\frac i{10}. $$

Next, they tell us to look at eigenvectors, so let's do so.

In [6]:
evects=A.eigenvects()
evects
Out[6]:
$\displaystyle \left[ \left( \frac{1}{5} - \frac{i}{2}, \ 1, \ \left[ \left[\begin{matrix}\frac{1}{2} - \frac{i}{2}\\-2\\0\\1\end{matrix}\right]\right]\right), \ \left( \frac{1}{5} + \frac{i}{2}, \ 1, \ \left[ \left[\begin{matrix}\frac{1}{2} + \frac{i}{2}\\-2\\0\\1\end{matrix}\right]\right]\right), \ \left( \frac{3}{10} - \frac{i}{10}, \ 1, \ \left[ \left[\begin{matrix}- \frac{1}{2}\\\frac{i}{2}\\- \frac{3}{4} - \frac{i}{4}\\1\end{matrix}\right]\right]\right), \ \left( \frac{3}{10} + \frac{i}{10}, \ 1, \ \left[ \left[\begin{matrix}- \frac{1}{2}\\- \frac{i}{2}\\- \frac{3}{4} + \frac{i}{4}\\1\end{matrix}\right]\right]\right)\right]$

I saved the output of SymPy's eigenvects method to a Python list.

Now, what are we supposed to do with those? Well, note first that the eigenvectors for mutually conjugate eigenvalues are also mutually conjugate. By this I mean that if $v$ is an eigenvector for $z$, then $\overline{v}$, the vector obtained by applying the complex-conjugate operation to each entry of $v$, is an eigenvector for $\overline{z}$. Ditto for the other eigenvalue $w$.

The problem tells us to first pick out one eigenvector from each mutually-conjugate pair. Let say, the first eigenvector and the third one, as listed above:

In [7]:
interesting_evects=[evects[0][2][0],evects[2][2][0]]
interesting_evects
Out[7]:
$\displaystyle \left[ \left[\begin{matrix}\frac{1}{2} - \frac{i}{2}\\-2\\0\\1\end{matrix}\right], \ \left[\begin{matrix}- \frac{1}{2}\\\frac{i}{2}\\- \frac{3}{4} - \frac{i}{4}\\1\end{matrix}\right]\right]$

I had to use all of those indices because evects is a list of lists. When I write evects[0] I mean its first member (remember zero-based indexing), which, if you look at evects, is of the form

(eigenvalue, multiplicity, [list of eigenvectors])

Then, out of that list, I want to extract its third element, i.e. evects[0][2] because of 0-based indexing again. That'll give me back what I denoted [list of eigenvectors]. Finally, I want the first element of that list (which actually only has one element here because multiplicity=1), so I take the zero-indexed entry: evects[0][2][0] overall.

Similarly, you get the third eigenvector by evects[2][2][0] (the only difference is that the first index I passed it is 2 in place of 0).

Anyway, I now have my interesting eigenvectors, saved conveniently to the list of vectors called interesting_evects.

Turning to the problem text again, I'm supposed to now break up each of those eigenvectors into its real and imaginary part. Luckily, SymPy has re and im functions that do just this for us:

In [8]:
broken_interesting_evects=[[re(x),im(x)] for x in interesting_evects]
broken_interesting_evects
Out[8]:
$\displaystyle \left[ \left[ \left[\begin{matrix}\frac{1}{2}\\-2\\0\\1\end{matrix}\right], \ \left[\begin{matrix}- \frac{1}{2}\\0\\0\\0\end{matrix}\right]\right], \ \left[ \left[\begin{matrix}- \frac{1}{2}\\0\\- \frac{3}{4}\\1\end{matrix}\right], \ \left[\begin{matrix}0\\\frac{1}{2}\\- \frac{1}{4}\\0\end{matrix}\right]\right]\right]$

Notice that I used a Python list comprehension there. This broke up each eigenvector x into pieces re(x) and im(x), put those together into a list [re(x),im(x)], and then collected those two lists together into a list of lists.

Next, I'd like to take all four of those vectors you can see in broken_interesting_evects and stick them together into a matrix $P$, as the problem says we should. First, since they're stuck not inside a list but rather a list of lists, let's "flatten" it:

In [9]:
cols=[y for x in broken_interesting_evects for y in x]
cols
Out[9]:
$\displaystyle \left[ \left[\begin{matrix}\frac{1}{2}\\-2\\0\\1\end{matrix}\right], \ \left[\begin{matrix}- \frac{1}{2}\\0\\0\\0\end{matrix}\right], \ \left[\begin{matrix}- \frac{1}{2}\\0\\- \frac{3}{4}\\1\end{matrix}\right], \ \left[\begin{matrix}0\\\frac{1}{2}\\- \frac{1}{4}\\0\end{matrix}\right]\right]$

OK, now we have a list of columns for our desired matrix $P$. How to make a matrix? If I ask Sympy to just do it, it'll misbehave:

In [10]:
Matrix(cols)
Out[10]:
$\displaystyle \left[\begin{matrix}\frac{1}{2}\\-2\\0\\1\\- \frac{1}{2}\\0\\0\\0\\- \frac{1}{2}\\0\\- \frac{3}{4}\\1\\0\\\frac{1}{2}\\- \frac{1}{4}\\0\end{matrix}\right]$

Oops.. It stacked them vertically rather than horizontally. Let's turn to numpy instead, which has a horizontal-stacking function:

In [12]:
import numpy as np
P=Matrix(np.hstack(cols))
P
Out[12]:
$\displaystyle \left[\begin{matrix}\frac{1}{2} & - \frac{1}{2} & - \frac{1}{2} & 0\\-2 & 0 & 0 & \frac{1}{2}\\0 & 0 & - \frac{3}{4} & - \frac{1}{4}\\1 & 0 & 1 & 0\end{matrix}\right]$

Looks fine now! Finally, to wrap up, I have to ensure that the original matrix $A$ is of the form $PCP^{-1}$, where $P$ we've just written down and $C$ is a block-diagonal matrix of the desired form (see the problem text).

This means that $P^{-1}AP=C$, so I have to check that indeed that product $P^{-1}AP$ is of the appropriate form. Let us.

In [13]:
P.inv()*A*P
Out[13]:
$\displaystyle \left[\begin{matrix}\frac{1}{5} & - \frac{1}{2} & 0 & 0\\\frac{1}{2} & \frac{1}{5} & 0 & 0\\0 & 0 & \frac{3}{10} & - \frac{1}{10}\\0 & 0 & \frac{1}{10} & \frac{3}{10}\end{matrix}\right]$

Yes! This is our $C$, and we indeed wanted it to be block-diagonal, with $2\times 2$ diagonal blocks of the form

$$ \left[ \begin{array}{rr} a&-b\\ b&a \end{array} \right]. $$

That's exactly what we got, so let's collect it all together.

In [14]:
A
Out[14]:
$\displaystyle \left[\begin{matrix}\frac{7}{10} & \frac{11}{10} & 2 & \frac{17}{10}\\-2 & -4 & - \frac{43}{5} & - \frac{37}{5}\\0 & - \frac{1}{2} & -1 & -1\\1 & \frac{14}{5} & 6 & \frac{53}{10}\end{matrix}\right]$
In [15]:
P
Out[15]:
$\displaystyle \left[\begin{matrix}\frac{1}{2} & - \frac{1}{2} & - \frac{1}{2} & 0\\-2 & 0 & 0 & \frac{1}{2}\\0 & 0 & - \frac{3}{4} & - \frac{1}{4}\\1 & 0 & 1 & 0\end{matrix}\right]$
In [16]:
C=P.inv()*A*P
C
Out[16]:
$\displaystyle \left[\begin{matrix}\frac{1}{5} & - \frac{1}{2} & 0 & 0\\\frac{1}{2} & \frac{1}{5} & 0 & 0\\0 & 0 & \frac{3}{10} & - \frac{1}{10}\\0 & 0 & \frac{1}{10} & \frac{3}{10}\end{matrix}\right]$
In [17]:
A==P*C*P.inv()
Out[17]:
True