LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
ctmc_gmres_multi.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_GMRES_MULTI_H
6#define LINE_API_MC_CTMC_GMRES_MULTI_H
7
8/**
9 * @file
10 * @ingroup api_mc
11 * Restarted GMRES for a block of right-hand sides sharing one coefficient
12 * matrix.
13 *
14 * Templated port of matlab/src/api/mc/ctmc_gmres_multi.m and the multi-column
15 * overload of jar/src/main/java/jline/api/mc/Ctmc_gmres.java. Every column is
16 * solved against the SAME equilibration, reordering and ILUT factorization, and
17 * each column starts from the previous column's solution: this is the shape of
18 * the stochastic complement, whose right-hand side is a whole block of the
19 * generator, and refactorizing per column would cost more than the direct solve
20 * the method replaces.
21 *
22 * FLAG is zero only when every column converged. On any other value the result
23 * matrix is empty and the caller must fall back to the direct solve; returning
24 * a partially converged block would leave the fallback ambiguous, which is the
25 * reference's rule and is kept.
26 *
27 * GATED ON TRANSCENDENTAL ARITHMETIC, for the reason given in ctmc_gmres: the
28 * iteration stops on a residual tolerance and normalizes by a Euclidean norm.
29 */
30
31#include <cstddef>
32#include <vector>
33
35#include "line/num/number.h"
36#include "line/util/error.h"
37#include "line/util/matrix.h"
38
39namespace line {
40namespace mc {
41
42template <class T>
44 Matrix<T> X; ///< solution block, empty unless flag == 0
45 int flag; ///< 0 all columns converged, otherwise the first failing flag
46};
47
48/**
49 * @brief Restarted GMRES for a block of right-hand sides sharing one
50 * coefficient matrix.
51 *
52 * @param A coefficient matrix
53 * @param B right-hand sides, one per column
54 * @param tol relative residual tolerance (default 1e-12)
55 * @param restart restart length; <= 0 selects min(n, 50)
56 * @param maxit outer cycles; <= 0 selects ceil(n / restart)
57 */
58template <class T>
59GmresMultiResult<T> ctmc_gmres_multi(const Matrix<T>& A, const Matrix<T>& B, double tol = 1e-12,
60 long restart = 0, long maxit = 0) {
62 "ctmc_gmres_multi requires transcendental arithmetic: see ctmc_gmres, the "
63 "iteration stops on a residual tolerance rather than reaching an exact value");
64 const std::size_t n = A.rows();
65 if (A.cols() != n) throw InputError("ctmc_gmres_multi: matrix is not square");
66 if (B.rows() != n) throw InputError("ctmc_gmres_multi: right-hand side block has the wrong height");
67
68 const detail::GmresPrepared<T> prep(A);
69 const std::size_t nrhs = B.cols();
71 std::vector<T> guess(n, num_traits<T>::from_int(1) / num_traits<T>::from_int(static_cast<long>(n)));
72 std::vector<T> rhs(n);
73
75 for (std::size_t c = 0; c < nrhs; ++c) {
76 for (std::size_t i = 0; i < n; ++i) rhs[i] = B(i, c);
77 const GmresResult<T> r = detail::gmres_solve(prep, rhs, guess, tol, restart, maxit);
78 if (r.flag != 0) {
79 out.X = Matrix<T>();
80 out.flag = r.flag;
81 return out;
82 }
83 for (std::size_t i = 0; i < n; ++i) X(i, c) = r.x[i];
84 guess = r.x;
85 }
86 out.X = X;
87 out.flag = 0;
88 return out;
89}
90
91} // namespace mc
92} // namespace line
93
94#endif // LINE_API_MC_CTMC_GMRES_MULTI_H
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
Restarted GMRES with an ILUT preconditioner, for the linear systems a generator produces.
The exception types the port throws.
Dense matrix and non-owning view.
GmresMultiResult< T > ctmc_gmres_multi(const Matrix< T > &A, const Matrix< T > &B, double tol=1e-12, long restart=0, long maxit=0)
Restarted GMRES for a block of right-hand sides sharing one coefficient matrix.
Number-type abstraction for the templated API port.
int flag
0 all columns converged, otherwise the first failing flag
Matrix< T > X
solution block, empty unless flag == 0
int flag
0 converged, 1 iteration limit, 3 stagnation/divergence
Definition ctmc_gmres.h:80
std::vector< T > x
solution
Definition ctmc_gmres.h:79