13_inverse_laplace_transform.cpp¶
Compute a probability distribution in time two ways, and check that they agree.
#include <algorithm>
#include <cmath>
#include <io/json.hpp>
#include <numerics.hpp>
using namespace num;
int main() {
constexpr idx n = 30;
mat<real> Q(n, n, 0.0);
for (idx j = 0; j < n; ++j) {
if (j + 1 < n) {
Q(j + 1, j) = 1.0;
Q(j, j) -= 1.0;
}
if (j > 0) {
Q(j - 1, j) = 0.6;
Q(j, j) -= 0.6;
}
}
vec<real> initial(n, 0.0);
initial[0] = 1.0;
const real time = 1.0;
const vec<real> reference = expv(time, operators::dense_op(Q), initial, 30, 1e-12);
const hessenberg_resolvent reduced(Q);
io::json results = io::json::array();
for (idx nodes : {8, 12, 16, 24, 32}) {
vec<cplx> result(n, 0.0);
for (const auto &[shift, weight] : talbot_contour(time, nodes)) {
const vec<cplx> solved = solve(num::shift(reduced, shift), initial);
for (idx i = 0; i < n; ++i)
result[i] += weight * solved[i];
}
vec<real> probability(n);
for (idx i = 0; i < n; ++i)
probability[i] = std::max(0.0, result[i].real());
clip_and_normalize_nonnegative(probability);
real error = 0.0;
for (idx i = 0; i < n; ++i) {
error = std::max(error, std::abs(probability[i] - reference[i]));
}
results.push_back({{"contour_nodes", nodes}, {"max_error", error}});
}
io::write_json(results, "result.json");
}
Saved result:
[
{
"contour_nodes": 8,
"max_error": 6.315807791764131e-05
},
{
"contour_nodes": 12,
"max_error": 4.54881358728024e-07
},
{
"contour_nodes": 16,
"max_error": 2.865651139221325e-09
},
{
"contour_nodes": 24,
"max_error": 9.540016771207016e-14
},
{
"contour_nodes": 32,
"max_error": 4.007905118896815e-14
}
]