5#ifndef LINE_API_QSYS_QSYS_MG1_PS_H
6#define LINE_API_QSYS_QSYS_MG1_PS_H
90using Cplx = std::complex<double>;
94 std::vector<double>
x;
95 std::vector<double>
s;
96 std::vector<double>
t;
99 std::function<double(
double)>
pdf;
116 double m2 = std::numeric_limits<double>::quiet_NaN();
121 std::vector<double>
x,
s,
t;
128 double m2Uncond = std::numeric_limits<double>::quiet_NaN();
129 double varUncond = std::numeric_limits<double>::quiet_NaN();
134namespace mg1psdetail {
137inline std::vector<double> conv(
const std::vector<double>& a,
const std::vector<double>& b) {
138 if (a.empty() || b.empty())
return std::vector<double>();
139 std::vector<double> c(a.size() + b.size() - 1, 0.0);
140 for (std::size_t i = 0; i < a.size(); ++i)
141 for (std::size_t j = 0; j < b.size(); ++j) c[i + j] += a[i] * b[j];
146template <
class C,
class U>
147U polyval(
const std::vector<C>& p,
const U& z) {
149 for (std::size_t i = 0; i < p.size(); ++i) v = v * z + U(p[i]);
154inline std::vector<double> polyder(
const std::vector<double>& p) {
155 if (p.size() <= 1)
return std::vector<double>(1, 0.0);
156 const std::size_t n = p.size() - 1;
157 std::vector<double> d(n, 0.0);
158 for (std::size_t i = 0; i < n; ++i) d[i] = p[i] * static_cast<double>(n - i);
169inline std::vector<Cplx> poly_roots(
const std::vector<double>& p_in) {
170 std::vector<double> p = p_in;
172 std::size_t lead = 0;
173 while (lead + 1 < p.size() && p[lead] == 0.0) ++lead;
174 p.erase(p.begin(), p.begin() +
static_cast<long>(lead));
175 if (p.size() <= 1)
return std::vector<Cplx>();
176 const std::size_t n = p.size() - 1;
177 std::vector<double> mon(p.size());
178 for (std::size_t i = 0; i < p.size(); ++i) mon[i] = p[i] / p[0];
182 std::vector<Cplx> r(n);
183 const Cplx seed(0.4, 0.9);
185 for (std::size_t i = 0; i < n; ++i) {
189 for (
int it = 0; it < 1000; ++it) {
191 for (std::size_t i = 0; i < n; ++i) {
193 for (std::size_t j = 0; j < n; ++j)
194 if (j != i) den *= (r[i] - r[j]);
195 if (std::abs(den) < 1e-300)
continue;
196 const Cplx step = polyval(mon, r[i]) / den;
198 move = std::max(move, std::abs(step));
200 if (move < 1e-14)
break;
214inline Matrix<Cplx> expm_cplx(
const Matrix<Cplx>& A) {
215 const std::size_t n = A.rows();
217 for (std::size_t i = 0; i < n; ++i) {
219 for (std::size_t j = 0; j < n; ++j) row += std::abs(A(i, j));
220 nrm = std::max(nrm, row);
227 const double sc = std::pow(2.0, -
static_cast<double>(sq));
228 Matrix<Cplx> B(n, n,
Cplx(0.0, 0.0)), E(n, n,
Cplx(0.0, 0.0)), Tk(n, n,
Cplx(0.0, 0.0));
229 for (std::size_t i = 0; i < n; ++i) {
230 E(i, i) =
Cplx(1.0, 0.0);
231 Tk(i, i) =
Cplx(1.0, 0.0);
232 for (std::size_t j = 0; j < n; ++j) B(i, j) = A(i, j) * sc;
234 for (
int k = 1; k <= 30; ++k) {
235 Matrix<Cplx>
P(n, n,
Cplx(0.0, 0.0));
236 for (std::size_t i = 0; i < n; ++i)
237 for (std::size_t j = 0; j < n; ++j) {
239 for (std::size_t l = 0; l < n; ++l) acc += Tk(i, l) * B(l, j);
240 P(i, j) = acc /
static_cast<double>(k);
243 for (std::size_t i = 0; i < n; ++i)
244 for (std::size_t j = 0; j < n; ++j) E(i, j) += Tk(i, j);
246 for (
int t = 0; t < sq; ++t) {
247 Matrix<Cplx>
P(n, n,
Cplx(0.0, 0.0));
248 for (std::size_t i = 0; i < n; ++i)
249 for (std::size_t j = 0; j < n; ++j) {
251 for (std::size_t l = 0; l < n; ++l) acc += E(i, l) * E(l, j);
267Cplx ilt(Fn fun,
double t, std::size_t nterms) {
268 const std::size_t n = 2 * ((nterms - 1) / 2) + 1;
272 for (std::size_t k = 0; k < n; ++k) {
273 const Cplx bj = beta[k] / t;
274 g += 0.5 * eta[k] * (fun(bj) + fun(std::conj(bj)));
285inline void quad_nodes(
double ymax, std::vector<double>* y, std::vector<double>* w,
286 std::size_t npanel = 8, std::size_t ng = 32) {
289 std::vector<double> edges;
290 edges.push_back(0.0);
291 for (
long k = -
static_cast<long>(npanel); k <= 0; ++k)
292 edges.push_back(ymax * std::pow(2.0,
static_cast<double>(k)));
293 for (std::size_t k = 0; k + 1 < edges.size(); ++k) {
294 std::vector<double> xg, wg;
295 pfqn::detail::gauss_legendre<double>(ng, edges[k], edges[k + 1], xg, wg);
296 for (std::size_t i = 0; i < xg.size(); ++i) {
312double second_moment(Fn lst,
double meanref,
double meanscale) {
313 if (!(meanref > 0.0))
return 0.0;
314 const double h = std::min(1e-2 / meanref, 1.0 / meanscale);
315 auto d2 = [&lst](
double hh) {
317 for (
int j = 0; j < 6; ++j) f[j] = std::real(lst(Cplx(static_cast<double>(j) * hh, 0.0)));
318 return (45.0 * f[0] - 154.0 * f[1] + 214.0 * f[2] - 156.0 * f[3] + 61.0 * f[4] -
322 return (16.0 * d2(h / 2.0) - d2(h)) / 15.0;
335inline Mg1PsResult mg1ps_finish(
double lambda,
double rho,
double m1,
double m2,
336 const std::function<
Cplx(
const Cplx&)>& bhat,
337 const std::function<
Cplx(
const Cplx&)>& bhatTag,
338 const std::function<
Cplx(
const Cplx&,
double,
double*)>& denom,
339 const std::function<
double(
double)>& bpdf,
double ymax,
bool isPH,
340 const Mg1PsOptions& opt) {
341 const bool hasPdf =
static_cast<bool>(bpdf);
342 const double nan = std::numeric_limits<double>::quiet_NaN();
348 res.meanUncond = m1 / (1.0 - rho);
349 res.atomUncond = (1.0 - rho) * bhatTag(
Cplx(lambda, 0.0)).real();
351 auto lst_cond = [denom, rho](
const Cplx& s,
double x,
bool excess) {
352 if (x == 0.0)
return Cplx(1.0, 0.0);
354 const Cplx val = denom(s, x, &scale);
355 const Cplx expo = (excess ? s :
Cplx(0.0, 0.0)) -
Cplx(scale, 0.0);
356 return Cplx(1.0 - rho, 0.0) * std::exp(expo * x) / val;
358 res.lstCond = [lst_cond](
Cplx s,
double x) {
return lst_cond(s, x,
false); };
359 res.lstExcess = [lst_cond](
Cplx s,
double x) {
return lst_cond(s, x,
true); };
360 res.dominantRoot = [lambda, bhat, rho](
Cplx s) {
362 const int maxit = std::max(200,
static_cast<int>(std::ceil(
363 3.0 * std::log(1e-15) / std::log(std::max(rho, 1e-3)))));
364 for (
int it = 0; it < maxit; ++it) {
365 const Cplx nx = s +
Cplx(lambda, 0.0) * (
Cplx(1.0, 0.0) - bhat(tau));
366 if (std::abs(nx - tau) <= 1e-14 * std::max(1.0, std::abs(nx)))
return nx;
369 throw InputError(
"qsys_mg1_ps: the dominant root iteration did not converge");
374 std::vector<double> yq, wq, bq;
375 quad_nodes(ymax, &yq, &wq);
377 bq.resize(yq.size());
378 for (std::size_t k = 0; k < yq.size(); ++k) bq[k] = bpdf(yq[k]);
380 res.lstUncond = [lst_cond, yq, wq, bq](
Cplx s) {
382 throw InputError(
"qsys_mg1_ps: the service density is required to remove the "
383 "conditioning, pass it as the pdf option");
385 for (std::size_t k = 0; k < yq.size(); ++k)
386 v += wq[k] * bq[k] * lst_cond(s, yq[k],
false);
393 res.lstCondVal = Matrix<double>(res.x.size(), res.s.size(), 0.0);
394 for (std::size_t i = 0; i < res.x.size(); ++i)
395 for (std::size_t j = 0; j < res.s.size(); ++j)
396 res.lstCondVal(i, j) = res.lstCond(
Cplx(res.s[j], 0.0), res.x[i]).real();
397 res.lstUncondVal.assign(res.s.size(), nan);
399 for (std::size_t j = 0; j < res.s.size(); ++j)
400 res.lstUncondVal[j] = res.lstUncond(
Cplx(res.s[j], 0.0)).real();
402 res.atomCond.assign(res.x.size(), 0.0);
403 res.meanCond.assign(res.x.size(), 0.0);
404 res.m2Cond.assign(res.x.size(), 0.0);
405 res.varCond.assign(res.x.size(), 0.0);
406 for (std::size_t i = 0; i < res.x.size(); ++i) {
407 res.atomCond[i] = (1.0 - rho) * std::exp(-lambda * res.x[i]);
408 res.meanCond[i] = res.x[i] / (1.0 - rho);
409 const double xi = res.x[i];
410 res.m2Cond[i] = second_moment([&res, xi](
Cplx u) {
return res.lstCond(u, xi); },
411 res.meanCond[i], res.meanUncond);
412 res.varCond[i] = res.m2Cond[i] - res.meanCond[i] * res.meanCond[i];
422 for (std::size_t k = 0; k < yq.size(); ++k) {
423 const double yk = yq[k];
424 m2u += wq[k] * bq[k] *
425 second_moment([&res, yk](
Cplx u) {
return res.lstCond(u, yk); },
426 yk / (1.0 - rho), res.meanUncond);
429 res.varUncond = m2u - res.meanUncond * res.meanUncond;
432 if (!res.t.empty() && !isPH)
433 throw InputError(
"qsys_mg1_ps: the sojourn time distribution needs phase-type service, "
434 "since inverting a numerically inverted transform is unstable in double "
435 "precision; with a transform handle only the LST and its moments are "
437 res.pdfCond = Matrix<double>(res.x.size(), res.t.size(), 0.0);
438 res.cdfCond = Matrix<double>(res.x.size(), res.t.size(), 0.0);
439 for (std::size_t i = 0; i < res.x.size(); ++i) {
440 const double atom = res.atomCond[i], xi = res.x[i];
443 auto gpdf = [&res, xi, atom](
Cplx u) {
return res.lstExcess(u, xi) -
Cplx(atom, 0.0); };
444 auto gcdf = [gpdf](
Cplx u) {
return gpdf(u) / u; };
445 for (std::size_t j = 0; j < res.t.size(); ++j) {
446 const double tj = res.t[j];
447 if (tj < xi)
continue;
449 res.cdfCond(i, j) = atom;
452 res.cdfCond(i, j) = ilt(gcdf, tj - xi, opt.nterms).real() + atom;
453 const double ratio = tj / xi;
454 if (std::fabs(ratio - std::floor(ratio + 0.5)) < 1e-9)
455 res.pdfCond(i, j) = std::numeric_limits<double>::quiet_NaN();
457 res.pdfCond(i, j) = ilt(gpdf, tj - xi, opt.nterms).real();
461 res.pdfUncond.assign(res.t.size(), nan);
462 res.cdfUncond.assign(res.t.size(), nan);
464 for (std::size_t j = 0; j < res.t.size(); ++j) {
465 if (res.t[j] <= 0.0) {
466 res.pdfUncond[j] = 0.0;
467 res.cdfUncond[j] = 0.0;
470 res.pdfUncond[j] = ilt(res.lstUncond, res.t[j], opt.nterms).real();
472 ilt([&res](
Cplx u) {
return res.lstUncond(u) / u; }, res.t[j], opt.nterms).real();
489 using namespace mg1psdetail;
490 if (!(lambda > 0.0) || !std::isfinite(lambda))
491 throw InputError(
"qsys_mg1_ps: lambda must be a finite positive scalar");
492 if (
opt.nterms % 2 == 0 ||
opt.nterms < 11)
493 throw InputError(
"qsys_mg1_ps: nterms must be an odd integer of at least 11");
495 const std::size_t n = alpha.size();
496 if (n == 0 || Tmat.
rows() != n || Tmat.
cols() != n)
497 throw InputError(
"qsys_mg1_ps: T must be n x n to match alpha");
499 for (std::size_t i = 0; i < n; ++i) {
500 if (alpha[i] < -1e-12)
throw InputError(
"qsys_mg1_ps: alpha must be a probability vector");
503 if (std::fabs(asum - 1.0) > 1e-8)
504 throw InputError(
"qsys_mg1_ps: alpha must be a probability vector");
505 std::vector<double> exitrate(n, 0.0);
506 for (std::size_t i = 0; i < n; ++i) {
508 for (std::size_t j = 0; j < n; ++j) row += Tmat(i, j);
510 if (exitrate[i] < -1e-10 || Tmat(i, i) >= 0.0)
511 throw InputError(
"qsys_mg1_ps: T must be a proper phase-type subgenerator");
516 for (std::size_t i = 0; i < n; ++i)
517 for (std::size_t j = 0; j < n; ++j) negT(i, j) = -Tmat(i, j);
518 const std::vector<double>
ones(n, 1.0);
519 const std::vector<double> u1 =
solve(negT,
ones);
520 const std::vector<double> u2 =
solve(negT, u1);
521 double m1 = 0.0, m2 = 0.0;
522 for (std::size_t i = 0; i < n; ++i) {
523 m1 += alpha[i] * u1[i];
524 m2 += 2.0 * alpha[i] * u2[i];
526 const double rho = lambda * m1;
528 throw InputError(
"qsys_mg1_ps: the system is unstable, the utilization is at least one");
532 const double m1mix = m1;
533 const bool tagged = !
opt.tagAlpha.empty();
534 const std::vector<double>& talpha = tagged ?
opt.tagAlpha : alpha;
536 if (talpha.size() != n)
537 throw InputError(
"qsys_mg1_ps: tagAlpha must have n entries to match T");
539 for (std::size_t i = 0; i < n; ++i) {
540 if (talpha[i] < -1e-12)
541 throw InputError(
"qsys_mg1_ps: tagAlpha must be a probability vector");
544 if (std::fabs(tsum - 1.0) > 1e-8)
545 throw InputError(
"qsys_mg1_ps: tagAlpha must be a probability vector");
547 throw InputError(
"qsys_mg1_ps: pass either tagAlpha or pdf, not both");
550 for (std::size_t i = 0; i < n; ++i) {
551 m1 += talpha[i] * u1[i];
552 m2 += 2.0 * talpha[i] * u2[i];
558 std::vector<double> db(n + 1, 0.0), nb(n, 0.0), nbt(n, 0.0);
561 for (std::size_t i = 0; i < n; ++i) Mk(i, i) = 1.0;
562 for (std::size_t k = 1; k <= n; ++k) {
563 double v = 0.0, vt = 0.0;
564 for (std::size_t i = 0; i < n; ++i) {
566 for (std::size_t j = 0; j < n; ++j) row += Mk(i, j) * exitrate[j];
568 vt += talpha[i] * row;
573 for (std::size_t i = 0; i < n; ++i)
574 for (std::size_t j = 0; j < n; ++j) {
576 for (std::size_t l = 0; l < n; ++l) acc += Tmat(i, l) * Mk(l, j);
580 for (std::size_t i = 0; i < n; ++i)
tr += TM(i, i);
581 db[k] = -
tr /
static_cast<double>(k);
583 for (std::size_t i = 0; i < n; ++i) Mk(i, i) += db[k];
586 auto bhat = [db, nb](
const Cplx& tau) {
return polyval(nb, tau) / polyval(db, tau); };
588 auto bhat_tag = [db, nbt](
const Cplx& tau) {
return polyval(nbt, tau) / polyval(db, tau); };
589 auto bpdf = [&Tmat, &talpha, &exitrate, n](
double y) {
591 for (std::size_t i = 0; i < n; ++i)
592 for (std::size_t j = 0; j < n; ++j) E(i, j) = Tmat(i, j) * y;
595 for (std::size_t i = 0; i < n; ++i) {
597 for (std::size_t j = 0; j < n; ++j) row += Ey(i, j) * exitrate[j];
598 v += talpha[i] * row;
613 std::vector<Cplx> Pc, Ahat, r, coef;
617 auto prep = std::make_shared<DenomPrep>();
618 auto denom = [db, nb, n, lambda, rho, prep](
const Cplx& s,
double x,
double* scale_out) {
619 if (!prep->valid || prep->s != s) {
620 std::vector<double> dm(db.size(), 0.0);
622 for (std::size_t i = 1; i < db.size(); ++i) dm[i] = db[i] - nb[i - 1];
626 std::vector<Cplx> Pc, Ac;
628 std::vector<Cplx> lin;
629 lin.push_back(
Cplx(1.0, 0.0));
630 lin.push_back(-(s +
Cplx(lambda, 0.0)));
631 std::vector<Cplx> dbz(db.size());
632 for (std::size_t i = 0; i < db.size(); ++i) dbz[i] =
Cplx(db[i], 0.0);
633 Pc.assign(lin.size() + dbz.size() - 1,
Cplx(0.0, 0.0));
634 for (std::size_t i = 0; i < lin.size(); ++i)
635 for (std::size_t j = 0; j < dbz.size(); ++j) Pc[i + j] += lin[i] * dbz[j];
637 for (std::size_t i = 0; i < nb.size(); ++i)
638 Pc[Pc.size() - nb.size() + i] +=
Cplx(lambda * nb[i], 0.0);
640 std::vector<Cplx> q1;
641 q1.push_back(
Cplx(1.0 - rho, 0.0));
642 q1.push_back(s *
Cplx(rho, 0.0));
643 q1.push_back(
Cplx(0.0, 0.0));
644 std::vector<Cplx> t1(q1.size() + dbz.size() - 1,
Cplx(0.0, 0.0));
645 for (std::size_t i = 0; i < q1.size(); ++i)
646 for (std::size_t j = 0; j < dbz.size(); ++j) t1[i + j] += q1[i] * dbz[j];
648 std::vector<Cplx> q2;
649 q2.push_back(
Cplx(1.0 - rho, 0.0));
651 std::vector<Cplx> ldm(dm.size());
652 for (std::size_t i = 0; i < dm.size(); ++i) ldm[i] =
Cplx(lambda * dm[i], 0.0);
653 std::vector<Cplx> t2(q2.size() + ldm.size() - 1,
Cplx(0.0, 0.0));
654 for (std::size_t i = 0; i < q2.size(); ++i)
655 for (std::size_t j = 0; j < ldm.size(); ++j) t2[i + j] += q2[i] * ldm[j];
657 Ac.assign(t1.size(),
Cplx(0.0, 0.0));
658 for (std::size_t i = 0; i < t1.size(); ++i) Ac[i] = t1[i];
659 for (std::size_t i = 0; i < t2.size(); ++i) Ac[Ac.size() - t2.size() + i] -= t2[i];
664 for (std::size_t i = 0; i < Ac.size(); ++i) nrmA = std::max(nrmA, std::abs(Ac[i]));
665 const double tailA = std::abs(Ac[Ac.size() - 2]) + std::abs(Ac[Ac.size() - 1]);
666 if (tailA > 1e-6 * std::max(1.0, nrmA))
667 throw InputError(
"qsys_mg1_ps: the double pole at the origin did not cancel");
672 std::vector<Cplx> Ahat(Ac.begin(), Ac.begin() +
static_cast<long>(n + 1));
677 std::vector<Cplx> mon(Pc.size());
678 for (std::size_t i = 0; i < Pc.size(); ++i) mon[i] = Pc[i] / Pc[0];
679 const std::size_t deg = mon.size() - 1;
680 r.assign(deg,
Cplx(0.0, 0.0));
681 const Cplx seed(0.4, 0.9);
683 for (std::size_t i = 0; i < deg; ++i) {
687 for (
int it = 0; it < 2000; ++it) {
689 for (std::size_t i = 0; i < deg; ++i) {
691 for (std::size_t j = 0; j < deg; ++j)
692 if (j != i) den *= (r[i] - r[j]);
693 if (std::abs(den) < 1e-300)
continue;
694 const Cplx step = polyval(mon, r[i]) / den;
696 move = std::max(move, std::abs(step));
698 if (move < 1e-14)
break;
701 double scale = -std::numeric_limits<double>::infinity();
702 for (std::size_t i = 0; i < r.size(); ++i) scale = std::max(scale, r[i].real());
704 double maxr = 0.0, minsep = std::numeric_limits<double>::infinity();
705 for (std::size_t i = 0; i < r.size(); ++i) maxr = std::max(maxr, std::abs(r[i]));
706 for (std::size_t i = 0; i < r.size(); ++i)
707 for (std::size_t j = 0; j < r.size(); ++j)
708 if (i != j) minsep = std::min(minsep, std::abs(r[i] - r[j]));
709 prep->simple = r.size() >= 2 && minsep > 1e-7 * std::max(1.0, maxr);
712 std::vector<Cplx> dP(Pc.size() - 1,
Cplx(0.0, 0.0));
713 for (std::size_t i = 0; i + 1 < Pc.size(); ++i)
714 dP[i] = Pc[i] *
static_cast<double>(Pc.size() - 1 - i);
715 for (std::size_t i = 0; i < r.size(); ++i) prep->coef.push_back(polyval(Ahat, r[i]) / polyval(dP, r[i]));
724 const double scale = prep->scale;
725 const std::vector<Cplx>& Pc = prep->Pc;
726 const std::vector<Cplx>& Ahat = prep->Ahat;
727 const std::vector<Cplx>& r = prep->r;
731 for (std::size_t i = 0; i < r.size(); ++i) val += std::exp((r[i] -
Cplx(scale, 0.0)) * x) * prep->coef[i];
737 const std::size_t d = Pc.size() - 1;
739 for (std::size_t j = 0; j < d; ++j) Acomp(0, j) = -Pc[j + 1] / Pc[0];
740 for (std::size_t i = 1; i < d; ++i) Acomp(i, i - 1) =
Cplx(1.0, 0.0);
741 for (std::size_t i = 0; i < d; ++i) Acomp(i, i) -=
Cplx(scale, 0.0);
743 for (std::size_t i = 0; i < d; ++i)
744 for (std::size_t j = 0; j < d; ++j) Ax(i, j) = Acomp(i, j) * x;
747 for (std::size_t j = 0; j < d && j < Ahat.size(); ++j) val += (Ahat[j] / Pc[0]) * E(j, 0);
755 double mineig = std::numeric_limits<double>::infinity();
757 const std::vector<Cplx> ev = poly_roots(db);
758 for (std::size_t i = 0; i < ev.size(); ++i) mineig = std::min(mineig, -ev[i].real());
759 if (ev.empty() || !std::isfinite(mineig))
760 for (std::size_t i = 0; i < n; ++i) mineig = std::min(mineig, -Tmat(i, i));
762 const double ymax = std::max(40.0 * std::max(m1, m1mix), 40.0 / std::max(mineig, 1e-12));
764 std::function<
Cplx(
const Cplx&,
double,
double*)> denom_fn = denom;
765 std::function<
Cplx(
const Cplx&)> bhat_fn = bhat;
766 std::function<
Cplx(
const Cplx&)> bhat_tag_fn = bhat_tag;
767 std::function<double(
double)> bpdf_fn =
opt.pdf ?
opt.pdf : std::function<double(double)>(bpdf);
768 return mg1psdetail::mg1ps_finish(lambda, rho, m1, m2, bhat_fn, bhat_tag_fn, denom_fn, bpdf_fn, ymax,
true,
791 using namespace mg1psdetail;
792 if (!(lambda > 0.0) || !std::isfinite(lambda))
793 throw InputError(
"qsys_mg1_ps: lambda must be a finite positive scalar");
794 if (
opt.nterms % 2 == 0 ||
opt.nterms < 11)
795 throw InputError(
"qsys_mg1_ps: nterms must be an odd integer of at least 11");
796 if (!bhat)
throw InputError(
"qsys_mg1_ps: the service transform handle is empty");
797 if (!std::isfinite(m1) || m1 <= 0.0)
798 throw InputError(
"qsys_mg1_ps: the mean service time must be a finite positive scalar");
799 const double rho = lambda * m1;
801 throw InputError(
"qsys_mg1_ps: the system is unstable, the utilization is at least one");
803 throw InputError(
"qsys_mg1_ps: the sojourn time distribution needs phase-type service, "
804 "since inverting a numerically inverted transform is unstable in double "
805 "precision; with a transform handle only the LST and its moments are "
809 auto rootfun = [lambda, bhat, rho](
const Cplx& s) {
811 const int maxit = std::max(200,
static_cast<int>(std::ceil(
812 3.0 * std::log(1e-15) / std::log(std::max(rho, 1e-3)))));
813 for (
int it = 0; it < maxit; ++it) {
814 const Cplx nx = s +
Cplx(lambda, 0.0) * (
Cplx(1.0, 0.0) - bhat(tau));
815 if (std::abs(nx - tau) <= 1e-14 * std::max(1.0, std::abs(nx)))
return nx;
818 throw InputError(
"qsys_mg1_ps: the dominant root iteration did not converge");
820 const double shift = 0.25 * (1.0 - rho) / m1;
821 const std::size_t nterms =
opt.nterms;
824 std::function<
Cplx(
const Cplx&,
double,
double*)> denom =
825 [lambda, rho, bhat, rootfun, shift, nterms](
const Cplx& s,
double x,
double* scale_out) {
826 const double scale = rootfun(s).real() + shift;
828 auto f = [&](
const Cplx& u) {
829 const Cplx tau = u +
Cplx(scale, 0.0);
830 const Cplx bh = bhat(tau);
831 const Cplx one_m_bh =
Cplx(1.0, 0.0) - bh;
832 const Cplx num =
Cplx(1.0 - rho, 0.0) * tau * tau -
833 Cplx((1.0 - rho) * lambda, 0.0) * one_m_bh * tau +
834 s *
Cplx(rho, 0.0) * tau - s *
Cplx(lambda, 0.0) * one_m_bh;
835 return num / (tau * tau * (tau - s -
Cplx(lambda, 0.0) * one_m_bh));
837 return ilt(f, x, nterms);
839 if (!
opt.tagAlpha.empty())
840 throw InputError(
"qsys_mg1_ps: tagAlpha needs phase-type service");
841 const double ymax = 60.0 * m1;
842 return mg1ps_finish(lambda, rho, m1, std::numeric_limits<double>::quiet_NaN(), bhat, bhat, denom,
843 opt.pdf, ymax,
false,
opt);
The exception types the port throws.
Matrix exponential by scaling and squaring with a diagonal Pade approximant.
Numerical inversion of a Laplace transform: Euler, Talbot, Gaver-Stehfest.
LU factorization with partial pivoting, templated on the number type.
Dense matrix and non-owning view.
std::vector< Cplx > euler_get_omega(std::size_t n)
Euler weights: eta, alternating in sign, scaled by 10^((n-1)/6).
std::vector< Cplx > euler_get_alpha(std::size_t n)
Euler nodes: a vertical line at Re = (n-1) log(10) / 6.
Mg1PsResult qsys_mg1_ps(double lambda, const std::vector< double > &alpha, const Matrix< double > &Tmat, const Mg1PsOptions &opt)
Sojourn-time distribution of the M/G/1 processor-sharing queue.
std::complex< double > Cplx
Conservation laws of a layered queueing network, enumerated from its structure.
std::vector< T > solve(const Matrix< T > &A, const std::vector< T > &b)
Convenience: solve Ax = b, leaving A and b untouched.
std::vector< T > ones(std::size_t n)
Column vector of ones, the ubiquitous e in MAP algebra.
Matrix< T > expm(const Matrix< T > &A)
Matrix exponential exp(A).
Number-type abstraction for the templated API port.
Shared scalar machinery for the integration / asymptotic members of the pfqn family (pfqn_le,...
Options of qsys_mg1_ps, mirroring the reference's name-value pairs.
std::function< double(double)> pdf
Service density, needed to remove the conditioning on a callback path.
std::vector< double > tagAlpha
Initial vector of the TAGGED job's requirement, PH(tagAlpha, T), on the phase-type path only (empty: ...
std::vector< double > s
transform arguments to tabulate the LST at
std::vector< double > t
times to evaluate the sojourn distribution at
std::size_t nterms
function evaluations per numerical inversion, ODD
std::vector< double > x
service requirements to condition on
Everything qsys_mg1_ps returns.
Matrix< double > lstCondVal
(|x| x |s|)
std::function< Cplx(Cplx)> lstUncond
s -> E[exp(-s V)]
std::vector< double > lstUncondVal
std::vector< double > meanCond
double atomUncond
(1-rho) bhat(lambda)
Matrix< double > cdfCond
(|x| x |t|)
std::function< Cplx(Cplx, double)> lstCond
(s,x) -> E[exp(-s V(x))]
std::function< Cplx(Cplx, double)> lstExcess
(s,x) -> E[exp(-s (V(x)-x))]
std::vector< double > cdfUncond
std::vector< double > m2Cond
std::vector< double > varCond
std::vector< double > pdfUncond
std::function< Cplx(Cplx)> dominantRoot
s -> tau*(s)
std::vector< double > atomCond
(1-rho) exp(-lambda x), at t = x