numerics 0.1.0
Loading...
Searching...
No Matches
expv.hpp
Go to the documentation of this file.
1/// @file expv.hpp
2/// @brief Krylov subspace matrix exponential-vector product: compute exp(t*A)*v
3///
4/// Approximates \f$\exp(tA)v \approx \|v\| Q_m \exp(tH_m)e_1\f$ where
5/// \f$AQ_m \approx Q_{m+1}\bar{H}_m\f$ is the Arnoldi relation.
6#pragma once
7
8#include "core/matrix.hpp"
9#include "core/types.hpp"
10#include "core/vector.hpp"
11#include "kernel/subspace.hpp"
14#include <stdexcept>
15#include <utility>
16#include <vector>
17
18namespace num {
19
20namespace detail {
22}
23
24/// @brief Compute \f$\exp(tA)v\f$ for any \f$y=A x\f$ adapter.
25template<class Op>
26 requires operators::LinearOperator<Op, Vector, Vector>
27Vector expv(real t, const Op& A, const Vector& v, int m_max = 30, real tol = 1e-8) {
28 const idx n = A.rows();
29 if (A.cols() != n || v.size() != n) {
30 throw std::invalid_argument("expv: dimension mismatch");
31 }
32
33 real beta = norm(v);
34 if (beta < 1e-300) {
35 return Vector(n, 0.0);
36 }
37
38 std::vector<Vector> V;
39 V.reserve(m_max + 1);
40
41 Vector v0(n);
42 for (idx i = 0; i < n; i++) {
43 v0[i] = v[i] / beta;
44 }
45 V.push_back(std::move(v0));
46
47 Matrix H(m_max + 1, m_max, 0.0);
48 int m_actual = m_max;
49 std::vector<real> h_col(m_max + 1, 0.0);
50
51 for (int j = 0; j < m_max; j++) {
52 Vector w(n, 0.0);
53 A.apply(V[j], w);
54
55 const real h_next = kernel::subspace::mgs_orthogonalize(V, w, h_col, j + 1);
56 for (int i = 0; i <= j; i++) {
57 H(i, j) = h_col[i];
58 }
59 H(j + 1, j) = h_next;
60
61 if (h_next < tol) {
62 m_actual = j + 1;
63 break;
64 }
65
66 scale(w, real(1) / h_next);
67 V.push_back(std::move(w));
68 }
69
70 Matrix Hm(m_actual, m_actual, 0.0);
71 for (int i = 0; i < m_actual; i++) {
72 for (int j = 0; j < m_actual; j++) {
73 Hm(i, j) = t * H(i, j);
74 }
75 }
76
78
79 Vector result(n, 0.0);
80 for (int j = 0; j < m_actual; j++) {
81 axpy(beta * E(j, 0), V[j], result);
82 }
83
84 return result;
85}
86
88 const SparseMatrix& A,
89 const Vector& v,
90 int m_max = 30,
91 real tol = 1e-8);
92
93} // namespace num
constexpr idx size() const noexcept
Definition vector.hpp:83
Core type definitions.
Compressed Sparse Row (CSR) matrix and operations.
Dense row-major matrix templated over scalar type T.
Matrix dense_expm_pade6(const Matrix &A)
Definition expv.cpp:14
real mgs_orthogonalize(const std::vector< Vector > &basis, Vector &v, std::vector< real > &h, idx k)
Modified Gram-Schmidt against basis[0..k-1].
Definition subspace.cpp:9
double real
Definition types.hpp:10
real beta(real a, real b)
B(a, b) – beta function.
Definition math.hpp:248
std::size_t idx
Definition types.hpp:11
void scale(Vector &v, real alpha, Backend b=default_backend)
Compute .
Definition vector.cpp:15
BasicMatrix< real > Matrix
Double-precision dense matrix with full backend dispatch (CPU + GPU).
Definition matrix.hpp:127
constexpr real e
Definition math.hpp:44
BasicVector< real > Vector
Real-valued dense vector with full backend dispatch (CPU + GPU)
Definition vector.hpp:129
real norm(const Vector &x, Backend b=default_backend)
Compute .
Definition vector.cpp:83
Vector expv(real t, const Op &A, const Vector &v, int m_max=30, real tol=1e-8)
Compute for any adapter.
Definition expv.hpp:27
void axpy(real alpha, const Vector &x, Vector &y, Backend b=default_backend)
Compute .
Definition vector.cpp:44
Umbrella include for operator concepts and adapters.
Subspace construction and orthogonalization kernels.
Dense vector storage and operations.