# Eigenvalues and Eigenvectors¶

We briefly discuss some algorithms for finding some eigenvalues and eigenvectors of square matrices.

To read more about this topic see § 4.6 of TAK, which we follow somewhat.

In :
# Standard imports
import numpy as np
import matplotlib.pyplot as plt
import math as m
from mpmath import mp, iv
from scipy import linalg


## Review of the definitions¶

We review the basic definitions related to eigenvalues and eigenvectors. Any textbook in linear algebra will give a more expansive explaination of these concepts.

Let $A$ be a $n \times n$ matrix. A non-zero vector $v \in {\mathbb R}^n$ is an eigenvector if there is a real number $\lambda$ such that $A v = \lambda v$. This number $\lambda$ is called an eigenvalue of $A$ and is eigenvalue associated to $v$.

An eigenvector determines an eigenvalue. In the other direction, if you have an eigenvalue $\lambda$, you can determine all the associated eigenvectors by solving the equation $A v = \lambda v$. This can be written as the matrix equation $$(A - \lambda I) v = 0.$$ The set of solutions to this equation forms a subspace of ${\mathbb R}^n$ called the eigenspace of $\lambda$. This eigenspace consists of the zero vector and all eigenvectors whose eigenvalue is $\lambda$.

From the fact that we are only considering non-zero vectors $v$, it can be seen that eigenvalues are the solutions to the characteristic equation: $$\det(A-\lambda I)=0.$$ It follows that an $n \times n$ matrix can have at most $n$ eigenvalues. The function of $\lambda$, $\det(A-\lambda I)$ is a polynomial and the roots are the eigenvalues. The algebraic multiplicity of an eigenvalue $r$ is the number of times the term $\lambda-r$ can be factored out of this polynomial. The geometric multiplicity of an eigenvalue is the dimension of the associated eigenspace. The geometric multiplicity of an eigenvalue is always less than or equal its algebraic multiplicity.

Remark. Since a non-zero scalar multiple of an eigenvector is an eigenvector, it is common to look for eigenvectors with length one.

## Finding a dominant eigenvalue and eigenvector¶

An eigenvalue of a matrix $A$ is a called dominant if its absolute value is larger than the absolute values of the other eigenvalues of $A$. We will first consider trying to find a dominant eigenvalue.

### Diagonal Matrices¶

Diagonal matrices are the simplest to understand. Let $D$ be the matrix $$D=\left(\begin{matrix} d_0 & 0 & \ldots & & \ldots & 0 \\ 0 & d_1 & 0 & & & \vdots \\ 0 & 0 & d_2 & 0 & \\ \vdots & & \ddots & \ddots & \ddots & \vdots \\ \vdots & & & 0 & d_{n-2} & 0 \\ 0 & \ldots & & & 0 & d_{n-1} \\ \end{matrix}\right).$$

Observe that if $e^k$ is the standard basis vector, which has all zero entries except in position $k$, where there is a one. Then we have $$D e^k = d_k e^k,$$ so $e^k$ is an eigenvector with eigenvalue $d_k$.

It follows that the set of eigenvalues is the set of diagonal elements. The eigenspace associated to an eigenvalue $\lambda$ is $$\textrm{span}~\{e^k:~d_k=\lambda\}.$$ (So typically, the eigenspace of $d_j$ is the set of all scalar multiples of $e_j$. But, for example if $i \neq j$ but $d_i=d_j$, then the eigenspace will include vectors of the form $a e^i+b e^j$.)

### Iteration¶

Iteration means repeatedly applying an operation. We will be repeatedly applying matrix multiplication.

Let $D$ be the diagonal matrix above, and let $x \in {\mathbb R}^n$ be a column vector. Iteratively multiplying by $D$ produces the sequence of vectors defined by $x_0=x$ and $$x^{k+1} = D x^{k} \quad \text{for integers k \geq 0}.$$ Since $D$ is a diagonal matrix, we can see that $$x^k=\left(\begin{matrix} d_0^k x_0 \\ d_1^k x_1 \\ \vdots \\ d_{n-1}^k x_{n-1}\end{matrix}\right).$$ Observe that the $j$-th entry of $x^k$ grows exponentially with constant $d_j$.

Assuming all entries of our initial vector $x$ are non-zero, observe that for $k$ very large, the entry of $x^k$ with largest absolute value will be the entry $j$ where $|d_j|$ is largest. (This $d_j$ is the dominant eigenvalue.) To see this clearly, we'll do an experiment. To keep entries from blowing up, we'll rescale our vectors to make them unit length.

In :
# A diagonal matrix.
# Note entry with largest absolute value is in the second position.
D = np.diag([-1, -4, 0.25, 3])
print(D)

[[-1.    0.    0.    0.  ]
[ 0.   -4.    0.    0.  ]
[ 0.    0.    0.25  0.  ]
[ 0.    0.    0.    3.  ]]

In :
# Build a vector with random entries in (-1, 1)
x = 2*np.random.random(4)-1
x

Out:
array([ 0.94541715, -0.12981172, -0.84915126,  0.49166402])

We'll arrange the $x^k$ vectors we compute under interation in a list x_list. Here x_list[k] will store $x^k$, except we will rescale all our vectors to have unit length.

The norm of a vector $x$ can be computed with

np.linalg.norm(x)


Dividing by this norm gives a unit vector. (We are doing this rescaling to keep entries from blowing up.)

In :
x_list = []
x_list.append(x/np.linalg.norm(x))
x_list

Out:
[array([ 0.69071951, -0.09484013, -0.62038788,  0.35920856])]
In :
temp = x_list
temp

Out:
array([ 0.69071951, -0.09484013, -0.62038788,  0.35920856])
In :
np.linalg.norm(temp)

Out:
1.0
In :
m.sqrt(temp**2 + temp**2 + temp**2 + temp**2)

Out:
1.0

We will compute up to $x^{20}$:

In :
for k in range(0,200):
x_k = x_list[k]
x_next = D @ x_k
x_list.append(x_next / np.linalg.norm(x_next))
x_list

