# QR Iteration¶

MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
In [6]:
import numpy as np
import numpy.linalg as la

np.set_printoptions(linewidth=120)


Make a matrix with given eigenvalues:

In [7]:
n = 5

np.random.seed(70)
eigvecs = np.random.randn(n, n)
eigvals = np.sort(np.random.randn(n))

A = np.dot(la.solve(eigvecs, np.diag(eigvals)), eigvecs)
print(eigvals)

[-1.3657822  -0.78460489 -0.08829521  0.30824369  0.52110266]


## Unshifted QR¶

In [121]:
X = A
Qall = np.eye(n)

In [142]:
Q, R = la.qr(X)
X = R @ Q

Qall = Qall @ Q

print(X)

[[-1.36578253e+00  8.75139040e-02  3.03786794e+00  2.62459350e-01  2.97606186e-01]
[-2.24915745e-06 -7.84620834e-01 -4.58283345e-01  1.38868353e+00 -2.45154950e-01]
[ 1.07650630e-09  4.64043042e-05  5.21117944e-01  1.51976500e-01 -7.37805721e-02]
[ 1.84131018e-14  8.27020225e-10  1.39850164e-06  3.08244689e-01  1.76856522e-01]
[-4.46854645e-26  4.39962637e-21 -5.92936598e-17  3.37890623e-12 -8.82952134e-02]]

In [169]:
np.tril(X, -1)

Out[169]:
array([[ 0.00000000e+000,  0.00000000e+000,  0.00000000e+000,  0.00000000e+000,  0.00000000e+000],
[-3.53477485e-033,  0.00000000e+000,  0.00000000e+000,  0.00000000e+000,  0.00000000e+000],
[-3.72649019e-043,  8.68015875e-012,  0.00000000e+000,  0.00000000e+000,  0.00000000e+000],
[ 1.80007802e-085,  1.09679232e-053, -1.15248643e-043,  0.00000000e+000,  0.00000000e+000],
[ 3.51450476e-152, -2.20139581e-120,  8.11711939e-110,  5.61178424e-068,  0.00000000e+000]])
In [170]:
la.norm(np.tril(X, -1))

Out[170]:
8.680158751671073e-12
In [143]:
la.norm(A - Qall @ X @ Qall.T, 2) / la.norm(A, 2)

Out[143]:
1.572363044898993e-15

## Shifted QR¶

In [145]:
X = A
Qall = np.eye(n)

In [164]:
i = -4
sigma = X[i,i]
Q, R = la.qr(X - sigma*np.eye(n))
X = R @ Q + sigma*np.eye(n)

Qall = Qall @ Q

print(X)

[[-1.36578220e+000  8.74081894e-002 -7.91894537e-001  1.33958935e+000 -2.63903577e+000]
[-3.53477485e-033 -7.84604886e-001  8.67481345e-001 -1.12831918e+000 -4.15900506e-001]
[-3.72649019e-043  8.68015875e-012 -8.82952134e-002 -1.96984053e-001  2.33745979e-002]
[ 1.80007802e-085  1.09679232e-053 -1.15248643e-043  3.08243691e-001 -1.43127333e-001]
[ 3.51450476e-152 -2.20139581e-120  8.11711939e-110  5.61178424e-068  5.21102657e-001]]


To compare convergence speed, count iterations until left-of-diagonal entries decay below $10^{-10}$.

In [172]:
la.norm(np.tril(X, -1))

Out[172]:
8.680158751671073e-12
In [173]:
la.norm(A - Qall @ X @ Qall.T, 2) / la.norm(A, 2)

Out[173]:
1.5498995078019794e-15
In [ ]: