14 constexpr real tiny = 1
e-300;
17 throw std::invalid_argument(
"power_iteration: matrix must be square");
25 for (
idx iter = 0; iter < max_iter; ++iter) {
26 result.iterations = iter + 1;
31 real new_lambda =
dot(v, w, backend);
34 real delta = std::abs(new_lambda - lambda);
38 if (delta < tol * (std::abs(lambda) + tiny)) {
39 result.converged =
true;
44 result.eigenvalue = lambda;
45 result.eigenvector = v;
54 constexpr real tiny = 1
e-300;
57 throw std::invalid_argument(
"inverse_iteration: matrix must be square");
61 for (
idx i = 0; i < n; ++i)
71 for (
idx iter = 0; iter < max_iter; ++iter) {
72 result.iterations = iter + 1;
81 real new_lambda =
dot(w, Av, backend);
83 real delta = std::abs(new_lambda - lambda);
87 if (delta < tol * (std::abs(lambda) + tiny)) {
88 result.converged =
true;
93 result.eigenvalue = lambda;
94 result.eigenvector = v;
105 throw std::invalid_argument(
106 "rayleigh_iteration: matrix must be square");
108 throw std::invalid_argument(
"rayleigh_iteration: x0 size mismatch");
115 matvec(A, v, Av, backend);
116 real sigma =
dot(v, Av, backend);
120 for (
idx iter = 0; iter < max_iter; ++iter) {
121 result.iterations = iter + 1;
125 for (
idx i = 0; i < n; ++i)
136 matvec(A, w, Av, backend);
137 real new_sigma =
dot(w, Av, backend);
141 for (
idx i = 0; i < n; ++i) {
142 real d = Av[i] - new_sigma * w[i];
145 res = std::sqrt(res);
151 result.converged =
true;
156 result.eigenvalue = sigma;
157 result.eigenvector = v;
constexpr idx size() const noexcept
Dense row-major matrix with optional GPU storage.
constexpr idx rows() const noexcept
constexpr idx cols() const noexcept
real normalise(Vector &v)
Normalise v in-place; returns the old norm.
Backend
Selects which backend handles a linalg operation.
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)
dot product
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 A*x = b using a precomputed LU factorization.
LUResult lu(const Matrix &A, Backend backend=lapack_backend)
LU factorization of a square matrix A with partial pivoting.
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 an LU factorization with partial pivoting (PA = LU)
Result of a single-eigenvalue iteration.