LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
ctmc_timereverse.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012-2026, QORE Lab, Imperial College London
3 * All rights reserved.
4 */
5#ifndef LINE_API_MC_CTMC_TIMEREVERSE_H
6#define LINE_API_MC_CTMC_TIMEREVERSE_H
7
8/**
9 * @file
10 * @ingroup api_mc
11 * Time-reversed generator and transition matrix.
12 *
13 * Templated port of matlab/lib/kpctoolbox/mc/ctmc_timereverse.m and
14 * dtmc_timereverse.m. The reversed chain is Qrev = diag(pi)^-1 Q' diag(pi),
15 * written here as the transpose of the elementwise scaling Q(i,j) pi_i / pi_j,
16 * which is the same matrix and is how MATLAB assembles it.
17 *
18 * The construction needs the stationary law, so it is defined only for an
19 * irreducible chain: on a reducible one the stationary vector is not unique and
20 * the reversal is not either.
21 */
22
23#include <cstddef>
24#include <vector>
25
28#include "line/num/number.h"
29#include "line/util/error.h"
30#include "line/util/matrix.h"
31
32namespace line {
33namespace mc {
34
35/** Generator of the time-reversed CTMC. */
36template <class T>
38 const std::size_t n = Q.rows();
39 if (Q.cols() != n) throw InputError("ctmc_timereverse: Q is not square");
40 const std::vector<T> pie = ctmc_solve(Q);
42 for (std::size_t i = 0; i < n; ++i)
43 for (std::size_t j = 0; j < n; ++j) Qrev(j, i) = Q(i, j) * pie[i] / pie[j];
44 return Qrev;
45}
46
47/** Transition matrix of the time-reversed DTMC. */
48template <class T>
50 const std::size_t n = P.rows();
51 if (P.cols() != n) throw InputError("dtmc_timereverse: P is not square");
52 const std::vector<T> pie = dtmc_solve(P);
54 for (std::size_t i = 0; i < n; ++i)
55 for (std::size_t j = 0; j < n; ++j) Prev(j, i) = P(i, j) * pie[i] / pie[j];
56 return Prev;
57}
58
59} // namespace mc
60} // namespace line
61
62#endif
InputError(const std::string &what)
Definition error.h:39
std::size_t cols() const
Definition matrix.h:90
std::size_t rows() const
Definition matrix.h:89
Steady-state distribution of a continuous-time Markov chain.
Equilibrium distribution of a discrete-time Markov chain, and stochastic complementation.
The exception types the port throws.
Dense matrix and non-owning view.
std::vector< T > ctmc_solve(const Matrix< T > &Qin)
Steady-state distribution of a continuous-time Markov chain.
Definition ctmc_solve.h:122
Matrix< T > dtmc_timereverse(const Matrix< T > &P)
Transition matrix of the time-reversed DTMC.
std::vector< T > dtmc_solve(const Matrix< T > &P)
Stationary distribution of a stochastic matrix P.
Definition dtmc_solve.h:106
Matrix< T > ctmc_timereverse(const Matrix< T > &Q)
Generator of the time-reversed CTMC.
Number-type abstraction for the templated API port.