4#include "../consteig_options.hpp"
5#include "../consteig_types.hpp"
6#include "../math/constmath.hpp"
7#include "../matrix/decompositions/hessenberg.hpp"
8#include "../matrix/decompositions/lu.hpp"
9#include "../matrix/decompositions/qr.hpp"
10#include "../matrix/matrix.hpp"
11#include "../matrix/operations.hpp"
20#ifdef CONSTEIG_USE_LONG_DOUBLE
21using InternalScalar =
long double;
23using InternalScalar = double;
27template <
typename T, Size S>
28constexpr Matrix<T, S, S>
eig(
36template <
typename T, Size S>
37constexpr Matrix<T, S, S> balance(Matrix<T, S, S> a)
39 bool converged =
false;
40 T factor =
static_cast<T
>(2);
42 for (Size iter = 0; iter < 10 && !converged; ++iter)
45 for (Size row = 0; row < S; ++row)
49 for (Size col = 0; col < S; ++col)
53 row_norm +=
abs(a(row, col));
54 col_norm +=
abs(a(col, row));
58 if (row_norm > 0 && col_norm > 0)
61 T s = row_norm + col_norm;
62 while (row_norm < col_norm / factor)
68 while (row_norm > col_norm * factor)
79 if ((row_norm + col_norm) <
83 for (Size col = 0; col < S; ++col)
87 for (Size scale_row = 0; scale_row < S; ++scale_row)
89 a(scale_row, row) /= f;
103constexpr T wilkinsonShift(
const T a,
const T b,
const T c)
105 T delta{(a - c) / 2};
106 if (delta ==
static_cast<T
>(0))
118template <
typename T, Size S>
119constexpr void francis_qr_step(Matrix<T, S, S> &
H, Size
l, Size
n,
T s,
T t)
127 Size
m = (
pos + 2 <=
n) ? 3 : 2;
156 (
m == 3 ?
v3 *
H(
pos + 2, col) : 0));
167 for (Size row = 0; row <=
upper_row && row <
S; ++row)
170 (
m == 3 ?
v3 *
H(row,
pos + 2) : 0));
207template <
typename T, Size S>
208constexpr Matrix<T, S, S> eig_double_shifted_qr(Matrix<T, S, S>
a)
210 if constexpr (
S <= 1)
277 if (
its > 0 &&
its % 10 == 0)
310 francis_qr_step(
a,
l,
n,
s,
t);
319template <
typename T, Size S>
320constexpr Matrix<T, S, S> eig_shifted_qr(Matrix<T, S, S>
a)
322 if constexpr (
S <= 1)
341 if (
abs(
a(
n - 1,
n - 2)) <=
350 wilkinsonShift(
a(
n - 2,
n - 2),
a(
n - 1,
n - 2),
a(
n - 1,
n - 1));
378template <
typename T, Size S>
381 static_assert(
is_float<T>(),
"eig reduction expects floating point");
425template <
typename T, Size S>
428 static_assert(
is_float<T>(),
"eigenvalues expects floating point type");
430 for (Size row = 0; row <
S; ++row)
432 for (Size col = 0; col <
S; ++col)
434 a_internal(row, col) =
static_cast<InternalScalar
>(
a(row, col));
441 (
norm1(
out) +
static_cast<InternalScalar
>(1.0));
527template <
typename T, Size R, Size C>
534 for (Size row = 0; row <
R; ++row)
548 if constexpr (
R <= 4)
552 for (Size row = 0; row <
R; ++row)
592template <
typename T, Size S>
596 static_assert(
is_float<T>(),
"eigenvectors expects floating point type");
605 for (Size row = 0; row <
S; ++row)
607 for (Size col = 0; col <
S; ++col)
624 for (Size row = 0; row <
S; ++row)
638 for (Size row = 0; row <
S; ++row)
655 for (Size row = 0; row <
S; ++row)
663 for (Size row = 0; row <
S; ++row)
666 b(row, 0).imag *
b(row, 0).imag;
673 for (Size row = 0; row <
S; ++row)
681 for (Size row = 0; row <
S; ++row)
Convenience class that computes both eigenvalues and eigenvectors.
Definition eigen.hpp:704
constexpr const Matrix< Complex< T >, S, S > & eigenvectors() const
Return the S×S eigenvector matrix (column eig_col corresponds to eigenvalue eig_col).
Definition eigen.hpp:721
constexpr const Matrix< Complex< T >, S, 1 > & eigenvalues() const
Return the S×1 eigenvalue column vector.
Definition eigen.hpp:714
constexpr EigenSolver(const Matrix< T, S, S > &mat)
Compute eigenvalues and eigenvectors of mat.
Definition eigen.hpp:707
Fixed-size matrix with compile-time dimensions.
Definition matrix.hpp:56
#define CONSTEIG_BALANCE_CONVERGENCE_THRESHOLD
Stopping criterion for the matrix balancing step.
Definition consteig_options.hpp:44
#define CONSTEIG_DEFAULT_SYMMETRIC_TOLERANCE
Symmetry routing threshold for eigensolver selection.
Definition consteig_options.hpp:33
#define CONSTEIG_MAX_ITER
Maximum QR iterations per eigenvalue block.
Definition consteig_options.hpp:21
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, S, 1 > lu_solve(const LUMatrix< T, S > &lu, const Matrix< T, S, 1 > &b)
Solve the linear system Ax = b given the LU factorization of A.
Definition lu.hpp:138
constexpr LUMatrix< T, S > lu(const Matrix< T, S, S > &a)
LU decomposition with partial pivoting.
Definition lu.hpp:54
constexpr QRMatrix< T, R > qr_hessenberg(const Matrix< T, R, C > a)
QR decomposition optimized for upper Hessenberg matrices.
Definition qr.hpp:59
constexpr Matrix< T, S, S > eig(Matrix< T, S, S > a, const T symmetryTolerance=CONSTEIG_DEFAULT_SYMMETRIC_TOLERANCE)
Reduce a matrix to quasi-upper-triangular (Schur) form.
Definition eigen.hpp:379
constexpr Matrix< Complex< T >, S, 1 > eigenvalues(const Matrix< T, S, S > &a)
Compute the eigenvalues of a real square matrix.
Definition eigen.hpp:426
static constexpr bool checkEigenValues(const Matrix< T, R, C > &a, const Matrix< Complex< T >, R, 1 > &lambda, const T thresh)
Verify computed eigenvalues against matrix invariants.
Definition eigen.hpp:528
constexpr Matrix< Complex< T >, S, S > eigenvectors(const Matrix< T, S, S > &A, const Matrix< Complex< T >, S, 1 > &eigenvalues)
Compute eigenvectors given a matrix and its eigenvalues.
Definition eigen.hpp:593
constexpr T sqrt(const T x)
Constexpr square root.
Definition sqrt.hpp:67
constexpr T abs(const T x)
Absolute value of a real number.
Definition abs.hpp:17
constexpr T epsilon()
Machine epsilon for type T.
Definition utilities.hpp:82
constexpr T determinant(const Matrix< T, R, C > &mat)
Determinant via Laplace (cofactor) expansion.
Definition operations.hpp:323
constexpr T norm1(const Matrix< T, R, C > &mat)
1-norm (maximum absolute column sum).
Definition operations.hpp:235
constexpr T norm(const Matrix< T, R, C > &mat)
Frobenius (Euclidean) norm: sqrt(sum of squared elements).
Definition operations.hpp:211
constexpr T trace(const Matrix< T, R, C > &mat)
Trace: sum of diagonal elements.
Definition operations.hpp:370
constexpr T normInf(const Matrix< T, R, C > &mat)
Infinity-norm (maximum absolute row sum).
Definition operations.hpp:262
Constexpr complex number type.
Definition complex.hpp:26