numerics 0.1.0
Loading...
Searching...
No Matches
power.hpp
Go to the documentation of this file.
1/// @file eigen/power.hpp
2/// @brief Power iteration, inverse iteration, Rayleigh quotient iteration
3///
4/// All three methods accept a backend parameter:
5///
6/// power_iteration(A) // default_backend
7/// power_iteration(A, tol, max, num::omp) // OmpBackend -- parallel
8/// matvec power_iteration(A, tol, max, num::blas) // BlasBackend -- BLAS
9/// matvec
10///
11/// The backend is forwarded to every matvec, dot, axpy, and norm call inside
12/// the iteration.
13#pragma once
14
15#include "core/matrix.hpp"
16#include "core/policy.hpp"
17#include "core/vector.hpp"
19#include <cmath>
20#include <stdexcept>
21
22namespace num {
23
24/// @brief Result of a single-eigenvalue iteration
26 real eigenvalue = 0.0; ///< Converged eigenvalue (Rayleigh quotient)
27 Vector eigenvector; ///< Corresponding unit eigenvector
28 idx iterations = 0; ///< Iterations performed
29 bool converged = false; ///< Whether tolerance was met
30};
31
32namespace detail {
33/// Normalise v in-place; returns the old norm.
34inline real normalise(Vector& v) {
35 real nrm = 0;
36 for (idx i = 0; i < v.size(); ++i)
37 nrm += v[i] * v[i];
38 nrm = std::sqrt(nrm);
39 if (nrm > 1e-300)
40 for (idx i = 0; i < v.size(); ++i)
41 v[i] /= nrm;
42 return nrm;
43}
44} // namespace detail
45
46/// @brief Power iteration -- finds the eigenvalue largest in absolute value.
47///
48/// @param A Square matrix (need not be symmetric)
49/// @param tol Tolerance on eigenvalue change between iterations
50/// @param max_iter Maximum iterations
51/// @param backend Backend forwarded to matvec and dot
52PowerResult power_iteration(const Matrix& A,
53 real tol = 1e-10,
54 idx max_iter = 1000,
55 Backend backend = default_backend);
56
57/// @brief Inverse iteration -- finds the eigenvalue closest to a shift sigma.
58///
59/// Factorizes (A - sigmaI) once then solves repeatedly.
60///
61/// @param A Square matrix (symmetric recommended)
62/// @param sigma Shift -- should be near the target eigenvalue
63/// @param tol Tolerance on eigenvalue change between iterations
64/// @param max_iter Maximum iterations
65/// @param backend Backend forwarded to matvec and dot
66PowerResult inverse_iteration(const Matrix& A,
67 real sigma,
68 real tol = 1e-10,
69 idx max_iter = 1000,
70 Backend backend = default_backend);
71
72/// @brief Rayleigh quotient iteration -- cubically convergent.
73///
74/// Updates the shift sigma = v^T*A*v at every step -> fresh LU each iteration.
75///
76/// @param A Symmetric matrix
77/// @param x0 Starting vector (determines which eigenvalue is found)
78/// @param tol Tolerance on residual ||A*v - lambda*v||
79/// @param max_iter Maximum iterations
80/// @param backend Backend forwarded to matvec, dot, axpy, norm
81PowerResult rayleigh_iteration(const Matrix& A,
82 const Vector& x0,
83 real tol = 1e-10,
84 idx max_iter = 50,
85 Backend backend = default_backend);
86
87} // namespace num
constexpr idx size() const noexcept
Definition vector.hpp:83
Backend enum and default backend selection.
LU factorization with partial pivoting.
Dense row-major matrix templated over scalar type T.
real normalise(Vector &v)
Normalise v in-place; returns the old norm.
Definition power.hpp:34
double real
Definition types.hpp:10
Backend
Definition policy.hpp:7
std::size_t idx
Definition types.hpp:11
BasicMatrix< real > Matrix
Double-precision dense matrix with full backend dispatch (CPU + GPU).
Definition matrix.hpp:127
PowerResult inverse_iteration(const Matrix &A, real sigma, real tol=1e-10, idx max_iter=1000, Backend backend=default_backend)
Inverse iteration – finds the eigenvalue closest to a shift sigma.
Definition power.cpp:46
PowerResult power_iteration(const Matrix &A, real tol=1e-10, idx max_iter=1000, Backend backend=default_backend)
Power iteration – finds the eigenvalue largest in absolute value.
Definition power.cpp:10
constexpr real e
Definition math.hpp:44
BasicVector< real > Vector
Real-valued dense vector with full backend dispatch (CPU + GPU)
Definition vector.hpp:129
constexpr Backend default_backend
Definition policy.hpp:53
PowerResult rayleigh_iteration(const Matrix &A, const Vector &x0, real tol=1e-10, idx max_iter=50, Backend backend=default_backend)
Rayleigh quotient iteration – cubically convergent.
Definition power.cpp:95
Result of a single-eigenvalue iteration.
Definition power.hpp:25
real eigenvalue
Converged eigenvalue (Rayleigh quotient)
Definition power.hpp:26
idx iterations
Iterations performed.
Definition power.hpp:28
Vector eigenvector
Corresponding unit eigenvector.
Definition power.hpp:27
bool converged
Whether tolerance was met.
Definition power.hpp:29
Dense vector storage and operations.