LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
mmap_backward_moment.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_MAM_MMAP_BACKWARD_MOMENT_H
6#define LINE_API_MAM_MMAP_BACKWARD_MOMENT_H
7
8/**
9 * @file
10 * @ingroup api_mam
11 * Class-conditional backward moments of an MMAP
12 * (matlab/lib/m3a/m3a/mmap/mmap_backward_moment.m).
13 *
14 * Its own header, and not part of mmap_compress.h, because the fitting
15 * families mmap_compress dispatches to (maph2m, mamap2m, mamap22) take backward
16 * moments themselves: had they kept including mmap_compress.h, mmap_compress
17 * could not include them back.
18 */
19
20#include <cstddef>
21#include <vector>
22
26#include "line/num/number.h"
27#include "line/util/error.h"
28#include "line/util/linalg.h"
29#include "line/util/matrix.h"
30
31namespace line {
32namespace mam {
33
34/**
35 * Class-conditional backward moments of an MMAP (mmap_backward_moment.m).
36 *
37 * @param orders the moment orders to compute
38 * @param normalized true for B(c,k) with M_k = sum_c B(c,k) p_c, i.e. divided
39 * by the class probability p_c (the MATLAB default); false for the
40 * unnormalized form with M_k = sum_c B(c,k)
41 * @param m the marked MAP whose backward moments are taken
42 * @return B[c][h], the moment of order orders[h] for class c
43 */
44template <class T>
45std::vector<std::vector<T>> mmap_backward_moment(const Mmap<T>& m,
46 const std::vector<unsigned>& orders,
47 bool normalized) {
48 const std::size_t n = m.order();
49 const std::size_t C = m.classes();
50 const T zero = num_traits<T>::from_int(0);
51 const std::vector<T> pie = map_pie(m.map());
52 Matrix<T> negD0 = m.D0;
53 for (std::size_t i = 0; i < n; ++i)
54 for (std::size_t j = 0; j < n; ++j) negD0(i, j) = -negD0(i, j);
55 const Matrix<T> Minv = inverse(negD0);
56
57 std::vector<std::vector<T>> B(C, std::vector<T>(orders.size(), zero));
58 for (std::size_t c = 0; c < C; ++c) {
60 if (normalized) {
61 const std::vector<T> t = vecmul(vecmul(pie, Minv), m.Dc[c]);
62 pa = zero;
63 for (const T& v : t) pa += v;
64 if (pa == zero)
65 throw NumericError(
66 "mmap_backward_moment: class with zero arrival probability cannot be "
67 "normalized");
68 }
69 for (std::size_t h = 0; h < orders.size(); ++h) {
70 const unsigned k = orders[h];
71 const std::vector<T> t = vecmul(vecmul(pie, matpow(Minv, k + 1)), m.Dc[c]);
72 T s = zero;
73 for (const T& v : t) s += v;
74 B[c][h] = num_factorial<T>(k) / pa * s;
75 }
76 }
77 return B;
78}
79
80/** mmap_backward_moment with the MATLAB default, normalized. */
81template <class T>
82std::vector<std::vector<T>> mmap_backward_moment(const Mmap<T>& m,
83 const std::vector<unsigned>& orders) {
84 return mmap_backward_moment(m, orders, true);
85}
86
87} // namespace mam
88} // namespace line
89
90#endif // LINE_API_MAM_MMAP_BACKWARD_MOMENT_H
NumericError(const std::string &what)
Definition error.h:45
The exception types the port throws.
Dense linear algebra over the templated number type: products, identity, inverse, and powers.
Markovian arrival process descriptors: stationary vectors, rate, moments, autocorrelation and the ind...
MAP constructors and structural transformations.
Dense matrix and non-owning view.
Marked MAP (MMAP) algebra: per-class rates, class probabilities, superposition, normalization and sca...
std::vector< std::vector< T > > mmap_backward_moment(const Mmap< T > &m, const std::vector< unsigned > &orders, bool normalized)
Class-conditional backward moments of an MMAP (mmap_backward_moment.m).
std::vector< T > map_pie(const Map< T > &m)
Phase distribution seen by an arriving job, pie = pi D1 / (pi D1 e).
Definition map_moment.h:89
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:52
T num_factorial(unsigned n)
Factorial as a value of T.
Definition number.h:210
std::vector< T > vecmul(const std::vector< T > &v, const Matrix< T > &A)
Row vector times matrix, v A.
Definition linalg.h:50
Matrix< T > inverse(const Matrix< T > &A)
Inverse by LU with one factorization and n back substitutions.
Definition linalg.h:72
Matrix< T > matpow(const Matrix< T > &A, unsigned k)
Integer matrix power, by repeated squaring.
Definition linalg.h:89
Number-type abstraction for the templated API port.
An MMAP: the underlying MAP plus the per-class arrival matrices.
Definition mmap_lambda.h:45
Map< T > map() const
Definition mmap_lambda.h:52
std::size_t classes() const
Definition mmap_lambda.h:51
Matrix< T > D0
Definition mmap_lambda.h:46
std::vector< Matrix< T > > Dc
per-class matrices, sum_c Dc = D1
Definition mmap_lambda.h:48
std::size_t order() const
Definition mmap_lambda.h:50