Out:
[array([ 0.69071951, -0.09484013, -0.62038788,  0.35920856]),
array([-0.51392764,  0.28226198, -0.1153994 ,  0.80180393]),
array([ 0.18987909, -0.41714548, -0.01065906,  0.88871928]),
array([-6.02604819e-02,  5.29545150e-01, -8.45695868e-04,  8.46138224e-01]),
array([ 1.82240819e-02, -6.40583936e-01, -6.39392113e-05,  7.67671870e-01]),
array([-5.28961873e-03,  7.43729052e-01, -4.63965828e-06,  6.68460259e-01]),
array([ 1.47436939e-03, -8.29194998e-01, -3.23301664e-07,  5.58957495e-01]),
array([-3.96701052e-04,  8.92429077e-01, -2.17472825e-08,  4.51187527e-01]),
array([ 1.03910390e-04, -9.35038142e-01, -1.42410046e-09,  3.54547121e-01]),
array([-2.67228033e-05,  9.61861091e-01, -9.15595554e-11,  2.73538372e-01]),
array([ 6.79280777e-06, -9.78001809e-01, -5.81849939e-12,  2.08596406e-01]),
array([-1.71460053e-06,  9.87445827e-01, -3.67167837e-13,  1.57958029e-01]),
array([ 4.31009024e-07, -9.92879809e-01, -2.30742742e-14,  1.19120462e-01]),
array([-1.08088284e-07,  9.95976126e-01, -1.44663949e-15,  8.96189563e-02]),
array([ 2.70696716e-08, -9.97730584e-01, -9.05742381e-17,  6.73326182e-02]),
array([-6.77413943e-09,  9.98721551e-01, -5.66651240e-18,  5.05496207e-02]),
array([ 1.69448228e-09, -9.99280269e-01, -3.54355152e-19,  3.79334249e-02]),
array([-4.23753975e-10,  9.99594960e-01, -2.21541715e-20,  2.84590281e-02]),
array([ 1.05957268e-10, -9.99772104e-01, -1.38488110e-21,  2.13480536e-02]),
array([-2.64919581e-11,  9.99871790e-01, -8.65636991e-23,  1.60126367e-02]),
array([ 6.62336104e-12, -9.99927876e-01, -5.41053467e-24,  1.20101511e-02]),
array([-1.65589251e-12,  9.99959428e-01, -3.38169087e-25,  9.00789759e-03]),
array([ 4.13980475e-13, -9.99977178e-01, -2.11359431e-26,  6.75604311e-03]),
array([-1.03496152e-13,  9.99987162e-01, -1.32100964e-27,  5.06708293e-03]),
array([ 2.58741834e-14, -9.99992779e-01, -8.25635659e-29,  3.80033354e-03]),
array([-6.46856628e-15,  9.99995938e-01, -5.16023917e-30,  2.85025916e-03]),
array([ 1.61714444e-15, -9.99997715e-01, -3.22515521e-31,  2.13769817e-03]),
array([-4.04286515e-16,  9.99998715e-01, -2.01572402e-32,  1.60327523e-03]),
array([ 1.01071686e-16, -9.99999277e-01, -1.25982822e-33,  1.20245710e-03]),
array([-2.52679294e-17,  9.99999593e-01, -7.87392889e-35,  9.01843109e-04]),
array([ 6.31698347e-18, -9.99999771e-01, -4.92120643e-36,  6.76382452e-04]),
array([-1.57924603e-18,  9.99999871e-01, -3.07575433e-37,  5.07286890e-04]),
array([ 3.94811529e-19, -9.99999928e-01, -1.92234656e-38,  3.80465189e-04]),
array([-9.87028853e-20,  9.99999959e-01, -1.20146664e-39,  2.85348901e-04]),
array([ 2.46757218e-20, -9.99999977e-01, -7.50916663e-41,  2.14011679e-04]),
array([-6.16893050e-21,  9.99999987e-01, -4.69322919e-42,  1.60508761e-04]),
array([ 1.54223263e-21, -9.99999993e-01, -2.93326826e-43,  1.20381571e-04]),
array([-3.85558160e-22,  9.99999996e-01, -1.83329267e-44,  9.02861789e-05]),
array([ 9.63895402e-23, -9.99999998e-01, -1.14580792e-45,  6.77146343e-05]),
array([-2.40973851e-23,  9.99999999e-01, -7.16129951e-47,  5.07859758e-05]),
array([ 6.02434627e-24, -9.99999999e-01, -4.47581219e-48,  3.80894819e-05]),
array([-1.50608657e-24,  1.00000000e+00, -2.79738262e-49,  2.85671114e-05]),
array([ 3.76521642e-25, -1.00000000e+00, -1.74836414e-50,  2.14253336e-05]),
array([-9.41304105e-26,  1.00000000e+00, -1.09272759e-51,  1.60690002e-05]),
array([ 2.35326026e-26, -1.00000000e+00, -6.82954742e-53,  1.20517501e-05]),
array([-5.88315066e-27,  1.00000000e+00, -4.26846714e-54,  9.03881259e-06]),
array([ 1.47078766e-27, -1.00000000e+00, -2.66779196e-55,  6.77910945e-06]),
array([-3.67696916e-28,  1.00000000e+00, -1.66736998e-56,  5.08433208e-06]),
array([ 9.19242290e-29, -1.00000000e+00, -1.04210623e-57,  3.81324906e-06]),
array([-2.29810573e-29,  1.00000000e+00, -6.51316397e-59,  2.85993680e-06]),
array([ 5.74526431e-30, -1.00000000e+00, -4.07072748e-60,  2.14495260e-06]),
array([-1.43631608e-30,  1.00000000e+00, -2.54420467e-61,  1.60871445e-06]),
array([ 3.59079020e-31, -1.00000000e+00, -1.59012792e-62,  1.20653584e-06]),
array([-8.97697549e-32,  1.00000000e+00, -9.93829951e-64,  9.04901877e-07]),
array([ 2.24424387e-32, -1.00000000e+00, -6.21143719e-65,  6.78676408e-07]),
array([-5.61060968e-33,  1.00000000e+00, -3.88214825e-66,  5.09007306e-07]),
array([ 1.40265242e-33, -1.00000000e+00, -2.42634265e-67,  3.81755479e-07]),
array([-3.50663105e-34,  1.00000000e+00, -1.51646416e-68,  2.86316610e-07]),
array([ 8.76657763e-35, -1.00000000e+00, -9.47790099e-70,  2.14737457e-07]),
array([-2.19164441e-35,  1.00000000e+00, -5.92368812e-71,  1.61053093e-07]),
array([ 5.47911102e-36, -1.00000000e+00, -3.70230508e-72,  1.20789820e-07]),
array([-1.36977775e-36,  1.00000000e+00, -2.31394067e-73,  9.05923648e-08]),
array([ 3.42444439e-37, -1.00000000e+00, -1.44621292e-74,  6.79442736e-08]),
array([-8.56111096e-38,  1.00000000e+00, -9.03883075e-76,  5.09582052e-08]),
array([ 2.14027774e-38, -1.00000000e+00, -5.64926922e-77,  3.82186539e-08]),
array([-5.35069435e-39,  1.00000000e+00, -3.53079326e-78,  2.86639904e-08]),
array([ 1.33767359e-39, -1.00000000e+00, -2.20674579e-79,  2.14979928e-08]),
array([-3.34418397e-40,  1.00000000e+00, -1.37921612e-80,  1.61234946e-08]),
array([ 8.36045993e-41, -1.00000000e+00, -8.62010074e-82,  1.20926210e-08]),
array([-2.09011498e-41,  1.00000000e+00, -5.38756296e-83,  9.06946572e-09]),
array([ 5.22528745e-42, -1.00000000e+00, -3.36722685e-84,  6.80209929e-09]),
array([-1.30632186e-42,  1.00000000e+00, -2.10451678e-85,  5.10157447e-09]),
array([ 3.26580466e-43, -1.00000000e+00, -1.31532299e-86,  3.82618085e-09]),
array([-8.16451165e-44,  1.00000000e+00, -8.22076868e-88,  2.86963564e-09]),
array([ 2.04112791e-44, -1.00000000e+00, -5.13798042e-89,  2.15222673e-09]),
array([-5.10281978e-45,  1.00000000e+00, -3.21123776e-90,  1.61417005e-09]),
array([ 1.27570494e-45, -1.00000000e+00, -2.00702360e-91,  1.21062753e-09]),
array([-3.18926236e-46,  1.00000000e+00, -1.25438975e-92,  9.07970651e-10]),
array([ 7.97315590e-47, -1.00000000e+00, -7.83993595e-94,  6.80977988e-10]),
array([-1.99328898e-47,  1.00000000e+00, -4.89995997e-95,  5.10733491e-10]),
array([ 4.98322244e-48, -1.00000000e+00, -3.06247498e-96,  3.83050118e-10]),
array([-1.24580561e-48,  1.00000000e+00, -1.91404686e-97,  2.87287589e-10]),
array([ 3.11451403e-49, -1.00000000e+00, -1.19627929e-98,  2.15465692e-10]),
array([-7.78628506e-050,  1.00000000e+000, -7.47674556e-100,
1.61599269e-010]),
array([ 1.94657127e-050, -1.00000000e+000, -4.67296597e-101,
1.21199451e-010]),
array([-4.86642816e-051,  1.00000000e+000, -2.92060373e-102,
9.08995886e-011]),
array([ 1.21660704e-051, -1.00000000e+000, -1.82537733e-103,
6.81746915e-011]),
array([-3.04151760e-052,  1.00000000e+000, -1.14086083e-104,
5.11310186e-011]),
array([ 7.60379401e-053, -1.00000000e+000, -7.13038021e-106,
3.83482639e-011]),
array([-1.90094850e-053,  1.00000000e+000, -4.45648763e-107,
2.87611980e-011]),
array([ 4.75237125e-054, -1.00000000e+000, -2.78530477e-108,
2.15708985e-011]),
array([-1.18809281e-054,  1.00000000e+000, -1.74081548e-109,
1.61781739e-011]),
array([ 2.97023203e-055, -1.00000000e+000, -1.08800968e-110,
1.21336304e-011]),
array([-7.42558009e-056,  1.00000000e+000, -6.80006047e-112,
9.10022279e-012]),
array([ 1.85639502e-056, -1.00000000e+000, -4.25003779e-113,
6.82516709e-012]),
array([-4.64098755e-057,  1.00000000e+000, -2.65627362e-114,
5.11887532e-012]),
array([ 1.16024689e-057, -1.00000000e+000, -1.66017101e-115,
3.83915649e-012]),
array([-2.90061722e-058,  1.00000000e+000, -1.03760688e-116,
2.87936737e-012]),
array([ 7.25154305e-059, -1.00000000e+000, -6.48504302e-118,
2.15952553e-012]),
array([-1.81288576e-059,  1.00000000e+000, -4.05315189e-119,
1.61964414e-012]),
array([ 4.53221441e-060, -1.00000000e+000, -2.53321993e-120,
1.21473311e-012]),
array([-1.13305360e-060,  1.00000000e+000, -1.58326246e-121,
9.11049831e-013]),
array([ 2.83263400e-061, -1.00000000e+000, -9.89539035e-123,
6.83287373e-013]),
array([-7.08158501e-062,  1.00000000e+000, -6.18461897e-124,
5.12465530e-013]),
array([ 1.77039625e-062, -1.00000000e+000, -3.86538686e-125,
3.84349148e-013]),
array([-4.42599063e-063,  1.00000000e+000, -2.41586678e-126,
2.88261861e-013]),
array([ 1.10649766e-063, -1.00000000e+000, -1.50991674e-127,
2.16196395e-013]),
array([-2.76624415e-064,  1.00000000e+000, -9.43697963e-129,
1.62147297e-013]),
array([ 6.91561036e-065, -1.00000000e+000, -5.89811227e-130,
1.21610472e-013]),
array([-1.72890259e-065,  1.00000000e+000, -3.68632017e-131,
9.12078543e-014]),
array([ 4.32225648e-066, -1.00000000e+000, -2.30395010e-132,
6.84058908e-014]),
array([-1.08056412e-066,  1.00000000e+000, -1.43996882e-133,
5.13044181e-014]),
array([ 2.70141030e-067, -1.00000000e+000, -8.99980510e-135,
3.84783136e-014]),
array([-6.75352574e-068,  1.00000000e+000, -5.62487818e-136,
2.88587352e-014]),
array([ 1.68838144e-068, -1.00000000e+000, -3.51554887e-137,
2.16440514e-014]),
array([-4.22095359e-069,  1.00000000e+000, -2.19721804e-138,
1.62330385e-014]),
array([ 1.05523840e-069, -1.00000000e+000, -1.37326128e-139,
1.21747789e-014]),
array([-2.63809599e-070,  1.00000000e+000, -8.58288297e-141,
9.13108417e-015]),
array([ 6.59523999e-071, -1.00000000e+000, -5.36430186e-142,
6.84831313e-015]),
array([-1.64881000e-071,  1.00000000e+000, -3.35268866e-143,
5.13623485e-015]),
array([ 4.12202499e-072, -1.00000000e+000, -2.09543041e-144,
3.85217614e-015]),
array([-1.03050625e-072,  1.00000000e+000, -1.30964401e-145,
2.88913210e-015]),
array([ 2.57626562e-073, -1.00000000e+000, -8.18527505e-147,
2.16684908e-015]),
array([-6.44066405e-074,  1.00000000e+000, -5.11579691e-148,
1.62513681e-015]),
array([ 1.61016601e-074, -1.00000000e+000, -3.19737307e-149,
1.21885261e-015]),
array([-4.02541503e-075,  1.00000000e+000, -1.99835817e-150,
9.14139454e-016]),
array([ 1.00635376e-075, -1.00000000e+000, -1.24897385e-151,
6.85604591e-016]),
array([-2.51588439e-076,  1.00000000e+000, -7.80608659e-153,
5.14203443e-016]),
array([ 6.28971098e-077, -1.00000000e+000, -4.87880412e-154,
3.85652582e-016]),
array([-1.57242775e-077,  1.00000000e+000, -3.04925257e-155,
2.89239437e-016]),
array([ 3.93106937e-078, -1.00000000e+000, -1.90578286e-156,
2.16929577e-016]),
array([-9.82767341e-079,  1.00000000e+000, -1.19111429e-157,
1.62697183e-016]),
array([ 2.45691835e-079, -1.00000000e+000, -7.44446429e-159,
1.22022887e-016]),
array([-6.14229588e-080,  1.00000000e+000, -4.65279018e-160,
9.15171655e-017]),
array([ 1.53557397e-080, -1.00000000e+000, -2.90799386e-161,
6.86378741e-017]),
array([-3.83893493e-081,  1.00000000e+000, -1.81749617e-162,
5.14784056e-017]),
array([ 9.59733732e-082, -1.00000000e+000, -1.13593510e-163,
3.86088042e-017]),
array([-2.39933433e-082,  1.00000000e+000, -7.09959439e-165,
2.89566031e-017]),
array([ 5.99833582e-083, -1.00000000e+000, -4.43724650e-166,
2.17174524e-017]),
array([-1.49958396e-083,  1.00000000e+000, -2.77327906e-167,
1.62880893e-017]),
array([ 3.74895989e-084, -1.00000000e+000, -1.73329941e-168,
1.22160670e-017]),
array([-9.37239972e-085,  1.00000000e+000, -1.08331213e-169,
9.16205021e-018]),
array([ 2.34309993e-085, -1.00000000e+000, -6.77070083e-171,
6.87153766e-018]),
array([-5.85774983e-086,  1.00000000e+000, -4.23168802e-172,
5.15365325e-018]),
array([ 1.46443746e-086, -1.00000000e+000, -2.64480501e-173,
3.86523993e-018]),
array([-3.66109364e-087,  1.00000000e+000, -1.65300313e-174,
2.89892995e-018]),
array([ 9.15273411e-088, -1.00000000e+000, -1.03312696e-175,
2.17419746e-018]),
array([-2.28818353e-088,  1.00000000e+000, -6.45704349e-177,
1.63064810e-018]),
array([ 5.72045882e-089, -1.00000000e+000, -4.03565218e-178,
1.22298607e-018]),
array([-1.43011470e-089,  1.00000000e+000, -2.52228261e-179,
9.17239555e-019]),
array([ 3.57528676e-090, -1.00000000e+000, -1.57642663e-180,
6.87929666e-019]),
array([-8.93821690e-091,  1.00000000e+000, -9.85266645e-182,
5.15947249e-019]),
array([ 2.23455423e-091, -1.00000000e+000, -6.15791653e-183,
3.86960437e-019]),
array([-5.58638556e-092,  1.00000000e+000, -3.84869783e-184,
2.90220328e-019]),
array([ 1.39659639e-092, -1.00000000e+000, -2.40543615e-185,
2.17665246e-019]),
array([-3.49149098e-093,  1.00000000e+000, -1.50339759e-186,
1.63248934e-019]),
array([ 8.72872744e-094, -1.00000000e+000, -9.39623494e-188,
1.22436701e-019]),
array([-2.18218186e-094,  1.00000000e+000, -5.87264684e-189,
9.18275256e-020]),
array([ 5.45545465e-095, -1.00000000e+000, -3.67040428e-190,
6.88706442e-020]),
array([-1.36386366e-095,  1.00000000e+000, -2.29400267e-191,
5.16529832e-020]),
array([ 3.40965916e-096, -1.00000000e+000, -1.43375167e-192,
3.87397374e-020]),
array([-8.52414789e-097,  1.00000000e+000, -8.96094794e-194,
2.90548030e-020]),
array([ 2.13103697e-097, -1.00000000e+000, -5.60059246e-195,
2.17911023e-020]),
array([-5.32759243e-098,  1.00000000e+000, -3.50037029e-196,
1.63433267e-020]),
array([ 1.33189811e-098, -1.00000000e+000, -2.18773143e-197,
1.22574950e-020]),
array([-3.32974527e-099,  1.00000000e+000, -1.36733214e-198,
9.19312127e-021]),
array([ 8.32436318e-100, -1.00000000e+000, -8.54582590e-200,
6.89484095e-021]),
array([-2.08109079e-100,  1.00000000e+000, -5.34114119e-201,
5.17113071e-021]),
array([ 5.20272698e-101, -1.00000000e+000, -3.33821324e-202,
3.87834804e-021]),
array([-1.30068175e-101,  1.00000000e+000, -2.08638328e-203,
2.90876103e-021]),
array([ 3.25170437e-102, -1.00000000e+000, -1.30398955e-204,
2.18157077e-021]),
array([-8.12926091e-103,  1.00000000e+000, -8.14993467e-206,
1.63617808e-021]),
array([ 2.03231523e-103, -1.00000000e+000, -5.09370917e-207,
1.22713356e-021]),
array([-5.08078807e-104,  1.00000000e+000, -3.18356823e-208,
9.20350169e-022]),
array([ 1.27019702e-104, -1.00000000e+000, -1.98973014e-209,
6.90262626e-022]),
array([-3.17549254e-105,  1.00000000e+000, -1.24358134e-210,
5.17696970e-022]),
array([ 7.93873136e-106, -1.00000000e+000, -7.77238338e-212,
3.88272727e-022]),
array([-1.98468284e-106,  1.00000000e+000, -4.85773961e-213,
2.91204546e-022]),
array([ 4.96170710e-107, -1.00000000e+000, -3.03608726e-214,
2.18403409e-022]),
array([-1.24042678e-107,  1.00000000e+000, -1.89755454e-215,
1.63802557e-022]),
array([ 3.10106694e-108, -1.00000000e+000, -1.18597158e-216,
1.22851918e-022]),
array([-7.75266735e-109,  1.00000000e+000, -7.41232240e-218,
9.21389382e-023]),
array([ 1.93816684e-109, -1.00000000e+000, -4.63270150e-219,
6.91042037e-023]),
array([-4.84541709e-110,  1.00000000e+000, -2.89543844e-220,
5.18281528e-023]),
array([ 1.21135427e-110, -1.00000000e+000, -1.80964902e-221,
3.88711146e-023]),
array([-3.02838568e-111,  1.00000000e+000, -1.13103064e-222,
2.91533359e-023]),
array([ 7.57096420e-112, -1.00000000e+000, -7.06894150e-224,
2.18650019e-023]),
array([-1.89274105e-112,  1.00000000e+000, -4.41808844e-225,
1.63987515e-023]),
array([ 4.73185263e-113, -1.00000000e+000, -2.76130527e-226,
1.22990636e-023]),
array([-1.18296316e-113,  1.00000000e+000, -1.72581580e-227,
9.22429770e-024]),
array([ 2.95740789e-114, -1.00000000e+000, -1.07863487e-228,
6.91822327e-024]),
array([-7.39351973e-115,  1.00000000e+000, -6.74146795e-230,
5.18866745e-024]),
array([ 1.84837993e-115, -1.00000000e+000, -4.21341747e-231,
3.89150059e-024]),
array([-4.62094983e-116,  1.00000000e+000, -2.63338592e-232,
2.91862544e-024]),
array([ 1.15523746e-116, -1.00000000e+000, -1.64586620e-233,
2.18896908e-024]),
array([-2.88809364e-117,  1.00000000e+000, -1.02866637e-234,
1.64172681e-024]),
array([ 7.22023411e-118, -1.00000000e+000, -6.42916484e-236,
1.23129511e-024]),
array([-1.80505853e-118,  1.00000000e+000, -4.01822803e-237,
9.23471331e-025]),
array([ 4.51264632e-119, -1.00000000e+000, -2.51139252e-238,
6.92603499e-025]),
array([-1.12816158e-119,  1.00000000e+000, -1.56962032e-239,
5.19452624e-025]),
array([ 2.82040395e-120, -1.00000000e+000, -9.81012702e-241,
3.89589468e-025])]

