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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
// Tests for qr_iteration.hpp / qr_iteration.cpp
//
// Stage 1: Unshifted QR iteration.
//
// All Stage 1 tests use symmetric matrices (only real eigenvalues) because
// the unshifted algorithm converges to upper-triangular form — not merely
// quasi-upper-triangular — only when all eigenvalues are real. A matrix
// with a complex-conjugate pair would stall: its 2×2 Schur block keeps a
// non-negligible subdiagonal entry indefinitely, so ||lower(A_k)||_F never
// falls below the tolerance. Proper handling of complex pairs requires the
// double-shift strategy introduced in Stage 2.
//
// Refs: T&B Lecture 28; GVL §7.3–7.4.
#include "linalg_error.hpp"
#include "matrix.hpp"
#include "qr_iteration.hpp"
#include "vector.hpp"
#include <catch2/catch_approx.hpp>
#include <catch2/catch_test_macros.hpp>
#include <algorithm>
#include <cmath>
#include <cstddef>
#include <iostream>
#include <utility>
#include <vector>
using linalg::Matrix;
using linalg::NonConvergenceError;
using linalg::QRIterationOptions;
using linalg::QRIterationResult;
using linalg::Vector;
// ---------------------------------------------------------------------------
// Test helpers
// ---------------------------------------------------------------------------
namespace {
// Sort (real, imag) eigenvalue pairs by real part (ascending), then by imag.
// Returns a std::vector<std::pair<double,double>> — a plain container of
// pairs, not a math vector.
using EigPairs = std::vector<std::pair<double, double>>;
EigPairs to_pairs(const Vector& real_v, const Vector& imag_v) {
EigPairs out;
out.reserve(real_v.size());
for (std::size_t i = 0; i < real_v.size(); ++i)
out.emplace_back(real_v[i], imag_v[i]);
std::sort(out.begin(), out.end(),
[](const std::pair<double, double>& a,
const std::pair<double, double>& b) {
return a.first != b.first ? a.first < b.first
: a.second < b.second;
});
return out;
}
// Return true when every computed eigenvalue is within `tol` of the
// corresponding expected eigenvalue (after sorting both sets).
// `expected` is a plain std::vector of (real, imag) pairs used as test data.
bool eigs_match(const Vector& computed_real, const Vector& computed_imag,
const EigPairs& expected, double tol) {
if (computed_real.size() != expected.size()) return false;
const EigPairs computed = to_pairs(computed_real, computed_imag);
EigPairs exp_sorted = expected;
std::sort(exp_sorted.begin(), exp_sorted.end(),
[](const std::pair<double, double>& a,
const std::pair<double, double>& b) {
return a.first != b.first ? a.first < b.first
: a.second < b.second;
});
for (std::size_t i = 0; i < computed.size(); ++i) {
const double dr = computed[i].first - exp_sorted[i].first;
const double di = computed[i].second - exp_sorted[i].second;
if (std::sqrt(dr * dr + di * di) > tol) return false;
}
return true;
}
} // namespace
// ---------------------------------------------------------------------------
// Test 1: 2×2 symmetric matrix with known eigenvalues
// ---------------------------------------------------------------------------
//
// A = | 2 1 | is symmetric positive definite.
// | 1 2 |
//
// Characteristic polynomial: (2-λ)^2 - 1 = 0 → λ = 1, 3.
// Eigenvectors: [1,-1]/√2 (λ=1) and [1,1]/√2 (λ=3).
//
// The unshifted iteration converges at rate |λ_1/λ_2| = 1/3 per step,
// so only a handful of iterations are needed.
// Ref: T&B Theorem 28.2.
TEST_CASE("QR iteration (unshifted): 2x2 symmetric known eigenvalues",
"[qr_iteration][stage1]") {
const Matrix A{
{2.0, 1.0},
{1.0, 2.0}
};
const QRIterationResult res = linalg::eigenvalues_unshifted(A);
REQUIRE(res.eigenvalues_real.size() == 2);
REQUIRE(res.eigenvalues_imag.size() == 2);
REQUIRE(res.iterations > 0);
// All eigenvalues of a symmetric matrix must be real.
CHECK(std::abs(res.eigenvalues_imag[0]) < 1e-8);
CHECK(std::abs(res.eigenvalues_imag[1]) < 1e-8);
const EigPairs expected = {{1.0, 0.0}, {3.0, 0.0}};
CHECK(eigs_match(res.eigenvalues_real, res.eigenvalues_imag, expected, 1e-8));
}
// ---------------------------------------------------------------------------
// Test 2: 4×4 symmetric tridiagonal matrix — reference eigenvalues
// ---------------------------------------------------------------------------
//
// The n×n symmetric tridiagonal matrix with 2 on the diagonal and -1 on the
// first super- and sub-diagonals has known eigenvalues (discrete Laplacian):
//
// λ_k = 2 - 2 cos(k π / (n+1)), k = 1, …, n
//
// Ref: Golub & Van Loan §4.4.2 (discrete sine transform).
//
// For n = 4:
// λ_1 = 2 - 2 cos(π/5) ≈ 0.3820
// λ_2 = 2 - 2 cos(2π/5) ≈ 1.3820
// λ_3 = 2 - 2 cos(3π/5) ≈ 2.6180
// λ_4 = 2 - 2 cos(4π/5) ≈ 3.6180
TEST_CASE("QR iteration (unshifted): 4x4 symmetric tridiagonal",
"[qr_iteration][stage1]") {
const Matrix A{
{ 2.0, -1.0, 0.0, 0.0},
{-1.0, 2.0, -1.0, 0.0},
{ 0.0, -1.0, 2.0, -1.0},
{ 0.0, 0.0, -1.0, 2.0}
};
const QRIterationResult res = linalg::eigenvalues_unshifted(A);
REQUIRE(res.eigenvalues_real.size() == 4);
REQUIRE(res.eigenvalues_imag.size() == 4);
// All eigenvalues of a symmetric matrix must be real.
for (std::size_t k = 0; k < 4; ++k)
CHECK(std::abs(res.eigenvalues_imag[k]) < 1e-8);
// Compare against the closed-form reference.
constexpr double pi = 3.14159265358979323846;
const EigPairs expected = {
{2.0 - 2.0 * std::cos( pi / 5.0), 0.0},
{2.0 - 2.0 * std::cos(2.0 * pi / 5.0), 0.0},
{2.0 - 2.0 * std::cos(3.0 * pi / 5.0), 0.0},
{2.0 - 2.0 * std::cos(4.0 * pi / 5.0), 0.0}
};
CHECK(eigs_match(res.eigenvalues_real, res.eigenvalues_imag, expected, 1e-8));
}
// ---------------------------------------------------------------------------
// Test 3: 5×5 symmetric tridiagonal — convergence history
// ---------------------------------------------------------------------------
//
// Uses a 5×5 symmetric tridiagonal (discrete Laplacian) to guarantee all
// real eigenvalues and predictable linear convergence. The Frobenius norm
// of the strict lower triangle is printed at every step so the convergence
// rate can be observed directly.
//
// Expected behaviour: ||lower(A_k)||_F decreases geometrically each step
// (linear convergence), with ratio ≈ max_j |λ_{j+1}/λ_j|.
// Ref: T&B Theorem 28.2.
//
// 5×5 tridiagonal eigenvalues: λ_k = 2 - 2cos(kπ/6), k = 1..5.
TEST_CASE("QR iteration (unshifted): 5x5 convergence history",
"[qr_iteration][stage1]") {
const Matrix A{
{ 2.0, -1.0, 0.0, 0.0, 0.0},
{-1.0, 2.0, -1.0, 0.0, 0.0},
{ 0.0, -1.0, 2.0, -1.0, 0.0},
{ 0.0, 0.0, -1.0, 2.0, -1.0},
{ 0.0, 0.0, 0.0, -1.0, 2.0}
};
QRIterationOptions opts;
opts.track_convergence = true;
const QRIterationResult res = linalg::eigenvalues_unshifted(A, opts);
REQUIRE_FALSE(res.convergence_history.empty());
REQUIRE(res.eigenvalues_real.size() == 5);
// Print convergence history so the linear rate is visible.
std::cout << "\n=== Stage 1: Unshifted QR — 5x5 convergence history ===\n";
std::cout << " Converged in " << res.iterations << " iteration(s)\n";
for (std::size_t k = 0; k < res.convergence_history.size(); ++k) {
std::cout << " iter " << (k + 1)
<< ": ||lower(A_k)||_F = "
<< res.convergence_history[k] << "\n";
}
std::cout << "=======================================================\n";
// The final recorded norm must be below the default tolerance.
CHECK(res.convergence_history.back() < opts.tolerance);
}
// ---------------------------------------------------------------------------
// Test 4: Eigenvalue residuals below 1e-8
// ---------------------------------------------------------------------------
//
// For several symmetric matrices with analytically known eigenvalues, verify
// that every computed eigenvalue is within 1e-8 of its expected value.
//
// Residual means the absolute error |λ_computed - λ_exact| (eigenvalue
// accuracy), not a matrix residual ||A x - λ x||, which would require
// eigenvectors unavailable in Stage 1.
TEST_CASE("QR iteration (unshifted): residuals below 1e-8",
"[qr_iteration][stage1]") {
SECTION("2x2: eigenvalues 1 and 3") {
const Matrix A{{2.0, 1.0}, {1.0, 2.0}};
const QRIterationResult res = linalg::eigenvalues_unshifted(A);
const EigPairs expected = {{1.0, 0.0}, {3.0, 0.0}};
CHECK(eigs_match(res.eigenvalues_real, res.eigenvalues_imag, expected, 1e-8));
}
SECTION("3x3 diagonal: eigenvalues 1, 4, 9") {
// Diagonal matrix — already in Schur form; converges in one step.
const Matrix D{
{1.0, 0.0, 0.0},
{0.0, 4.0, 0.0},
{0.0, 0.0, 9.0}
};
const QRIterationResult res = linalg::eigenvalues_unshifted(D);
const EigPairs expected = {{1.0, 0.0}, {4.0, 0.0}, {9.0, 0.0}};
CHECK(eigs_match(res.eigenvalues_real, res.eigenvalues_imag, expected, 1e-8));
}
SECTION("4x4 tridiagonal: closed-form eigenvalues") {
const Matrix A{
{ 2.0, -1.0, 0.0, 0.0},
{-1.0, 2.0, -1.0, 0.0},
{ 0.0, -1.0, 2.0, -1.0},
{ 0.0, 0.0, -1.0, 2.0}
};
constexpr double pi = 3.14159265358979323846;
const EigPairs expected = {
{2.0 - 2.0 * std::cos( pi / 5.0), 0.0},
{2.0 - 2.0 * std::cos(2.0 * pi / 5.0), 0.0},
{2.0 - 2.0 * std::cos(3.0 * pi / 5.0), 0.0},
{2.0 - 2.0 * std::cos(4.0 * pi / 5.0), 0.0}
};
const QRIterationResult res = linalg::eigenvalues_unshifted(A);
CHECK(eigs_match(res.eigenvalues_real, res.eigenvalues_imag, expected, 1e-8));
}
SECTION("5x5 identity: all eigenvalues == 1") {
const Matrix I = Matrix::identity(5);
const QRIterationResult res = linalg::eigenvalues_unshifted(I);
REQUIRE(res.eigenvalues_real.size() == 5);
for (std::size_t k = 0; k < 5; ++k) {
CHECK(std::abs(res.eigenvalues_real[k] - 1.0) < 1e-8);
CHECK(std::abs(res.eigenvalues_imag[k]) < 1e-8);
}
}
}
// ---------------------------------------------------------------------------
// Failure cases
// ---------------------------------------------------------------------------
TEST_CASE("QR iteration (unshifted): non-square matrix throws",
"[qr_iteration][stage1]") {
const Matrix A(3, 4); // non-square
CHECK_THROWS_AS(linalg::eigenvalues_unshifted(A),
linalg::DimensionMismatchError);
}
TEST_CASE("QR iteration (unshifted): max_iterations exceeded throws",
"[qr_iteration][stage1]") {
// Cap at zero iterations — any non-trivial matrix fails immediately.
const Matrix A{{2.0, 1.0}, {1.0, 2.0}};
QRIterationOptions opts;
opts.max_iterations = 0;
CHECK_THROWS_AS(linalg::eigenvalues_unshifted(A, opts), NonConvergenceError);
}
|