from __future__ import division
import numpy as np
import numpy.linalg as la
n = 6
if 1:
np.random.seed(70)
eigvecs = np.random.randn(n, n)
eigvals = np.sort(np.random.randn(n))
# Uncomment for near-duplicate largest-magnitude eigenvalue
# eigvals[1] = eigvals[0] + 1e-3
A = np.dot(la.solve(eigvecs, np.diag(eigvals)), eigvecs)
print eigvals
else:
# Complex eigenvalues
np.random.seed(40)
A = np.random.randn(n, n)
print la.eig(A)[0]
x0 = np.random.randn(n)
x = x0
# Run this cell in-place (Ctrl-Enter) many times.
x = np.dot(A, x)
x
x = x0/la.norm(x0)
# Run this cell in-place (Ctrl-Enter) many times.
# x is unit-length here
x = np.dot(A, x)
nrm = la.norm(x)
x = x/nrm
print nrm
print x
What if we want smallest, not largest?
x = x0/la.norm(x0)
# Run this cell in-place (Ctrl-Enter) many times.
x = ...
nrm = la.norm(x)
x = x/nrm
print 1/nrm
print x
(Edit this cell for solution.)
\(\uparrow\) Inverse iteration
How do we use the Rayleigh quotient?
x = x0/la.norm(x0)
# Run this cell in-place (Ctrl-Enter) many times.
sigma = np.dot(x, np.dot(A, x))/np.dot(x, x)
x = ...
x = x/la.norm(x)
print sigma
print x
(Edit this cell for solution.)
\(\uparrow\) Rayleigh quotient iteration