LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
qsys_types.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_QSYS_TYPES_H
6#define LINE_API_QSYS_TYPES_H
7
8/**
9 * @file
10 * @ingroup api_qsys
11 * Shared return type and arithmetic helpers for the templated qsys port.
12 *
13 * The MATLAB closed-form queueing-system functions in matlab/src/api/qsys/
14 * almost all return the pair [W,rhohat], mirrored in the JAR by Ret.qsys.
15 * This header carries that pair plus the handful of ADL wrappers the family
16 * needs; each ported function lives in its own header named after the MATLAB
17 * file, as required by the port convention.
18 */
19
20#include <cmath>
21#include <limits>
22#include <string>
23
24#include "line/num/number.h"
25#include "line/util/error.h"
26
27namespace line {
28namespace qsys {
29
30/**
31 * Return value of the qsys family, mirroring MATLAB's [W,rhohat] and the
32 * JAR's Ret.qsys.
33 *
34 * W mean response time (time in system, service included)
35 * rhohat modified utilization chosen so that the M/M/1 relations still hold
36 */
37template <class T>
38struct QsysResult {
39 T W;
41};
42
43namespace detail {
44
45/** exp(v), resolved by ADL so double, cpp_bin_float and mpfr all work. */
46template <class T>
47inline T num_exp(const T& v) {
48 using std::exp;
49 return exp(v);
50}
51
52/** sqrt(v), resolved by ADL. */
53template <class T>
54inline T num_sqrt(const T& v) {
55 using std::sqrt;
56 return sqrt(v);
57}
58
59/** base^exponent for a real-valued exponent, resolved by ADL. */
60template <class T>
61inline T num_pow(const T& base, const T& exponent) {
62 using std::pow;
63 return pow(base, exponent);
64}
65
66template <class T>
67inline T num_min(const T& a, const T& b) {
68 return a < b ? a : b;
69}
70
71/**
72 * Guard against the exact pole at rho == 1.
73 *
74 * MATLAB divides by 1-rho and returns +-Inf/NaN there, and so does any type
75 * with an IEEE infinity (double, Real50), which therefore evaluates the formula
76 * as MATLAB does. In an exact field (Rational) that division has no value at
77 * all, so only such a type reports it as an input error. Nothing else is
78 * checked: rho > 1 still returns MATLAB's negative W, since the MATLAB
79 * functions do not reject it either.
80 */
81template <class E>
82inline void require_no_pole(const E& one_minus_rho, const char* fn) {
83 // E is deduced, not fixed to T, so a Boost expression template binds here
84 // without being materialized first (and, having no numeric_limits, keeps
85 // the refusal).
86 if (!std::numeric_limits<E>::has_infinity && one_minus_rho == 0)
87 throw InputError(std::string(fn) + ": rho == 1, the mean waiting time is undefined");
88}
89
90/**
91 * Saturation test for the closed-form single-queue formulas.
92 *
93 * At rho >= 1 there is no steady state and the mean response time is
94 * unbounded, so every codebase returns W = Inf there (MATLAB, JAR and python
95 * alike, since 2026-11-23) instead of evaluating the formula, which would give
96 * a meaningless negative W. Returns true for a type with an IEEE infinity
97 * (double, Real50), whose caller then returns Inf; a type without one
98 * (Rational) cannot represent the answer, so it raises instead.
99 */
100template <class T>
101inline bool saturated(const T& rho, const char* fn) {
102 if (rho < num_traits<T>::from_int(1)) return false;
103 if constexpr (std::numeric_limits<T>::has_infinity) {
104 return true;
105 } else {
106 throw InputError(std::string(fn) +
107 ": rho >= 1, the mean response time is unbounded and this type has no infinity");
108 }
109}
110
111/** +Inf of a type that has one (see saturated()). */
112template <class T>
113inline T num_inf() {
114 return std::numeric_limits<T>::infinity();
115}
116
117/** rhohat = W*lambda/(1+W*lambda), the closing line of most qsys functions. */
118template <class T>
119inline T rhohat_from_W(const T& W, const T& lambda) {
120 const T Wl = W * lambda;
121 return Wl / (num_traits<T>::from_int(1) + Wl);
122}
123
124} // namespace detail
125
126} // namespace qsys
127} // namespace line
128
129#endif // LINE_API_QSYS_TYPES_H
InputError(const std::string &what)
Definition error.h:105
The exception types the port throws.
Conservation laws of a layered queueing network, enumerated from its structure.
Definition aoi_dist2ph.h:51
Number-type abstraction for the templated API port.
Return value of the qsys family, mirroring MATLAB's [W,rhohat] and the JAR's Ret.qsys.
Definition qsys_types.h:38