42 const std::vector<Segment> &segments,
const Matrix &twist,
43 const Matrix &orthonormalNormals)
47 constexpr double eps = std::numeric_limits<double>::epsilon();
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))
59 throw std::invalid_argument(
60 "Expected orthonormal constraints in the full cyclic nodal "
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) =
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();
82 coupling.topLeftCorner(d, d) -= segments.front().L.transpose();
83 coupling.bottomLeftCorner(d, d) -= segments.back().L * twist;
84 coupling.rightCols(k) =
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;
91 Matrix schur = endpoint - correction;
92 const double reductionDimension =
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 "
102 << std::scientific << std::setprecision(17) << asymmetry
103 <<
", tolerance=" << tolerance
104 <<
", reduction_dimension=" << reductionDimension <<
')';
105 throw std::runtime_error(message.str());
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");
117 double logDet = open.
logAbsD();
118 for (
double value: eig.eigenvalues()) {
119 if (std::abs(value) <= tolerance) {
127 logDet += std::log(std::abs(value));
134 throw std::runtime_error(
135 "Border inertia cannot resolve its constraint pair");
139 if (!std::isfinite(logDet))
140 throw std::overflow_error(
141 "Nonfinite restricted twisted determinant");
148 const std::vector<Segment> &segments,
const Matrix &twist,
149 const Matrix &orthonormalNormals,
const Matrix &features,
double hbar,
152 const auto qualification =
155 qualification.logPositiveDensity(hbar);
156 else if (!qualification.logAbsNormalizedDet || qualification.inertia.zero ||
157 qualification.unresolvedBorderEigenvalues ||
158 !std::isfinite(*qualification.logAbsNormalizedDet))
159 throw std::domain_error(
160 "Restricted inverse requires a resolved nonsingular kernel");
162 const Eigen::Index d = open.
blockDimension(), k = orthonormalNormals.cols();
163 const Eigen::Index total = qualification.dimension + k;
164 if (features.rows() != total || !features.cols() || !features.allFinite())
165 throw std::invalid_argument(
166 "Invalid linear observables for the restricted Gaussian");
167 Matrix endpoint = Matrix::Zero(d + k, d + k);
168 endpoint.topLeftCorner(d, d) =
170 twist.transpose() * segments.back().rightEndpoint() * twist;
171 endpoint.topRightCorner(d, k) = orthonormalNormals.topRows(d);
172 endpoint.bottomLeftCorner(k, d) = orthonormalNormals.topRows(d).transpose();
177 if (segments.size() == 1) {
178 endpoint.topLeftCorner(d, d) -=
179 segments.front().L * twist +
180 twist.transpose() * segments.front().L.transpose();
182 coupling.topLeftCorner(d, d) -= segments.front().L.transpose();
183 coupling.bottomLeftCorner(d, d) -= segments.back().L * twist;
184 coupling.rightCols(k) =
186 for (Eigen::Index col = 0; col < d + k; ++col)
187 response.col(col) = open.
solve(
Vector(coupling.col(col)));
188 for (Eigen::Index col = 0; col < features.cols(); ++col)
189 interiorFeatures.col(col) = open.
solve(
191 endpoint -= coupling.transpose() * response;
193 Matrix right = Matrix::Zero(d + k, features.cols());
194 right.topRows(d) = features.topRows(d);
195 right -= coupling.transpose() * interiorFeatures;
196 Eigen::FullPivLU<Matrix> border(endpoint);
197 if (!border.isInvertible())
198 throw std::runtime_error(
"Restricted covariance border is unresolved");
199 const Matrix solved = border.solve(right);
200 Matrix physical(total, features.cols());
201 physical.topRows(d) = solved.topRows(d);
204 interiorFeatures - response * solved;
205 if (!physical.allFinite())
206 throw std::runtime_error(
"Nonfinite restricted covariance response");
207 Matrix equation = orthonormalNormals * solved.bottomRows(k) - features;
208 for (std::size_t j = 0; j < segments.size(); ++j) {
209 const Matrix left = physical.middleRows(j * d, d);
211 j + 1 < segments.size()
212 ?
Matrix(physical.middleRows((j + 1) * d, d))
213 :
Matrix(twist * physical.topRows(d));
214 equation.middleRows(j * d, d) +=
215 segments[j].A * left - segments[j].L * rightNode;
216 const Matrix endContribution = segments[j].rightEndpoint() * rightNode -
217 segments[j].L.transpose() * left;
218 if (j + 1 < segments.size())
219 equation.middleRows((j + 1) * d, d) += endContribution;
221 equation.topRows(d) += twist.transpose() * endContribution;
225 equation.norm() / std::max(1.0, features.norm());
227 (orthonormalNormals.transpose() * physical).norm() /
228 std::max(1.0, physical.norm());
233 throw std::runtime_error(
234 "Restricted covariance fails its original equation residual "
237 retained->values = physical;
238 retained->multipliers = solved.bottomRows(k);
240 retained->relativeConstraintResidual =
243 const Matrix raw = hbar * features.transpose() * physical;
245 64 * std::numeric_limits<double>::epsilon() * total * hbar *
247 (physical.norm() + solved.norm() + interiorFeatures.norm() +
248 response.norm() * solved.norm());
251 throw std::runtime_error(
252 "Restricted covariance symmetry exceeds arithmetic roundoff");
253 result.
covariance = (.5 * raw + .5 * raw.transpose()).eval();
267 const std::vector<Segment> &segments,
const Matrix &orthonormalNormals,
272 const Eigen::Index k = orthonormalNormals.cols();
273 const Eigen::Index total = d *
static_cast<Eigen::Index
>(segments.size());
274 if (segments.size() < 2 || orthonormalNormals.rows() != total || k < 1 ||
275 k > total || !orthonormalNormals.allFinite() ||
276 (orthonormalNormals.transpose() * orthonormalNormals -
277 Matrix::Identity(k, k))
279 twist.rows() != d || twist.cols() != d || !twist.allFinite())
280 throw std::invalid_argument(
281 "Invalid cyclic inverse diagonal-block inputs");
282 const Matrix gram = twist.transpose() * twist;
283 if ((gram - Matrix::Identity(d, d)).norm() >
284 64 * std::numeric_limits<double>::epsilon() * d *
285 std::max(1.0, gram.norm()))
286 throw std::invalid_argument(
"Twist must be mass-metric orthogonal");
288 Matrix border = Matrix::Zero(d + k, d + k);
289 border.topLeftCorner(d, d) =
291 twist.transpose() * segments.back().rightEndpoint() * twist;
292 border.topRightCorner(d, k) = orthonormalNormals.topRows(d);
293 border.bottomLeftCorner(k, d) = orthonormalNormals.topRows(d).transpose();
295 coupling.topLeftCorner(d, d) = -segments.front().L.transpose();
296 coupling.bottomLeftCorner(d, d) = -segments.back().L * twist;
297 coupling.rightCols(k) =
300 border.noalias() -= coupling.transpose() * response;
301 border = (.5 * border + .5 * border.transpose()).eval();
302 Eigen::FullPivLU<Matrix> factor(border);
303 if (!factor.isInvertible())
304 throw std::runtime_error(
305 "Restricted inverse diagonal border is unresolved");
306 const Matrix borderInverse = factor.solve(Matrix::Identity(d + k, d + k));
310 result.
blocks.reserve(segments.size());
311 result.
blocks.push_back((.5 * borderInverse.topLeftCorner(d, d) +
312 .5 * borderInverse.topLeftCorner(d, d).transpose())
314 for (std::size_t j = 0; j < openDiagonal.size(); ++j) {
315 const Matrix localResponse = response.middleRows(j * d, d);
316 Matrix block = openDiagonal[j] + localResponse * borderInverse *
317 localResponse.transpose();
318 result.
blocks.push_back((.5 * block + .5 * block.transpose()).eval());
321 for (
const auto &block: result.
blocks)
322 if (!block.allFinite())
323 throw std::runtime_error(
324 "Restricted inverse diagonal blocks are nonfinite");
350 const auto d = green.rows(), r = measured.cols();
351 if (d < 1 || green.cols() != d || measured.rows() != d || r > d ||
352 !green.allFinite() || !measured.allFinite() || !(duration > 0) ||
353 !std::isfinite(duration) ||
354 (green - green.transpose()).norm() >
355 1e-10 * std::max(1., green.norm()) ||
356 (measured.transpose() * measured - Matrix::Identity(r, r)).norm() >
358 throw std::invalid_argument(
359 "Symmetric finite inverse and orthonormal measured basis required");
362 -duration * (green + green.transpose()) / 4;
366 if (r == d)
return result;
367 const Matrix p = Matrix::Identity(d, d) - measured * measured.transpose();
372 const Eigen::SelfAdjointEigenSolver<Matrix> spectrum(
374 if (spectrum.info() != Eigen::Success)
375 throw std::runtime_error(
376 "Unmeasured determinant response eigensystem failed");
377 Eigen::Index selected;
379 spectrum.eigenvalues().cwiseAbs().maxCoeff(&selected);
382 Vector direction = p * spectrum.eigenvectors().col(selected);
383 if (!(direction.norm() > 0))
384 throw std::runtime_error(
385 "Unmeasured influence direction is unresolved");