LINE Solver (C++)
Templated C++ port of the LINE queueing solver
Loading...
Searching...
No Matches
trace_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_TRACE_TRACE_TYPES_H
6#define LINE_API_TRACE_TRACE_TYPES_H
7
8/**
9 * @file
10 * @ingroup api_trace
11 * Shared declarations for the empirical trace statistics domain.
12 *
13 * Templated port of the kpctoolbox trace primitives
14 * (matlab/lib/kpctoolbox/trace/, matlab/lib/m3a/m3a/mtrace/) and of
15 * jar/src/main/java/jline/api/trace/.
16 *
17 * CONVENTIONS
18 * - A single-class trace is a vector of inter-arrival (or service) times,
19 * passed as std::vector<T>.
20 * - A marked (multi-class) trace is the pair (T, A) with A a vector of
21 * integer class labels of the same length. The label alphabet is whatever
22 * distinct values occur in A, taken in increasing order; row c of every
23 * returned matrix refers to the c-th smallest label, exactly as MATLAB's
24 * `unique(A)` and the JAR's TreeSet do. Two JAR functions instead index by
25 * the raw label value (Mtrace_cov, Mtrace_joint use 1..max(A)); those
26 * divergences are documented on the individual headers.
27 *
28 * ARITHMETIC
29 * Sample moments, autocovariances, class-transition frequencies, counting
30 * processes and order statistics are sums, products and divisions of the
31 * data, so the whole of this domain except trace_skew, trace_summary and
32 * trace_gamma's residual comparison is a field computation and is
33 * instantiated for double, Rational and Real50. Only the functions that take
34 * a square root (skewness normalization, standard deviations in the summary)
35 * carry a has_transcendental static_assert.
36 */
37
38#include <algorithm>
39#include <cmath>
40#include <cstddef>
41#include <set>
42#include <vector>
43
44#include "line/num/number.h"
45#include "line/util/error.h"
46
47namespace line {
48namespace trace {
49
50namespace detail {
51
52/** exp(v), resolved by ADL. */
53template <class T>
54inline T num_exp(const T& v) {
55 using std::exp;
56 return exp(v);
57}
58
59/** log(v), resolved by ADL. */
60template <class T>
61inline T num_log(const T& v) {
62 using std::log;
63 return log(v);
64}
65
66/** sqrt(v), resolved by ADL. */
67template <class T>
68inline T num_sqrt(const T& v) {
69 using std::sqrt;
70 return sqrt(v);
71}
72
73/** base^exponent for a real-valued exponent, resolved by ADL. */
74template <class T>
75inline T num_pow(const T& base, const T& exponent) {
76 using std::pow;
77 return pow(base, exponent);
78}
79
80/** The distinct class labels in increasing order, i.e. MATLAB's unique(A). */
81inline std::vector<int> unique_labels(const std::vector<int>& A) {
82 const std::set<int> s(A.begin(), A.end());
83 return std::vector<int>(s.begin(), s.end());
84}
85
86/** Throws unless the trace is non-empty. */
87template <class T>
88inline void require_nonempty(const std::vector<T>& S, const char* who) {
89 if (S.empty()) throw InputError(std::string(who) + ": the trace is empty");
90}
91
92/** Throws unless times and labels have the same length. */
93template <class T>
94inline void require_marked(const std::vector<T>& S, const std::vector<int>& A, const char* who) {
95 require_nonempty(S, who);
96 if (S.size() != A.size())
97 throw InputError(std::string(who) + ": times and class labels have different lengths");
98}
99
100/**
101 * Cumulative sums with a leading zero: cs[k] is the sum of the first k
102 * entries, so cs has size n+1 and cs[0] = 0. This is MATLAB's [0; cumsum(S)]
103 * and lets the 1-based index arithmetic of the reference sources be
104 * transcribed literally.
105 */
106template <class T>
107inline std::vector<T> cumsum0(const std::vector<T>& S) {
108 std::vector<T> cs(S.size() + 1, num_traits<T>::from_int(0));
109 for (std::size_t i = 0; i < S.size(); ++i) cs[i + 1] = cs[i] + S[i];
110 return cs;
111}
112
113} // namespace detail
114} // namespace trace
115} // namespace line
116
117#endif // LINE_API_TRACE_TRACE_TYPES_H
InputError(const std::string &what)
Definition error.h:39
The exception types the port throws.
Number-type abstraction for the templated API port.