In [1]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
sns.set_context('talk')
In [2]:
np.random.seed(2222)
T, _ = np.linalg.qr(np.random.rand(5,5))
A = T.T @ np.diag([5,4,3,2,1]) @ T

w, v = np.linalg.eig(A)
np.set_printoptions(precision=2)
print(w)
for ww in w:
    print(ww, np.abs(ww))
print(v)
[5. 1. 2. 4. 3.]
5.000000000000001 5.000000000000001
0.9999999999999983 0.9999999999999983
1.999999999999996 1.999999999999996
3.9999999999999947 3.9999999999999947
2.999999999999998 2.999999999999998
[[ 0.58 -0.29 -0.57  0.5  -0.03]
 [-0.23 -0.6   0.49  0.45 -0.37]
 [ 0.31  0.52  0.14  0.06 -0.78]
 [-0.6  -0.11 -0.64 -0.13 -0.44]
 [-0.39  0.52 -0.04  0.72  0.24]]
In [3]:
X = np.random.rand(5,3)
for i in range(50):
    Q, R = np.linalg.qr(X, mode='reduced')
    X = A @ Q
    
    print(np.diag(X.conj().T @ (A @ X)) / np.diag(X.conj().T @ X))
[3.6  4.02 4.14]
[4.   4.74 3.71]
[4.3  4.75 3.5 ]
[4.52 4.63 3.37]
[4.67 4.5  3.25]
[4.78 4.38 3.16]
[4.85 4.27 3.1 ]
[4.9  4.19 3.06]
[4.94 4.13 3.03]
[4.96 4.09 3.02]
[4.97 4.06 3.01]
[4.98 4.04 3.01]
[4.99 4.03 3.  ]
[4.99 4.02 3.  ]
[5.   4.01 3.  ]
[5.   4.01 3.  ]
[5. 4. 3.]
[5. 4. 3.]
[5. 4. 3.]
[5. 4. 3.]
[5. 4. 3.]
[5. 4. 3.]
[5. 4. 3.]
[5. 4. 3.]
[5. 4. 3.]
[5. 4. 3.]
[5. 4. 3.]
[5. 4. 3.]
[5. 4. 3.]
[5. 4. 3.]
[5. 4. 3.]
[5. 4. 3.]
[5. 4. 3.]
[5. 4. 3.]
[5. 4. 3.]
[5. 4. 3.]
[5. 4. 3.]
[5. 4. 3.]
[5. 4. 3.]
[5. 4. 3.]
[5. 4. 3.]
[5. 4. 3.]
[5. 4. 3.]
[5. 4. 3.]
[5. 4. 3.]
[5. 4. 3.]
[5. 4. 3.]
[5. 4. 3.]
[5. 4. 3.]
[5. 4. 3.]

QR

In [4]:
X = A.copy()
Qall = np.eye(5)
for i in range(10):
    Q, R = np.linalg.qr(X)
    X = R @ Q
    
    np.set_printoptions(precision=2)
    print(np.diag(X))
    
    #Qall = Qall @ Q
    #print(Qall)
[4.4  3.58 3.08 2.52 1.43]
[4.71 4.   3.12 2.14 1.03]
[4.83 4.07 3.06 2.04 1.  ]
[4.89 4.07 3.03 2.02 1.  ]
[4.93 4.05 3.02 2.01 1.  ]
[4.95 4.04 3.01 2.   1.  ]
[4.97 4.03 3.01 2.   1.  ]
[4.98 4.02 3.   2.   1.  ]
[4.99 4.01 3.   2.   1.  ]
[4.99 4.01 3.   2.   1.  ]
In [5]:
from matplotlib.colors import LogNorm
In [7]:
A = np.random.rand(10,10)
A = A + A.T
X = A.copy()

ct = 0
f, ax = plt.subplots(1,4,sharey=True,figsize=(12,6))
for i in range(88):
    Q, R = np.linalg.qr(X)
    X = R @ Q
    
    if i % 22 == 0:
        
        I, J = np.where(np.abs(X) < 1e-13)
        Xtmp = X.copy()
        Xtmp[I,J] = 0.0
        im = ax[ct].imshow(np.abs(Xtmp), cmap=plt.cm.Blues,
                   norm=LogNorm())
        ax[ct].axis('off')
        ct += 1
        
f.colorbar(im, ax=ax.ravel().tolist(), shrink=0.95)
Out[7]:
<matplotlib.colorbar.Colorbar at 0x11cf42d10>