32 constexpr double eps = std::numeric_limits<double>::epsilon();
33 if (twist.rows() != d || twist.cols() != d || !twist.allFinite())
34 throw std::invalid_argument(
35 "Twist must be a finite matrix matching the segment dimension");
36 const Matrix gram = twist.transpose() * twist;
37 if (!gram.allFinite() || magnitude(gram - Matrix::Identity(d, d)) >
38 64 * eps * d * std::max(1.0, magnitude(gram)))
39 throw std::invalid_argument(
40 "Twist must be orthogonal in the mass-weighted metric");
42 static_cast<std::size_t
>(std::numeric_limits<Eigen::Index>::max() / d))
43 throw std::overflow_error(
44 "Twisted trace dimension exceeds index range");
48 twist.transpose() * segments.back().rightEndpoint() * twist;
50 if (segments.size() == 1) {
51 correction = segments.front().L * twist +
52 twist.transpose() * segments.front().L.transpose();
55 coupling.topRows(d) -= segments.front().L.transpose();
56 coupling.bottomRows(d) -= segments.back().L * twist;
57 Matrix response(coupling.rows(), d);
58 for (Eigen::Index col = 0; col < d; ++col)
59 response.col(col) = open.
solve(
Vector(coupling.col(col)));
60 correction = coupling.transpose() * response;
62 Matrix schur = endpoint - correction;
63 const double scale = magnitude(endpoint) + magnitude(correction);
64 const double tolerance = 64 * eps * d * scale;
65 if (!schur.allFinite() || !std::isfinite(tolerance) ||
66 magnitude(schur - schur.transpose()) > tolerance)
67 throw std::runtime_error(
68 "Twisted Schur matrix exceeds arithmetic roundoff");
69 schur = (.5 * schur + .5 * schur.transpose()).eval();
70 Eigen::SelfAdjointEigenSolver<Matrix> eig(schur);
71 if (eig.info() != Eigen::Success || !eig.eigenvalues().allFinite())
72 throw std::runtime_error(
"Twisted Schur eigendecomposition failed");
76 result.
dimension =
static_cast<Eigen::Index
>(segments.size()) * d;
79 for (
double value: eig.eigenvalues()) {
80 if (std::abs(value) <= tolerance) {
88 logDet += std::log(std::abs(value));
92 if (!std::isfinite(logDet))
93 throw std::overflow_error(
94 "Nonfinite normalized twisted determinant");