82 const std::vector<double>& t,
double tol = 1e-14,
83 const std::string& method_in =
"auto") {
84 using namespace detail;
85 const char* fn =
"qsys_mm1k_transient";
86 if (!std::isfinite(lambda) || lambda <= 0) transient_arg_error(fn,
"lambda must be finite and positive");
87 if (!std::isfinite(mu) || mu <= 0) transient_arg_error(fn,
"mu must be finite and positive");
88 if (!transient_is_int(K) || K < 1) transient_arg_error(fn,
"K must be an integer >= 1");
89 if (!transient_is_int(n0) || n0 < 0) transient_arg_error(fn,
"n0 must be an integer >= 0");
90 if (n0 > K) transient_arg_error(fn,
"n0 must not exceed K");
91 if (t.empty()) transient_arg_error(fn,
"t must be a nonempty vector of finite nonnegative times");
93 if (!std::isfinite(tv) || tv < 0)
94 transient_arg_error(fn,
"t must be a nonempty vector of finite nonnegative times");
95 if (!std::isfinite(tol) || tol <= 0 || tol >= 1) transient_arg_error(fn,
"tol must lie in (0, 1)");
96 std::string method = method_in;
97 std::transform(method.begin(), method.end(), method.begin(),
98 [](
unsigned char c) { return static_cast<char>(std::tolower(c)); });
99 if (method !=
"auto" && method !=
"spectral" && method !=
"uniformization")
100 transient_arg_error(fn,
"method must be 'auto', 'spectral' or 'uniformization'");
101 const std::size_t Kn =
static_cast<std::size_t
>(K);
102 const std::size_t i =
static_cast<std::size_t
>(n0);
103 const std::size_t nt = t.size();
104 const double rho = lambda / mu, sr = std::sqrt(rho), logrho = std::log(rho);
110 res.
pi.assign(Kn + 1, 0.0);
112 for (
double& v : res.
pi) v = 1.0 /
static_cast<double>(Kn + 1);
114 double emax = -std::numeric_limits<double>::infinity();
115 for (std::size_t j = 0; j <= Kn; ++j) emax = std::max(emax, static_cast<double>(j) * logrho);
117 for (std::size_t j = 0; j <= Kn; ++j) s += (res.
pi[j] = std::exp(
static_cast<double>(j) * logrho - emax));
118 for (
double& v : res.
pi) v /= s;
120 std::vector<double> th(Kn), den(Kn);
121 for (std::size_t h = 0; h < Kn; ++h) {
122 th[h] =
static_cast<double>(h + 1) * kTransientPi /
static_cast<double>(Kn + 1);
123 den[h] = 1.0 + rho - 2.0 * sr * std::cos(th[h]);
126 for (std::size_t j = 0; j <= Kn; ++j)
127 A = std::max(A, std::exp(0.5 * (
static_cast<double>(j) -
static_cast<double>(i)) * logrho));
129 for (std::size_t h = 0; h < Kn; ++h)
130 ksum += 2.0 * (1.0 + sr) * (1.0 + sr) / (
static_cast<double>(Kn + 1) * den[h]);
131 res.
kappa = A * ksum + 1.0;
132 if (method ==
"auto") method = res.
kappa * kTransientEps <= tol ?
"spectral" :
"uniformization";
136 if (method ==
"spectral") {
137 std::vector<double> g(Kn);
138 for (std::size_t h = 0; h < Kn; ++h) g[h] = -(lambda + mu) + 2.0 * std::sqrt(lambda * mu) * std::cos(th[h]);
140 for (std::size_t j = 0; j <= Kn; ++j)
141 for (std::size_t h = 0; h < Kn; ++h)
142 W(j, h) = sr * std::sin(
static_cast<double>(j + 1) * th[h]) - std::sin(
static_cast<double>(j) * th[h]);
143 std::vector<double> pref(Kn + 1), coef(Kn);
144 for (std::size_t j = 0; j <= Kn; ++j)
145 pref[j] = std::exp(0.5 * (
static_cast<double>(j) -
static_cast<double>(i)) * logrho);
146 for (std::size_t h = 0; h < Kn; ++h) coef[h] = W(i, h) / den[h];
147 for (std::size_t r = 0; r < nt; ++r) {
148 std::vector<double> e(Kn);
149 for (std::size_t h = 0; h < Kn; ++h) e[h] = std::exp(g[h] * t[r]) * coef[h];
150 for (std::size_t j = 0; j <= Kn; ++j) {
152 for (std::size_t h = 0; h < Kn; ++h) s += W(j, h) * e[h];
153 res.
P(r, j) = res.
pi[j] + 2.0 /
static_cast<double>(Kn + 1) * pref[j] * s;
158 const double Lam = lambda + mu, p = lambda / Lam, q = mu / Lam;
159 std::vector<PoissonWindow>
ws(nt);
161 for (std::size_t r = 0; r < nt; ++r) {
162 ws[r] = transient_poiswin(Lam * t[r], tol);
164 nRmax = std::max(nRmax,
ws[r].nR);
166 std::vector<double> v(Kn + 1, 0.0), vn(Kn + 1, 0.0);
168 for (
long n = 0; n <= nRmax; ++n) {
169 for (std::size_t r = 0; r < nt; ++r)
170 if (n >=
ws[r].nL && n <=
ws[r].nR) {
171 const double wn =
ws[r].w[
static_cast<std::size_t
>(n -
ws[r].nL)];
172 for (std::size_t j = 0; j <= Kn; ++j) res.
P(r, j) += wn * v[j];
175 vn[0] = q * v[0] + q * v[1];
176 for (std::size_t j = 1; j < Kn; ++j) vn[j] = p * v[j - 1] + q * v[j + 1];
177 vn[Kn] = p * v[Kn - 1] + p * v[Kn];
182 res.
EN.assign(nt, 0.0);
183 res.
P0.assign(nt, 0.0);
184 for (std::size_t r = 0; r < nt; ++r) {
185 for (std::size_t j = 0; j <= Kn; ++j) res.
EN[r] += res.
P(r, j) *
static_cast<double>(j);
186 res.
P0[r] = res.
P(r, 0);