consteig
Compile-time eigenvalue and eigenvector computation for C++17
Loading...
Searching...
No Matches
hessenberg.hpp
1#ifndef HESSENBERG_HPP
2#define HESSENBERG_HPP
3
4#include "../../math/constmath.hpp"
5#include "../matrix.hpp"
6#include "../operations.hpp"
7#include "householder.hpp"
8
9namespace consteig
10{
11
14
27template <typename T, Size S> struct PHMatrix
28{
29 Matrix<T, S, S> _p;
30 Matrix<T, S, S> _h;
31};
32
54template <typename T, Size R, Size C, Size L = R>
56
58
59// Algorithm: Hessenberg Reduction
60// Reduces a square matrix A to upper Hessenberg form H via orthogonal
61// similarity: H = P^T * A * P, where P is the accumulated product of
62// Householder reflectors. H has zeros below the first subdiagonal.
63//
64// Reducing to Hessenberg form before QR iteration cuts the cost of each
65// QR step from O(n^3) to O(n^2), making the overall solver O(n^3).
66//
67// The reduction is implemented recursively. At each level, a Householder
68// reflector P is computed from the trailing submatrix and applied as a
69// similarity transformation P^T * A * P, preserving eigenvalues. The template
70// parameter L tracks the remaining submatrix size, bottoming out at L <= 2
71// when no further reduction is needed.
72//
73// Reference: Golub & Van Loan, "Matrix Computations" (4th ed.), sec. 7.4
74template <typename T, Size R, Size C, Size L>
75constexpr PHMatrix<T, R> hess(Matrix<T, R, C> a)
76{
77 static_assert(is_float<T>(), "hess expects floating point");
78 static_assert(R == C, "Hessenberg reduction expects a square matrix");
79
80 if constexpr (L <= 2)
81 {
82 // Base case: submatrix is 2x2 or smaller, already Hessenberg
83 PHMatrix<T, R> result{};
84 result._h = a;
85 return result;
86 }
87 else
88 {
89 constexpr Size size{R};
90 constexpr Size houseSize{L};
91
92 // Extract the trailing submatrix and compute its Householder reflector
94 R - houseSize, R - houseSize)};
96
97 // Embed the reflector into a full-size identity matrix
99 p.template setBlock<houseSize - 1, houseSize - 1>(
100 m.template block<houseSize - 1, houseSize - 1>(1, 1),
101 R - houseSize + 1, R - houseSize + 1);
102
103 // Apply the similarity transformation P * A * P and recurse
104 PHMatrix<T, R> out = hess<T, R, R, houseSize - 1>(p * a * p);
105
106 // Accumulate the orthogonal factor P
107 Matrix<T, size, size> pRtn = (houseSize > 3) ? p * out._p : p;
108
109 return {pRtn, out._h};
110 }
111}
112
114
115} // namespace consteig
116#endif
Fixed-size matrix with compile-time dimensions.
Definition matrix.hpp:56
constexpr PHMatrix< T, R > hess(Matrix< T, R, C > a)
Reduce a square matrix to upper Hessenberg form.
Definition hessenberg.hpp:75
constexpr Matrix< T, R, R > house(Matrix< T, R, C > a)
Compute a Householder reflector for a square matrix.
Definition householder.hpp:52
constexpr T epsilon()
Machine epsilon for type T.
Definition utilities.hpp:82