The last entry printed above is $x^{20}$. Observe that the entry with the largest absolute value is the second entry, and the other entries are very close to zero. Thus, we have found an eigenvector of the form $(0,\pm 1,0,0)$.

Since $x^{20}$ is a unit vector close to an eigenvector, the associated eigenvalue can be (nearly) recovered by computing $$x^{20} \cdot (D x^{20}).$$ (This works because $D x^{20} \approx \lambda x^{20}$, so $$x^{20} \cdot (D x^{20}) \approx x^{20} \cdot (\lambda x^{20}) = \lambda (x^{20} \cdot x^{20})= \lambda.$$

In :
x20 = x_list
x20

Out:
array([ 6.62336104e-12, -9.99927876e-01, -5.41053467e-24,  1.20101511e-02])
In :
eigvalue = x20 @ (D @ x20)
eigvalue

Out:
-3.9989902938865516

Thus, we have an approximate eigenvalue--eigenvector pair (eigvalue, x20).

### Diagonalizable matrices¶

Of course, it is easy to find eigenvectors in a diagonal matrix. We looked at them because the method we used above makes it easy to see how iteration works there.

A slightly more general setting is the case of diagonalizable matrices. A $n \times n$ matrix $A$ is diagonalizable if it has the form $$A = P D P^{-1},$$ where $P$ is an invertible $n \times n$ matrix and $D$ is a diagonal matrix as above. In this case, the diagonal entries $d_j$ of $D$ are eigenvalues associated to eigenvectors of the form $v^j = P e^j$. This is because $$A v^j = P D P^{-1} P e^j = P D e^j = P d_j e^j = d_j P e^j = d_j v^j.$$

For a diagonalizable matrix, we can apply the same algorithm to find the dominant eigenvalue (the eigenvalue with larger absolute value than any other).

It is a well-known and very useful fact that symmetric matrices are diagonalizable. A symmetric matrix is a matrix $A$ which equals its transpose.

We will use this to give an example of an $n \times n$ matrix with $n=4$.

In :
n = 4


We computed a random $4 \times 4$ matrix $A$ with entries in the interval $(-1,1)$ using the command

A = (2*np.random.random(n**2)-1).reshape(4,4)


Below we show what we ended up with:

In :
(2*np.random.random(n**2)-1).reshape(4,4)

Out:
array([[-0.4070785 , -0.75309184, -0.2579446 , -0.66507789],
[ 0.09968207,  0.67026957, -0.08113898,  0.02238426],
[-0.03040067,  0.08939184,  0.80037572, -0.57031027],
[-0.77730174,  0.24388915, -0.92012184,  0.75629579]])
In :
A = np.array([[-0.25171678,  0.58924155, -0.32522934,  0.92260052],
[ 0.58924155,  0.99124496, -0.85124213, -0.93359258],
[-0.32522934, -0.85124213,  0.74766801, -0.79890941],
[ 0.92260052, -0.93359258, -0.79890941,  0.66430218]])
A

Out:
array([[-0.25171678,  0.58924155, -0.32522934,  0.92260052],
[ 0.58924155,  0.99124496, -0.85124213, -0.93359258],
[-0.32522934, -0.85124213,  0.74766801, -0.79890941],
[ 0.92260052, -0.93359258, -0.79890941,  0.66430218]])

To make the matrix symmetric, we fix it by setting $a_{j,i}=a_{i,j}$ for $j>i$.

In :
for i in range(n):
for j in range(i+1,n):
A[j,i]=A[i,j]
A

Out:
array([[-0.25171678,  0.58924155, -0.32522934,  0.92260052],
[ 0.58924155,  0.99124496, -0.85124213, -0.93359258],
[-0.32522934, -0.85124213,  0.74766801, -0.79890941],
[ 0.92260052, -0.93359258, -0.79890941,  0.66430218]])

Now the matrix is symmetric. We also choose a random initial vector using

x = 2*np.random.random(n)-1


Again, we just write what we computed below.

In :
x = np.array([-0.79842236, -0.50141247,  0.21444716,  0.64910438])
x

Out:
array([-0.79842236, -0.50141247,  0.21444716,  0.64910438])

Now we will iterate it for a while, continually rescaling so that the vector has unit length. We define $x_0 = x / \|x\|$ and then define $$x_{k+1} = \frac{1}{\|A x_k\|} A x_k \quad \text{for k \geq 0}.$$ We'll repeat this operation $100$ times, discarding intermediate values.

In :
k = 0
x_k = x / np.linalg.norm(x)
for i in range(1000):
temp = A @ x_k
k += 1
x_k = temp / np.linalg.norm(temp)
print("x_{} = {}".format(k, x_k))

x_1000 = [-0.37108227 -0.55544272  0.68572761 -0.28906571]


Note that because we are continually dividing by the norm, $x_{100}$ is a unit vector:

In :
np.linalg.norm(x_k)

Out:
1.0

Since $\|x_k\|=1$, assuming $x_k$ is close to a eigenvector, the associated eigenvalue can be computed with:

In :
eigvalue = x_k @ A @ x_k
eigvalue

Out:
1.9499535748337486

To verify that $x_{100}$ is nearly an eigenvector with eigenvalue $\lambda$, we could compute $$A x^{100} - \lambda x^{100}$$ and check that all entries are small. We do that below:

In :
A @ x_k - eigvalue * x_k

Out:
array([0., 0., 0., 0.])

The small values above suggest that (eigvalue, x_k) is approximately an eigenvalue–eigenvector pair.

We can improve it further by iterating more.

In :
for i in range(100):
temp = A @ x_k
k += 1
x_k = temp / np.linalg.norm(temp)
print("x_{} = {}".format(k, x_k))

x_1100 = [-0.37108227 -0.55544272  0.68572761 -0.28906571]

In :
eigvalue = x_k @ A @ x_k
eigvalue

Out:
1.9499535748337486
In :
A @ x_k - eigvalue * x_k

Out:
array([0., 0., 0., 0.])

The above difference is even smaller. Continuing further:

In :
for i in range(200):
temp = A @ x_k
k += 1
x_k = temp / np.linalg.norm(temp)
print("x_{} = {}".format(k, x_k))

x_1300 = [-0.37108227 -0.55544272  0.68572761 -0.28906571]

In :
eigvalue = x_k @ A @ x_k
eigvalue

Out:
1.9499535748337486
In :
A @ x_k - eigvalue * x_k

Out:
array([0., 0., 0., 0.])

The above difference is on the order of round off error, so we can't expect to do much better. We store off the eigenvector:

In :
eigenvector = x_k
eigenvector

Out:
array([-0.37108227, -0.55544272,  0.68572761, -0.28906571])

### The error in eigenvalue and eigenvector approximation¶

Lets look at the error in computation of eigenvalues and eigenvectors. Here we make a list of approximate eigenvalues, computed as $$x_k \cdot (A x^k).$$ We'll store these in a list.

In :
k_list = []
approx_eigenvalues = []
approx_eigenvectors = []
k = 0
x_k = x / np.linalg.norm(x)
k_list.append(k)
approx_eigenvalues.append(x_k @ A @ x_k)
approx_eigenvectors.append(x_k)
for i in range(100):
temp = A @ x_k
k += 1
x_k = temp / np.linalg.norm(temp)
k_list.append(k)
approx_eigenvalues.append(x_k @ A @ x_k)
approx_eigenvectors.append(x_k)


Below we compute the error in our eigenvalue approximations as a function of $k$.

In :
# Convert k_list and approx_eigenvalues to numpy arrays
k_list = np.array(k_list)
approx_eigenvalues = np.array(approx_eigenvalues)
plt.plot(k_list, eigvalue - approx_eigenvalues, "ob", label="approximate eigenvalues")
plt.xlabel("k")
plt.ylabel("eigenvalue approximate")
plt.title("Plot of eigenvalue approximates")
plt.show() To better see what is going on, we can plot the above graph on a log scale.

In :
# Convert k_list and approx_eigenvalues to numpy arrays
k_list = np.array(k_list)
approx_eigenvalues = np.array(approx_eigenvalues)
plt.plot(k_list, eigvalue - approx_eigenvalues, "ob", label="approximate eigenvalues")
plt.xlabel("k")
plt.ylabel("eigenvalue approximate")
plt.title("Plot of eigenvalue approximates (log scale)")
plt.yscale("log")
plt.show() The fact that the graph is asymptotic to a downward sloping line means that our eigenvalues approximates are approaching the true eigenvalue exponentially quickly (with exponent related to the slope of the line).

Now we turn our attention to our approximate eigenvectors $x_k$. Since these values are approaching eigenvector, we can look at the norm of the difference of eigenvector and $x^k$. This is our error in eigenvector approximation.

In :
eigenvector_error = []
for x_k in approx_eigenvectors:
eigenvector_error.append(np.linalg.norm(eigenvector - x_k))
# Convert to a numpy array:
eigenvector_error = np.array(eigenvector_error)


We plot these errors:

In :
plt.plot(k_list, eigenvector_error, "ob", label="eigenvector error")
plt.xlabel("k")
plt.ylabel("eigenvalue approximate")
plt.title("Plot of eigenvector errors")
plt.show() We switch to a log scale:

In :
plt.plot(k_list, eigenvector_error, "ob", label="eigenvector error")
plt.xlabel("k")
plt.ylabel("eigenvalue approximate")
plt.title("Plot of eigenvector errors (log scale)")
plt.yscale("log")
plt.show() Again, we can see that the error is decaying exponentially.

### Finding a dominant eigenvalue¶

Here we rigorously spell out the algorithm we have been describing above.

It is useful to know that a matrix $A$ and its transpose $A^T$ are conjugate. (That is there is an invertible matrix $P$ such that $A^T=P A P^{-1}$.) Conjugate matrices have the same eigenvalues and the same multiplicity of eigenvectors.

#### Theorem: The power method of finding a dominant eigenvalue.

Suppose $\lambda_{\max}$ is the dominant eigenvalue of the square matrix $A$, i.e., $\lambda_{\max}$ is an eigenvalue and all other eigenvalues satisfy $|\lambda|<|\lambda_{\max}|$. Suppose also that the algebraic multiplicity of $\lambda_{\max}$ is one. Suppose $v$ and $w$ are unit vectors satisfying $A v = \lambda_\max v$ and $A^T w = \lambda_\max w$. Let $x$ be any vector such that $w \cdot x \neq 0$. Define the sequence of vectors by $x^0 = x/\|x\|$ and $$x^{k+1} = \frac{A x^k}{\|A x^k\|} \quad \text{for k \geq 0}.$$ Then $x^k$ converges exponentially to $\pm v$, and the sequence of eigenvalue approximates $x^k \cdot (A x^k)$ converges exponentially to $\lambda_\max$.

Remark: The condition that $w \cdot x=0$ means you can't use just any initial vector $x$. Also, of course we don't want to assume we know $w$. But, it is very unlikely if you choose $x$ at random that $w \cdot x=0$. This is why we have been using random vectors for $x$.

The algorithm in the Theorem above is basically the same as the Algorithm 8 of TAK § 4.6.

## Finding the eigenvalue with smallest magnitude¶

Suppose $A$ is invertible. Observe that if $A v = \lambda v$ then $$A^{-1} v = \frac{1}{\lambda} v.$$ This means that we can apply the power method with $A^{-1}$ to find the eigenvalue with smallest absolute value.

However, instead of using the inverse, we use $LU$ factorization. The algorithm is spelled out in Algorithm 9 of TAK § 4.6.

Let's find the smallest eigenvalue of the matrix $A$ as above.

In :
A

Out:
array([[-0.25171678,  0.58924155, -0.32522934,  0.92260052],
[ 0.58924155,  0.99124496, -0.85124213, -0.93359258],
[-0.32522934, -0.85124213,  0.74766801, -0.79890941],
[ 0.92260052, -0.93359258, -0.79890941,  0.66430218]])

First we compute the $LU$ factorization. We'll use the linalg module from scipy, and use a slightly different function so we can make use of the packages abilities to solve equations.

In :
LU_data = linalg.lu_factor(A)
LU_data

Out:
(array([[ 0.92260052, -0.93359258, -0.79890941,  0.66430218],
[ 0.63867463,  1.58750686, -0.34099895, -1.35786553],
[-0.27283399,  0.21072398, -0.47134232,  1.38997956],
[-0.35251372, -0.74352203, -0.4508427 , -0.94767457]]),
array([3, 1, 3, 3], dtype=int32))

Our variable LU_data stores the information of the $PLU$-factorization. The pair consists of a matrix storing $L+U-I$ and a vector representing the permutation matrix $P$. You can find out more by typing the following command into jupyter:

linalg.lu_factor?


Recall that we use $LU$-factorization to efficiently solve the equation $Ay=b$ for $y$. The linalg module in scipy has a function that does this for us. For example:

In :
b = np.array([1, 2, 3, 4])
y = linalg.lu_solve(LU_data, b)
y

Out:
array([-18.69186494,  -9.20001546, -20.22280393,  -5.26888264])

To check that it worked observe:

In :
A @ y

Out:
array([1., 2., 3., 4.])

Here we apply the power method to find the smallest eigenvalue and eigenvector pair. We'll start with the same random vector $x$ as before.

In :
x

Out:
array([-0.79842236, -0.50141247,  0.21444716,  0.64910438])

We take $x^0$ to be $x$ rescaled to have unit length:

In :
k = 0
x_k = x / np.linalg.norm(x)


Then we iterate backward $100$ times.

In :
for i in range(100):
temp = linalg.lu_solve(LU_data, x_k)
k += 1
x_k = temp / np.linalg.norm(temp)
print("x_{} = {}".format(k, x_k))

x_100 = [-0.69440134 -0.26041893 -0.65244138 -0.15591345]


Again because x_100 is a unit vector, we can find the associated eigenvalue by computing the following:

In :
eigvalue = x_k @ A @ x_k
eigvalue

Out:
-0.1291617015920534

To see that $x_100$ is an approximate eigenvalue observe:

In :
A @ x_k - eigvalue * x_k

Out:
array([-8.32667268e-17, -3.46944695e-17,  9.71445147e-17, -1.04083409e-16])

The above is so small that the quantity above looks like round off error.

In :
A

Out:
array([[-0.25171678,  0.58924155, -0.32522934,  0.92260052],
[ 0.58924155,  0.99124496, -0.85124213, -0.93359258],
[-0.32522934, -0.85124213,  0.74766801, -0.79890941],
[ 0.92260052, -0.93359258, -0.79890941,  0.66430218]])

So that we can use it later, we organize the above algorithm into a function. This essentially implements Algorithm 9 of TAK.

In :
def smallest_magnitude_eigenvalue(A, epsilon):
''' This function returns a pair consisting of an eigenvalue and an
eigenvector. The eigenvalue returned is the eigenvalue of smallest
absolute value. We use the power method and LU-factorization iterating
until two consequtive eigenvalue estimates are within epsilon of each other.
'''
n = A.shape
assert A.shape == (n,n), "A should be a square matrix."
x = 2*np.random.random(n)-1
LU_data = linalg.lu_factor(A)
k = 0
x_k = x / np.linalg.norm(x)
eigenvalue_old_est = float('inf')
eigenvalue_est = x_k @ A @ x_k
while abs(eigenvalue_est - eigenvalue_old_est) >= epsilon:
temp = linalg.lu_solve(LU_data, x_k)
k += 1
x_k = temp / np.linalg.norm(temp)
eigenvalue_old_est = eigenvalue_est
eigenvalue_est = x_k @ A @ x_k
return eigenvalue_est, x_k


We test it using the same matrix as before.

In :
eigenvalue, eigenvector = smallest_magnitude_eigenvalue(A, 10**-8)

In :
eigenvalue

Out:
-0.12916170159169746
In :
A @ eigenvector - eigenvalue * eigenvector

Out:
array([-3.47415713e-08, -2.33187480e-07,  4.45700239e-07, -1.32087313e-06])

It seemed to work.

## Finding other eigenvalues¶

To find other eigenvalues we can use the following trick. If $A$ is a matrix and $A v = \lambda v$ then we also have $$(A - cI) v = (\lambda-c)v.$$ That is $A$ and $A-c I$ have the same eigenvectors, and eigenvalues are shifted by subtracting $c$.

Let's use this to find the other eigenvalues of $A$. We first found a largest magnitude eigenvalue–eigenvector pair:

In :
eigvalue_0 = 1.9499535745247238
vect_0 = np.array([-0.37108228, -0.55544271,  0.68572761, -0.28906573])


We found the smallest magnitude pair above. We repeat the calculation here:

In :
eigvalue_1, vect_1 = smallest_magnitude_eigenvalue(A, 10**-12)
eigvalue_1

Out:
-0.1291617015920593

Since real symmetric matrices have all real eigenvalues, the other eigenvalues must be in the union of intervals $[-1.949\ldots, -0.1291\ldots] \cup [0.1291\ldots, 1.949\ldots]$. We can look for eigenvalues in the center of these intervals. First we set c to be the center of the interval $[-1.949\ldots, -0.1291\ldots]$:

In :
c = (-eigvalue_0 + eigvalue_1)/2
c

Out:
-1.0395576380583915

To find the eigenvalue, eigenvector pair nearest to $c$, we can find the smallest eigenvalue for $A-cI$.

In :
temp_eigvalue, vect = smallest_magnitude_eigenvalue(A - c*np.identity(4), 10**-12)


Because of the shift, the actual eigenvalue should be obtained by adding $c$.

In :
eigvalue = temp_eigvalue + c
eigvalue

Out:
-1.4548040879663942

Let's check if it worked.

In :
A@vect - eigvalue*vect

Out:
array([2.09390929e-07, 7.85272024e-08, 1.96738310e-07, 4.70144496e-08])

These numbers are small, so it seemed to work. Let's store off our eigenvalue–eigenvector pair.

In :
eigvalue_2 = eigvalue
vect_2 = vect


Let's check the other interval $[0.1291\ldots, 1.949\ldots]$ now. We set the midpoint to $c$:

In :
c = (eigvalue_0 - eigvalue_1)/2


Now we look for the eigenvalue closest to $c$.

In :
temp_eigvalue, vect = smallest_magnitude_eigenvalue(A - c*np.identity(4), 10**-12)
eigvalue = temp_eigvalue + c
eigvalue

Out:
1.785510592487924
In :
A@vect - eigvalue*vect

Out:
array([-2.08027915e-07, -3.11232349e-07,  3.84046759e-07, -1.61974643e-07])

Again it seemed to work. We found a final eigenvalue eigenvector pair:

In :
eigvalue_3 = eigvalue
vect_3 = vect


Since $A$ is $4 \times 4$, we found all the eigenvalue–eigenvector pairs. To check our work, we verify that we have diagonalized $A$. If we create a diagonal matrix $D$ with entries given by the eigenvalues and a matrix $P$ with columns given by the eigenvectors (in the same order), we should have the equation $$A = P D P^{-1}.$$

In :
D = np.diag([eigvalue_0, eigvalue_1, eigvalue_2, eigvalue_3])
P = np.array([vect_0, vect_1, vect_2, vect_3]).transpose()


To check that the equation is satisfied, we can check that $$AP = PD.$$ (Though, this doesn't check that $P$ is invertible.) To check this equation, we observe that $AP - PD$ has all small entries.

In :
A @ P - P @ D

Out:
array([[-1.66582370e-09,  1.60288668e-08,  2.84544605e-07,
-2.08027915e-07],
[ 4.69555528e-09, -1.53296454e-08,  1.06711865e-07,
-3.11232349e-07],
[ 5.18975019e-09, -7.18850650e-09,  2.67350792e-07,
3.84046759e-07],
[ 5.42714196e-09, -1.57026744e-08,  6.38886958e-08,
-1.61974643e-07]])

It seems to work! The easiest way to check that $P$ is invertible is to see that the columns of $P$ are orthonormal. By construction the columns are unit vectors. To see they are pairwise orthogonal we compute their dot products.

In :
for i in range(4):
for j in range(i+1, 4):
print(P[:,i] @ P[:,j])

3.1141575637660956e-09
2.325384745871517e-09
3.4082679064573185e-06
-3.3021975581748464e-07
-9.017504798425335e-10
-2.220446049250313e-16


All dot products of columns are nearly zero, so the columns form an (approximate) orthonormal basis for ${\mathbb R}^4$. We conclude that $P$ is invertible. (Any collection of non-zero pairwise orthogonal vectors is linearly independent, since the projection of any vector to the span of the others is the zero vector.)

Remark: It is a theorem that symmetric matrices are orthogonally diagonalizable. If all eigenvalues of a symmetric matrix have algebraic multiplicity one, then whenever $A=PDP^{-1}$, the matrix $P$ will have orthogonal columns.