11 constexpr real tiny = 1
e-300;
14 throw std::invalid_argument(
"power_iteration: matrix must be square");
22 for (
idx iter = 0; iter < max_iter; ++iter) {
23 result.iterations = iter + 1;
28 real new_lambda =
dot(v, w, backend);
31 real delta = std::abs(new_lambda - lambda);
35 if (delta < tol * (std::abs(lambda) + tiny)) {
36 result.converged =
true;
41 result.eigenvalue = lambda;
42 result.eigenvector = v;
51 constexpr real tiny = 1
e-300;
54 throw std::invalid_argument(
"inverse_iteration: matrix must be square");
58 for (
idx i = 0; i < n; ++i)
68 for (
idx iter = 0; iter < max_iter; ++iter) {
69 result.iterations = iter + 1;
78 real new_lambda =
dot(w, Av, backend);
80 real delta = std::abs(new_lambda - lambda);
84 if (delta < tol * (std::abs(lambda) + tiny)) {
85 result.converged =
true;
90 result.eigenvalue = lambda;
91 result.eigenvector = v;
102 throw std::invalid_argument(
"rayleigh_iteration: matrix must be square");
104 throw std::invalid_argument(
"rayleigh_iteration: x0 size mismatch");
111 matvec(A, v, Av, backend);
112 real sigma =
dot(v, Av, backend);
116 for (
idx iter = 0; iter < max_iter; ++iter) {
117 result.iterations = iter + 1;
121 for (
idx i = 0; i < n; ++i)
132 matvec(A, w, Av, backend);
133 real new_sigma =
dot(w, Av, backend);
137 for (
idx i = 0; i < n; ++i) {
138 real d = Av[i] - new_sigma * w[i];
141 res = std::sqrt(res);
147 result.converged =
true;
152 result.eigenvalue = sigma;
153 result.eigenvector = v;
constexpr idx rows() const noexcept
constexpr idx cols() const noexcept
constexpr idx size() const noexcept
real normalise(Vector &v)
Normalise v in-place; returns the old norm.
void matvec(const Matrix &A, const Vector &x, Vector &y, Backend b=default_backend)
y = A * x
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.
real dot(const Vector &x, const Vector &y, Backend b=default_backend)
Compute .
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.
void lu_solve(const LUResult &f, const Vector &b, Vector &x)
Solve from a precomputed factorization.
LUResult lu(const Matrix &A, Backend backend=lapack_backend)
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.
Power iteration, inverse iteration, Rayleigh quotient iteration.
Result of a single-eigenvalue iteration.