1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| import numpy as np import matplotlib.pyplot as plt
e1 = np.array([1, 0]) e2 = np.array([0, 1])
v1 = np.array([np.sqrt(2)/2, -np.sqrt(2)/2]) v2 = np.array([np.sqrt(2)/2, np.sqrt(2)/2])
V = np.column_stack([v1, v2]) Lambda = np.diag([2, 0.5]) VT = V.T
A = V @ Lambda @ VT
Ae1 = A.dot(e1) Ae2 = A.dot(e2)
theta = np.linspace(0, 2*np.pi, 400) circle = np.vstack([np.cos(theta), np.sin(theta)]) ellipse = A.dot(circle)
Av1 = A.dot(v1) Av2 = A.dot(v2)
fig, axes = plt.subplots(1, 2, figsize=(12, 6))
ax = axes[0] ax.set_title("Spectral Decomposition from Standard Basis") ax.set_xlim(-2, 2) ax.set_ylim(-2, 2) ax.axhline(0, color='gray') ax.axvline(0, color='gray') ax.set_aspect('equal')
ax.plot(circle[0], circle[1], 'k--', label="Unit Circle")
ax.plot(ellipse[0], ellipse[1], 'r', label="Ellipse = A(Unit Circle)")
ax.arrow(0, 0, e1[0], e1[1], width=0.01, color='black', label='e1') ax.arrow(0, 0, e2[0], e2[1], width=0.01, color='black', label='e2')
ax.arrow(0, 0, Ae1[0], Ae1[1], width=0.01, color='red', label='A e1') ax.arrow(0, 0, Ae2[0], Ae2[1], width=0.01, color='blue', label='A e2')
ax.legend(loc="upper left")
ax = axes[1] ax.set_title("Spectral Decomposition from Eigenvector Basis") ax.set_xlim(-2, 2) ax.set_ylim(-2, 2) ax.axhline(0, color='gray') ax.axvline(0, color='gray') ax.set_aspect('equal')
ax.plot(circle[0], circle[1], 'k--', label="Unit Circle")
ax.plot(ellipse[0], ellipse[1], 'r', label="Ellipse = A(Unit Circle)")
ax.arrow(0, 0, v1[0], v1[1], width=0.01, color='purple', label='v1') ax.arrow(0, 0, v2[0], v2[1], width=0.01, color='brown', label='v2')
ax.arrow(0, 0, Av1[0], Av1[1], width=0.01, color='purple', alpha=0.6, label='lambda1 * v1') ax.arrow(0, 0, Av2[0], Av2[1], width=0.01, color='brown', alpha=0.6, label='lambda2 * v2')
ax.legend(loc="upper left")
plt.tight_layout() plt.show()
|