numerics
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 matvec
8/// power_iteration(A, tol, max, num::blas) // BlasBackend -- BLAS matvec
9///
10/// The backend is forwarded to every matvec, dot, axpy, and norm call inside
11/// the iteration.
12#pragma once
13
14#include "core/matrix.hpp"
15#include "core/vector.hpp"
16#include "core/policy.hpp"
18#include <cmath>
19#include <stdexcept>
20
21namespace num {
22
23/// @brief Result of a single-eigenvalue iteration
25 real eigenvalue; ///< Converged eigenvalue (Rayleigh quotient)
26 Vector eigenvector; ///< Corresponding unit eigenvector
27 idx iterations; ///< Iterations performed
28 bool converged; ///< Whether tolerance was met
29};
30
31namespace detail {
32/// Normalise v in-place; returns the old norm.
33inline real normalise(Vector& v) {
34 real nrm = 0;
35 for (idx i = 0; i < v.size(); ++i) nrm += v[i] * v[i];
36 nrm = std::sqrt(nrm);
37 if (nrm > 1e-300)
38 for (idx i = 0; i < v.size(); ++i) v[i] /= nrm;
39 return nrm;
40}
41} // namespace detail
42
43/// @brief Power iteration -- finds the eigenvalue largest in absolute value.
44///
45/// @param A Square matrix (need not be symmetric)
46/// @param tol Tolerance on eigenvalue change between iterations
47/// @param max_iter Maximum iterations
48/// @param backend Backend forwarded to matvec and dot
49PowerResult power_iteration(const Matrix& A,
50 real tol = 1e-10, idx max_iter = 1000,
51 Backend backend = default_backend);
52
53/// @brief Inverse iteration -- finds the eigenvalue closest to a shift sigma.
54///
55/// Factorizes (A - sigmaI) once then solves repeatedly.
56///
57/// @param A Square matrix (symmetric recommended)
58/// @param sigma Shift -- should be near the target eigenvalue
59/// @param tol Tolerance on eigenvalue change between iterations
60/// @param max_iter Maximum iterations
61/// @param backend Backend forwarded to matvec and dot
62PowerResult inverse_iteration(const Matrix& A, real sigma,
63 real tol = 1e-10, idx max_iter = 1000,
64 Backend backend = default_backend);
65
66/// @brief Rayleigh quotient iteration -- cubically convergent.
67///
68/// Updates the shift sigma = vᵀAv at every step -> fresh LU each iteration.
69///
70/// @param A Symmetric matrix
71/// @param x0 Starting vector (determines which eigenvalue is found)
72/// @param tol Tolerance on residual ||A*v - lambda*v||
73/// @param max_iter Maximum iterations
74/// @param backend Backend forwarded to matvec, dot, axpy, norm
75PowerResult rayleigh_iteration(const Matrix& A, const Vector& x0,
76 real tol = 1e-10, idx max_iter = 50,
77 Backend backend = default_backend);
78
79} // namespace num
constexpr idx size() const noexcept
Definition vector.hpp:77
LU factorization with partial pivoting.
Matrix operations.
real normalise(Vector &v)
Normalise v in-place; returns the old norm.
Definition power.hpp:33
double real
Definition types.hpp:10
Backend
Selects which backend handles a linalg operation.
Definition policy.hpp:19
constexpr T ipow(T x) noexcept
Compute x^N at compile time via repeated squaring.
std::size_t idx
Definition types.hpp:11
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:48
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:11
constexpr real e
Definition math.hpp:41
BasicVector< real > Vector
Real-valued dense vector with full backend dispatch (CPU + GPU)
Definition vector.hpp:122
constexpr Backend default_backend
Definition policy.hpp:57
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:93
Backend enum for linear algebra operations.
Result of a single-eigenvalue iteration.
Definition power.hpp:24
real eigenvalue
Converged eigenvalue (Rayleigh quotient)
Definition power.hpp:25
idx iterations
Iterations performed.
Definition power.hpp:27
Vector eigenvector
Corresponding unit eigenvector.
Definition power.hpp:26
bool converged
Whether tolerance was met.
Definition power.hpp:28
Vector operations.