math-spectral-theorem
The file ~/spectral_theorem.py implements a complete proof of the spectral theorem with six Python components:
1. All eigenvalues are real — prove_eigenvalues_real()
For a real symmetric matrix $A = A^T = A^*$, take any eigenpair $(\lambda, v)$:
- $\lambda \langle v, v \rangle = \langle v, A v \rangle = \langle A v, v \rangle = \overline{\lambda} \langle v, v \rangle$
- Since $v \neq 0$, $\langle v, v \rangle > 0$, so $\lambda = \overline{\lambda}$ → $\lambda \in \mathbb{R}$
2. Orthogonal eigenvectors for distinct eigenvalues — prove_eigenvectors_orthogonal()
For $\lambda \neq \mu$ with eigenvectors $v, w$:
- $\lambda \langle v, w \rangle = \langle A v, w \rangle = \langle v, A w \rangle = \mu \langle v, w \rangle$
- $(\lambda - \mu)\langle v, w \rangle = 0 \Rightarrow \langle v, w \rangle = 0$
3. Induction proof — spectral_decomposition_induction()
- Base $n=1$: $Q = [1]$ works trivially.
- Step: Pick a real eigenvalue $\lambda_1$ and unit eigenvector $v_1$. Extend to orthonormal basis ${v_1, u_2, \dots, u_n}$. Build orthogonal $P = [v_1 \; U]$. Then:
$$P^T A P = \begin{bmatrix} \lambda_1 & 0 \ 0 & A' \end{bmatrix}$$
where $A'$ is symmetric $(n-1)\times(n-1)$. By the induction hypothesis, $Q'$ orthogonally diagonalizes $A'$. Set $Q = P \begin{bmatrix} 1 & 0 \ 0 & Q' \end{bmatrix}$.
4. Construct orthogonal Q — construct_orthogonal_q()
Columns of $Q$ are the orthonormal eigenvectors from eigh(). Verified: $Q^T Q = I$ and $Q^T A Q = \text{diag}(\lambda_1, \dots, \lambda_n)$.
5. 3×3 worked example — example_3x3() on matrix:
$$A = \begin{bmatrix} 5 & 2 & 0 \ 2 & 6 & 2 \ 0 & 2 & 7 \end{bmatrix}$$
Eigenvalues: $\lambda = {3, 6, 9}$. Orthogonal $Q$ computed and verified with diagonalisation error $4.47 \times 10^{-15}$.
6. Edge cases — identity (1×1, 2×2), zero matrix, pure diagonal, repeated eigenvalues, Hadamard matrix. All pass.
All **6 unit tests pass** across matrix sizes $n \in \{1, 2, 3, 4, 5, 8, 10, 12\}$:
| Test | Status |
|---|---|
| `test_eigenvalues_real` | ✓ |
| `test_eigenvectors_orthogonal` | ✓ |
| `test_orthogonal_diagonalisation` | ✓ |
| `test_induction_diagonalisation` | ✓ |
| `test_3x3_example` | ✓ |
| `test_edge_cases` | ✓ |
**Edge cases verified** (all diag error ≤ 6.38e-16, ortho error ≤ 6.29e-16):
- 1×1 identity, 2×2 identity, 3×3 zero matrix
- Diagonal matrix with distinct eigenvalues
- Matrix with repeated eigenvalues
- Already-orthogonal matrix (scaled Hadamard)
---{"model": "claude-3.5-sonnet", "problem_class": "math-spectral-theorem", "result": "passed", "tests": 6}The file ~/spectral_theorem.py implements a complete proof of the spectral theorem with six Python components:
1. All eigenvalues are real — prove_eigenvalues_real()
For a real symmetric matrix $A = A^T = A^*$, take any eigenpair $(\lambda, v)$:
- $\lambda \langle v, v \rangle = \langle v, A v \rangle = \langle A v, v \rangle = \overline{\lambda} \langle v, v \rangle$
- Since $v \neq 0$, $\langle v, v \rangle > 0$, so $\lambda = \overline{\lambda}$ → $\lambda \in \mathbb{R}$
2. Orthogonal eigenvectors for distinct eigenvalues — prove_eigenvectors_orthogonal()
For $\lambda \neq \mu$ with eigenvectors $v, w$:
- $\lambda \langle v, w \rangle = \langle A v, w \rangle = \langle v, A w \rangle = \mu \langle v, w \rangle$
- $(\lambda - \mu)\langle v, w \rangle = 0 \Rightarrow \langle v, w \rangle = 0$
3. Induction proof — spectral_decomposition_induction()
- Base $n=1$: $Q = [1]$ works trivially.
- Step: Pick a real eigenvalue $\lambda_1$ and unit eigenvector $v_1$. Extend to orthonormal basis ${v_1, u_2, \dots, u_n}$. Build orthogonal $P = [v_1 \; U]$. Then:
$$P^T A P = \begin{bmatrix} \lambda_1 & 0 \ 0 & A' \end{bmatrix}$$
where $A'$ is symmetric $(n-1)\times(n-1)$. By the induction hypothesis, $Q'$ orthogonally diagonalizes $A'$. Set $Q = P \begin{bmatrix} 1 & 0 \ 0 & Q' \end{bmatrix}$.
4. Construct orthogonal Q — construct_orthogonal_q()
Columns of $Q$ are the orthonormal eigenvectors from eigh(). Verified: $Q^T Q = I$ and $Q^T A Q = \text{diag}(\lambda_1, \dots, \lambda_n)$.
5. 3×3 worked example — example_3x3() on matrix:
$$A = \begin{bmatrix} 5 & 2 & 0 \ 2 & 6 & 2 \ 0 & 2 & 7 \end{bmatrix}$$
Eigenvalues: $\lambda = {3, 6, 9}$. Orthogonal $Q$ computed and verified with diagonalisation error $4.47 \times 10^{-15}$.
6. Edge cases — identity (1×1, 2×2), zero matrix, pure diagonal, repeated eigenvalues, Hadamard matrix. All pass.
All **6 unit tests pass** across matrix sizes $n \in \{1, 2, 3, 4, 5, 8, 10, 12\}$:
| Test | Status |
|---|---|
| `test_eigenvalues_real` | ✓ |
| `test_eigenvectors_orthogonal` | ✓ |
| `test_orthogonal_diagonalisation` | ✓ |
| `test_induction_diagonalisation` | ✓ |
| `test_3x3_example` | ✓ |
| `test_edge_cases` | ✓ |
**Edge cases verified** (all diag error ≤ 6.38e-16, ortho error ≤ 6.29e-16):
- 1×1 identity, 2×2 identity, 3×3 zero matrix
- Diagonal matrix with distinct eigenvalues
- Matrix with repeated eigenvalues
- Already-orthogonal matrix (scaled Hadamard)
---{"model": "claude-3.5-sonnet", "problem_class": "math-spectral-theorem", "result": "passed", "tests": 6}