315 double tol = 1e-10) {
317 if (phases.empty())
throw InputError(
ErrorKind{
"Argument"},
"ldqbd_censor: phases must give one vector of phase counts per set.");
318 const std::size_t S = phases.size();
319 std::vector<std::size_t> nset(S, 0), setOff(S + 1, 0);
320 for (std::size_t s = 0; s < S; ++s) {
321 if (phases[s].empty())
323 "} must be a non-empty vector of positive integers.");
324 for (std::size_t p : phases[s]) {
327 "} must be a non-empty vector of positive integers.");
330 setOff[s + 1] = setOff[s] + nset[s];
332 const std::size_t n = setOff[S];
336 " but the phase counts add up to " + std::to_string(n) +
".");
339 std::vector<std::map<std::size_t, T>> rows(n);
341 for (std::size_t i = 0; i < n; ++i) {
343 if (d > scale) scale = d;
345 for (std::size_t i = 0; i < n; ++i) {
347 for (std::size_t j = 0; j < n; ++j) {
349 if (v == zero<T>())
continue;
350 if (i != j && v < zero<T>())
351 throw InputError(
ErrorKind{
"Argument"},
"ldqbd_censor: the off-diagonal entries of Q must be nonnegative.");
357 if (bad)
throw InputError(
ErrorKind{
"Argument"},
"ldqbd_censor: the rows of Q must sum to zero.");
361 std::vector<Factor<T>> fac;
362 std::vector<std::vector<std::size_t>> bRows(S);
363 std::vector<std::vector<std::vector<T>>> bVals(S);
364 for (std::size_t k = 0; k + 1 < S; ++k) {
365 const std::size_t o0 = setOff[k], o1 = setOff[k + 1], nk = nset[k];
367 std::map<std::size_t, std::size_t> colPos;
368 for (std::size_t i = 0; i < nk; ++i)
369 for (
const auto& e : rows[o0 + i]) {
370 if (e.first >= o0 && e.first < o1) M(i, e.first - o0) = -e.second;
371 else if (e.first >= o1) colPos[e.first] = 0;
373 check(M, phases[k], k);
374 fac.emplace_back(M, phases[k], k,
false);
375 std::vector<std::size_t> cols;
376 for (
auto& c : colPos) {
377 c.second = cols.size();
378 cols.push_back(c.first);
380 Matrix<T> QkB(nk, cols.size(), zero<T>());
381 for (std::size_t i = 0; i < nk; ++i)
382 for (
const auto& e : rows[o0 + i])
383 if (e.first >= o1) QkB(i, colPos[e.first]) = e.second;
384 for (std::size_t r = o1; r < n; ++r) {
386 for (
const auto& e : rows[r]) {
387 if (e.first >= o0 && e.first < o1 && !(e.second == zero<T>())) {
388 if (v.empty()) v.assign(nk, zero<T>());
389 v[e.first - o0] = e.second;
393 bRows[k].push_back(r);
394 bVals[k].push_back(v);
397 if (!cols.empty() && !bRows[k].empty()) {
398 const Matrix<T> X = fac.back().colsolve(QkB);
399 for (std::size_t i = 0; i < bRows[k].size(); ++i) {
400 const std::vector<T> u =
vecmul(bVals[k][i], X);
401 auto& row = rows[bRows[k][i]];
402 for (std::size_t j = 0; j < cols.size(); ++j) {
403 if (u[j] == zero<T>())
continue;
404 auto it = row.find(cols[j]);
405 if (it == row.end()) row[cols[j]] = u[j];
406 else it->second += u[j];
413 const std::size_t oS = setOff[S - 1], nS = nset[S - 1];
415 for (std::size_t i = 0; i < nS; ++i)
416 for (
const auto& e : rows[oS + i])
417 if (e.first >= oS) MS(i, e.first - oS) = -e.second;
418 check(MS, phases[S - 1], S - 1);
419 const Factor<T> f(MS, phases[S - 1], S - 1,
true);
420 const std::size_t L = f.levels();
425 for (std::size_t i = 0; i < A.
rows(); ++i)
426 for (std::size_t j = 0; j < A.
cols(); ++j) At(j, i) = A(i, j);
427 std::vector<T> e1(A.
rows(), zero<T>());
433 throw NumericError(
ErrorKind{
"Singular"},
"ldqbd_censor: the last level of set " + std::to_string(S) +
434 " has no unique stationary vector; the chain may be reducible.");
436 std::vector<T> pB(n, zero<T>());
437 for (std::size_t i = 0; i < xL.size(); ++i) pB[oS + f.off[L - 1] + i] = xL[i];
438 for (std::size_t l = L - 1; l-- > 0;) {
439 const std::vector<T> xn(pB.begin() + oS + f.off[l + 1], pB.begin() + oS + f.off[l + 2]);
440 const std::vector<T> xg =
vecmul(xn, f.G[l + 1]);
441 for (std::size_t i = 0; i < f.P[l]; ++i) pB[oS + f.off[l] + i] = -xg[i];
445 for (std::size_t k = S - 1; k-- > 0;) {
446 std::vector<T> y(nset[k], zero<T>());
447 for (std::size_t i = 0; i < bRows[k].size(); ++i) {
448 const T p = pB[bRows[k][i]];
449 if (p == zero<T>())
continue;
450 for (std::size_t t = 0; t < nset[k]; ++t) y[t] += p * bVals[k][i][t];
452 const std::vector<T> x = fac[k].rowsolve(y);
453 for (std::size_t t = 0; t < nset[k]; ++t) pB[setOff[k] + t] = x[t];
456 for (
const T& v : pB) tot += v;
459 for (std::size_t i = 0; i < n; ++i) res.
pi[i] = pB[i] / tot;
462 for (std::size_t s = 0; s < S; ++s)
463 for (std::size_t p : phases[s]) {
464 res.
pi_cells[s].emplace_back(res.
pi.begin() + pos, res.
pi.begin() + pos + p);