numerics 0.1.0
Loading...
Searching...
No Matches
jacobi_eig.hpp
Go to the documentation of this file.
1/// @file eigen/jacobi_eig.hpp
2/// @brief Full symmetric eigendecomposition via cyclic Jacobi sweeps
3///
4/// Computes ALL eigenvalues and eigenvectors of a real symmetric matrix.
5///
6/// Algorithm -- cyclic Jacobi:
7/// Repeatedly apply plane (Givens) rotations in the (p,q) plane to zero
8/// the off-diagonal element A[p,q]. Each rotation is a similarity transform
9/// so eigenvalues are preserved. Convergence is quadratic: after each full
10/// sweep the sum of squared off-diagonal elements decreases by at least a
11/// constant factor.
12///
13/// Why Jacobi instead of the implicit QR algorithm?
14/// Jacobi is conceptually simpler and each rotation is the exact same
15/// 2x2 eigendecomposition students encounter first. For dense n < 1000 the
16/// performance is acceptable. LAPACK uses the Francis QR algorithm (dsyev)
17/// which is O(n^2) per iteration vs O(n^2) per sweep here, but harder to
18/// explain.
19///
20/// Complexity: O(n^2) per sweep, O(n) sweeps typical -> O(n^3) total.
21#pragma once
22
23#include "core/matrix.hpp"
24#include "core/policy.hpp"
25#include "core/vector.hpp"
26
27namespace num {
28
29/// @brief Full eigendecomposition result: A = V * diag(values) * V^T
31 Vector values; ///< Eigenvalues in ascending order
32 Matrix vectors; ///< Eigenvectors as columns of an nxn orthogonal matrix
33 idx sweeps = 0; ///< Number of Jacobi sweeps performed
34 bool converged = false; ///< Whether off-diagonal norm fell below tol
35};
36
37/// @brief Full eigendecomposition of a real symmetric matrix.
38///
39/// The rotation accumulation loop is parallelised when backend == Backend::omp.
40///
41/// @param A Real symmetric nxn matrix (upper/lower triangle used)
42/// @param tol Jacobi convergence tolerance (ignored for Backend::lapack)
43/// @param max_sweeps Jacobi sweep cap (ignored for Backend::lapack)
44/// @param backend Backend::lapack uses LAPACKE_dsyevd (default when
45/// available).
46/// Backend::omp parallelises the Jacobi rotation loop.
47/// Backend::seq uses our cyclic Jacobi implementation.
48/// @return EigenResult with eigenvalues in ascending order and corresponding
49/// eigenvectors as columns of the V matrix (A = V diag(lambda) V^T)
50EigenResult eig_sym(const Matrix &A, real tol = 1e-12, idx max_sweeps = 100,
51 Backend backend = lapack_backend);
52
53} // namespace num
Dense row-major matrix with optional GPU storage.
Definition matrix.hpp:12
Backend enum for linear algebra operations.
Matrix operations.
double real
Definition types.hpp:10
Backend
Selects which backend handles a linalg operation.
Definition policy.hpp:19
std::size_t idx
Definition types.hpp:11
constexpr Backend lapack_backend
Definition policy.hpp:124
constexpr real e
Definition math.hpp:43
EigenResult eig_sym(const Matrix &A, real tol=1e-12, idx max_sweeps=100, Backend backend=lapack_backend)
Full eigendecomposition of a real symmetric matrix.
Definition eig.cpp:17
Full eigendecomposition result: A = V * diag(values) * V^T.
Vector values
Eigenvalues in ascending order.
bool converged
Whether off-diagonal norm fell below tol.
idx sweeps
Number of Jacobi sweeps performed.
Matrix vectors
Eigenvectors as columns of an nxn orthogonal matrix.
Vector operations.