In [1]:
from __future__ import division

import numpy as np
import numpy.linalg as la
In [2]:
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]

In [3]:
X = np.random.randn(n, n)
In [51]:
# Run this cell in-place (Ctrl-Enter) many times.

Q, R = la.qr(X)
X = np.dot(A, Q)
print Q
[[-0.36366705 -0.24462159  0.39052837  0.06435026 -0.8070026 ]
 [-0.42943052 -0.63956476  0.08336455 -0.49910706  0.3879289 ]
 [-0.040959   -0.42224061 -0.83670032  0.25124759 -0.23841653]
 [ 0.80079022 -0.35639657  0.08781549 -0.40630442 -0.24273785]
 [-0.20098031  0.47519633 -0.36436102 -0.72009898 -0.28721745]]

In [52]:
la.norm(
    np.dot(np.dot(Q, R), Q.T)
    - A)
Out[52]:
3.1730296900420734e-08
In [53]:
np.set_printoptions(linewidth=100)
np.dot(np.dot(Q.T, A), Q)
Out[53]:
array([[ -1.36578220e+00,   8.74082014e-02,   3.03787100e+00,  -2.62444769e-01,  -2.97605237e-01],
       [  1.62514446e-12,  -7.84604888e-01,  -4.58332378e-01,  -1.38868012e+00,   2.45153479e-01],
       [  1.90819582e-16,   5.14360649e-09,   5.21102659e-01,  -1.52024449e-01,   7.37881218e-02],
       [  6.24500451e-17,   2.22044605e-16,  -2.84249013e-12,   3.08243691e-01,   1.76857006e-01],
       [ -3.70363462e-16,   5.72458747e-17,   4.12864187e-16,   2.46330734e-16,  -8.82952134e-02]])
In []: