5#ifndef LINE_API_MC_CTMC_SOLVE_H
6#define LINE_API_MC_CTMC_SOLVE_H
62 const std::size_t n = Q.
rows();
63 if (Q.
cols() != n)
throw InputError(
"ctmc_makeinfgen: generator is not square");
66 for (std::size_t i = 0; i < n; ++i) {
69 for (std::size_t j = 0; j < n; ++j) s += R(i, j);
79std::vector<std::vector<std::size_t>> weak_components(
const Matrix<T>& Q) {
80 const std::size_t n = Q.
rows();
82 std::vector<int> comp(n, -1);
83 std::vector<std::vector<std::size_t>> out;
84 for (std::size_t s = 0; s < n; ++s) {
85 if (comp[s] >= 0)
continue;
86 std::vector<std::size_t> stack{s}, members;
87 comp[s] =
static_cast<int>(out.size());
88 while (!stack.empty()) {
89 const std::size_t u = stack.back();
92 for (std::size_t v = 0; v < n; ++v) {
93 if (v == u || comp[v] >= 0)
continue;
94 if (Q(u, v) != zero || Q(v, u) != zero) {
100 std::sort(members.begin(), members.end());
101 out.push_back(members);
108Matrix<T> submatrix(
const Matrix<T>& Q,
const std::vector<std::size_t>& idx) {
109 Matrix<T> S(idx.size(), idx.size());
110 for (std::size_t a = 0; a < idx.size(); ++a)
111 for (std::size_t b = 0; b < idx.size(); ++b) S(a, b) = Q(idx[a], idx[b]);
134 const std::size_t n = Qin.
rows();
135 if (Qin.
cols() != n)
throw InputError(
"ctmc_solve: generator is not square");
139 if (n == 0)
throw InputError(
"ctmc_solve: empty generator");
140 if (n == 1)
return std::vector<T>{one};
145 for (std::size_t i = 0; i < n && allZero; ++i)
146 for (std::size_t j = 0; j < n; ++j)
147 if (Q(i, j) != zero) {
158 const std::vector<std::vector<std::size_t>> comps = detail::weak_components(Q);
159 if (comps.size() > 1) {
160 std::vector<T> pi(n, zero);
161 for (
const std::vector<std::size_t>& c : comps) {
163 for (std::size_t k = 0; k < c.size(); ++k) pi[c[k]] = pc[k];
166 for (
const T& v : pi) s += v;
168 for (T& v : pi) v /= s;
183 std::vector<std::size_t> keep(n);
184 for (std::size_t i = 0; i < n; ++i) keep[i] = i;
187 const std::size_t m = Qk.
rows();
188 std::vector<std::size_t> active;
189 for (std::size_t i = 0; i < m; ++i) {
191 for (std::size_t j = 0; j < m; ++j) {
192 if (Qk(j, i) != zero) { colNz =
true;
break; }
194 if (colNz) active.push_back(i);
198 "ctmc_solve: the generator has no connected state, every state was eliminated as "
199 "isolated; it admits no unique stationary distribution");
200 if (active.size() == m)
break;
201 std::vector<std::size_t> keep2(active.size());
202 for (std::size_t k = 0; k < active.size(); ++k) keep2[k] = keep[active[k]];
210 if (linsolver ==
"gth") {
213 std::vector<T> pi(n, zero);
214 for (std::size_t k = 0; k < Qk.
rows(); ++k) pi[keep[k]] = g.
p[k];
226 std::vector<T> pi(n, zero);
227 for (std::size_t k = 0; k < Qk.
rows(); ++k) pi[keep[k]] = c.
p[k];
234 const std::size_t m = Qk.
rows();
236 for (std::size_t i = 0; i < m; ++i)
237 for (std::size_t j = 0; j < m; ++j) A(i, j) = (i == m - 1) ? one : Qk(j, i);
238 std::vector<T> b(m, zero);
253 if (bs.
flag == 0) x = bs.
x;
257 if (x.empty()) x =
solve(A, b);
259 std::vector<T> pi(n, zero);
260 for (std::size_t k = 0; k < m; ++k) pi[keep[k]] = x[k];
NumericError(const std::string &what)
Preconditioned stabilized biconjugate gradients, for the linear systems a generator produces.
Classical stationary iterative solvers for pi Q = 0, sum(pi) = 1: the power method on the uniformised...
Restarted GMRES with an ILUT preconditioner, for the linear systems a generator produces.
Grassmann-Taksar-Heyman (GTH) elimination for the stationary vector, and the limit matrix J = lim exp...
The exception types the port throws.
LU factorization with partial pivoting, templated on the number type.
Dense matrix and non-owning view.
std::vector< T > ctmc_solve(const Matrix< T > &Qin, const std::string &linsolver=std::string())
Steady-state distribution of a continuous-time Markov chain.
bool is_classical_linsolver(const std::string &name)
Whether a linsolver name selects one of the classical solvers, as accepted by ctmc_solve in all four ...
BicgstabResult< T > ctmc_bicgstab(const Matrix< T > &A, const std::vector< T > &b, double tol=1e-12, long maxit=0, const std::vector< T > &x0=std::vector< T >())
Preconditioned stabilized biconjugate gradients, for the linear systems a generator produces.
Matrix< T > ctmc_makeinfgen(const Matrix< T > &Q)
Set the diagonal so that every row sums to zero (ctmc_makeinfgen).
constexpr std::size_t GMRES_MIN_STATES
Order above which the direct sparse factorization is abandoned in favour of the Krylov path.
GthResult< T > ctmc_gth_flag(const Matrix< T > &Q)
Stationary vector of a CTMC by GTH elimination, with a flag.
ClassicalResult< T > ctmc_classical_by_name(const std::string &name, const Matrix< T > &Q)
Run the classical solver named by is_classical_linsolver with defaults.
GmresResult< T > ctmc_gmres(const Matrix< T > &A, const std::vector< T > &b, double tol=1e-12, long restart=0, long maxit=0, const std::vector< T > &x0=std::vector< T >())
Restarted GMRES with an ILUT preconditioner, for the linear systems a generator produces.
Conservation laws of a layered queueing network, enumerated from its structure.
std::vector< T > solve(const Matrix< T > &A, const std::vector< T > &b)
Convenience: solve Ax = b, leaving A and b untouched.
Number-type abstraction for the templated API port.
The <kind> part of a LINE:<category>:<kind> identifier (see _kb/17-error-vocabulary....
std::vector< T > x
solution
int flag
0 converged, 1 iteration limit, 3 stagnation, 4 breakdown
int flag
0 converged, 1 iteration cap, 2 zero diagonal, 4 breakdown
std::vector< T > p
stationary distribution estimate, sums to one
int flag
0 converged, 1 iteration limit, 3 stagnation/divergence
std::vector< T > x
solution
int flag
0 success, 1 several closed classes (not unique)
std::vector< T > p
stationary vector (empty when flag == 1)