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, real tol = 1e-10,
53 idx max_iter = 1000,
54 Backend backend = default_backend);
55
56/// @brief Inverse iteration -- finds the eigenvalue closest to a shift sigma.
57///
58/// Factorizes (A - sigmaI) once then solves repeatedly.
59///
60/// @param A Square matrix (symmetric recommended)
61/// @param sigma Shift -- should be near the target eigenvalue
62/// @param tol Tolerance on eigenvalue change between iterations
63/// @param max_iter Maximum iterations
64/// @param backend Backend forwarded to matvec and dot
65PowerResult inverse_iteration(const Matrix &A, real sigma, real tol = 1e-10,
66 idx max_iter = 1000,
67 Backend backend = default_backend);
68
69/// @brief Rayleigh quotient iteration -- cubically convergent.
70///
71/// Updates the shift sigma = v^T*A*v at every step -> fresh LU each iteration.
72///
73/// @param A Symmetric matrix
74/// @param x0 Starting vector (determines which eigenvalue is found)
75/// @param tol Tolerance on residual ||A*v - lambda*v||
76/// @param max_iter Maximum iterations
77/// @param backend Backend forwarded to matvec, dot, axpy, norm
78PowerResult rayleigh_iteration(const Matrix &A, const Vector &x0,
79 real tol = 1e-10, idx max_iter = 50,
80 Backend backend = default_backend);
81
82} // namespace num
constexpr idx size() const noexcept
Definition vector.hpp:80
Backend enum for linear algebra operations.
LU factorization with partial pivoting.
Matrix operations.
real normalise(Vector &v)
Normalise v in-place; returns the old norm.
Definition power.hpp:34
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
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:49
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:43
BasicVector< real > Vector
Real-valued dense vector with full backend dispatch (CPU + GPU)
Definition vector.hpp:130
constexpr Backend default_backend
Definition policy.hpp:92
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:98
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
Vector operations.