44{
46 const Eigen::Index d = open.blockDimension();
47 constexpr double eps = std::numeric_limits<double>::epsilon();
48 if (segments.size() >
49 static_cast<std::size_t>(std::numeric_limits<Eigen::Index>::max() / d))
50 throw std::overflow_error(
51 "Twisted restriction dimension exceeds index range");
52 const Eigen::Index total = static_cast<Eigen::Index>(segments.size()) * d;
53 const Eigen::Index k = orthonormalNormals.cols();
54 if (orthonormalNormals.rows() != total || k < 1 || k > total ||
55 !orthonormalNormals.allFinite() ||
56 (orthonormalNormals.transpose() * orthonormalNormals -
57 Matrix::Identity(k, k))
58 .norm() > 1e-10)
59 throw std::invalid_argument(
60 "Expected orthonormal constraints in the full cyclic nodal "
61 "measure");
62 if (twist.rows() != d || twist.cols() != d || !twist.allFinite())
63 throw std::invalid_argument(
64 "Twist must match the physical block dimension");
65 const Matrix gram = twist.transpose() * twist;
66 if (!gram.allFinite() || magnitude(gram - Matrix::Identity(d, d)) >
67 64 * eps * d * std::max(1.0, magnitude(gram)))
68 throw std::invalid_argument("Twist must be mass-metric orthogonal");
69 Matrix endpoint = Matrix::Zero(d + k, d + k);
70 endpoint.topLeftCorner(d, d) =
71 segments.front().A +
72 twist.transpose() * segments.back().rightEndpoint() * twist;
73 endpoint.topRightCorner(d, k) = orthonormalNormals.topRows(d);
74 endpoint.bottomLeftCorner(k, d) = orthonormalNormals.topRows(d).transpose();
75 Matrix correction = Matrix::Zero(d + k, d + k);
76 if (segments.size() == 1) {
77 correction.topLeftCorner(d, d) =
78 segments.front().L * twist +
79 twist.transpose() * segments.front().L.transpose();
80 } else {
81 Matrix coupling = Matrix::Zero(open.interiorDimension(), d + k);
82 coupling.topLeftCorner(d, d) -= segments.front().L.transpose();
83 coupling.bottomLeftCorner(d, d) -= segments.back().L * twist;
84 coupling.rightCols(k) =
85 orthonormalNormals.bottomRows(open.interiorDimension());
86 Matrix response(coupling.rows(), d + k);
87 for (Eigen::Index col = 0; col < d + k; ++col)
88 response.col(col) = open.solve(
Vector(coupling.col(col)));
89 correction = coupling.transpose() * response;
90 }
91 Matrix schur = endpoint - correction;
92 const double reductionDimension =
93 static_cast<double>(open.interiorDimension() + d + k);
94 const double tolerance = 64 * eps * reductionDimension *
95 (magnitude(endpoint) + magnitude(correction));
96 const double asymmetry = magnitude(schur - schur.transpose());
97 if (!schur.allFinite() || !std::isfinite(tolerance) ||
98 asymmetry > tolerance) {
99 std::ostringstream message;
100 message << "Restricted twisted border exceeds arithmetic roundoff "
101 "(asymmetry="
102 << std::scientific << std::setprecision(17) << asymmetry
103 << ", tolerance=" << tolerance
104 << ", reduction_dimension=" << reductionDimension << ')';
105 throw std::runtime_error(message.str());
106 }
107 schur = (.5 * schur + .5 * schur.transpose()).eval();
108 Eigen::SelfAdjointEigenSolver<Matrix> eig(schur);
109 if (eig.info() != Eigen::Success || !eig.eigenvalues().allFinite())
110 throw std::runtime_error(
111 "Restricted twisted border eigendecomposition failed");
112 TwistedRestrictedResult result;
113 result.duration = open.duration();
114 result.dimension = total - k;
115 result.codimension = k;
116 result.inertia = open.inertia();
117 double logDet = open.logAbsD();
118 for (double value: eig.eigenvalues()) {
119 if (std::abs(value) <= tolerance) {
120 ++result.inertia.zero;
121 ++result.unresolvedBorderEigenvalues;
122 } else {
123 if (value > 0)
124 ++result.inertia.positive;
125 else
126 ++result.inertia.negative;
127 logDet += std::log(std::abs(value));
128 }
129 }
130
131
132 if (result.inertia.positive < static_cast<std::size_t>(k) ||
133 result.inertia.negative < static_cast<std::size_t>(k))
134 throw std::runtime_error(
135 "Border inertia cannot resolve its constraint pair");
136 result.inertia.positive -= k;
137 result.inertia.negative -= k;
138 if (!result.unresolvedBorderEigenvalues) {
139 if (!std::isfinite(logDet))
140 throw std::overflow_error(
141 "Nonfinite restricted twisted determinant");
142 result.logAbsNormalizedDet = logDet;
143 }
144 return result;
145}