Loading...
Searching...
No Matches
gpr::quantum::gaussian Namespace Reference

Classes

struct  CurvatureRateInfluence
class  Factorization
struct  Inertia
struct  RestrictedCovarianceProjection
struct  RestrictedDeterminant
struct  RestrictedInverseDiagonalBlocks
struct  RestrictedInverseProjection
struct  RestrictedKernelSolution
struct  Segment
class  SingularBlockPivot
struct  TwistedRestrictedResult
struct  TwistedTraceResult

Typedefs

using Matrix = Eigen::MatrixXd
using Vector = Eigen::VectorXd

Functions

Segment exactSegment (const Matrix &hessian, double duration)
double logRestrictedDensityRatio (const Factorization &reference, const RestrictedDeterminant &restricted, double action, double hbar)
TwistedRestrictedResult restrictedTraceDeterminant (const std::vector< Segment > &segments, const Matrix &twist, const Vector &unitNormal)
TwistedRestrictedResult restrictedTraceDeterminant (const std::vector< Segment > &segments, const Matrix &twist, const Matrix &orthonormalNormals)
static RestrictedCovarianceProjection restrictedProjection (const std::vector< Segment > &segments, const Matrix &twist, const Matrix &orthonormalNormals, const Matrix &features, double hbar, bool positive, RestrictedKernelSolution *retained=nullptr)
RestrictedKernelSolution solveRestrictedTraceKernel (const std::vector< Segment > &segments, const Matrix &twist, const Matrix &orthonormalNormals, const Matrix &sources)
RestrictedInverseDiagonalBlocks restrictedTraceInverseDiagonalBlocks (const std::vector< Segment > &segments, const Matrix &orthonormalNormals, const Matrix &twist)
RestrictedInverseProjection restrictedTraceInverseProjection (const std::vector< Segment > &segments, const Matrix &twist, const Matrix &orthonormalNormals, const Matrix &features)
RestrictedCovarianceProjection restrictedTraceCovarianceProjection (const std::vector< Segment > &segments, const Matrix &twist, const Matrix &orthonormalNormals, const Matrix &features, double hbar)
CurvatureRateInfluence nodalCurvatureRateInfluence (const Matrix &green, double duration, const Matrix &measured)
TwistedTraceResult traceDeterminant (const std::vector< Segment > &segments, const Matrix &twist)
double midpointTaylorAction (const Matrix &hessian, const Vector &gradient, const Vector &displacement, double potential, double duration)

Typedef Documentation

◆ Matrix

using gpr::quantum::gaussian::Matrix = Eigen::MatrixXd

Definition at line 14 of file GaussianSegments.h.

◆ Vector

using gpr::quantum::gaussian::Vector = Eigen::VectorXd

Definition at line 15 of file GaussianSegments.h.

Function Documentation

◆ exactSegment()

Segment gpr::quantum::gaussian::exactSegment ( const Matrix & hessian,
double duration )

Definition at line 34 of file GaussianSegments.cpp.

35{
36 requireSymmetric(hessian);
37 if (!(duration > 0) || !std::isfinite(duration))
38 throw std::invalid_argument(
39 "Expected finite positive segment duration");
40 Eigen::SelfAdjointEigenSolver<Matrix> eig(.5 * hessian +
41 .5 * hessian.transpose());
42 if (eig.info() != Eigen::Success || !eig.eigenvalues().allFinite())
43 throw std::runtime_error("Segment Hessian eigendecomposition failed");
44 Vector a(hessian.rows()), l(hessian.rows());
46 out.duration = duration;
47 out.minimumCausticDistance = std::numeric_limits<double>::infinity();
48 for (Eigen::Index i = 0; i < hessian.rows(); ++i) {
49 const double lambda = eig.eigenvalues()[i];
50 const double frequency = std::sqrt(std::abs(lambda));
51 const double x = duration * frequency;
52 if (!std::isfinite(x))
53 throw std::overflow_error(
54 "Unrepresentable segment frequency-duration product");
55 double logL;
56 if (lambda < 0) {
57 const double distance = std::numbers::pi - x;
58 out.minimumCausticDistance =
59 std::min(out.minimumCausticDistance, distance);
60 if (!(distance > 32 * eps * std::numbers::pi))
61 throw std::domain_error(
62 "Segment reaches or cannot resolve its first Dirichlet "
63 "caustic");
64 }
65 if (x < 1e-3) {
66 const double z = std::copysign(x * x, lambda);
67 a[i] = (1 + z / 3 - z * z / 45 + 2 * z * z * z / 945) / duration;
68 logL = -std::log(duration) - z / 6 + z * z / 180 - z * z * z / 2835;
69 l[i] = std::exp(logL);
70 } else if (lambda > 0) {
71 const double denominator = -std::expm1(-2 * x);
72 a[i] = frequency * (1 + std::exp(-2 * x)) / denominator;
73 logL = std::log(2 * frequency) - x - std::log(denominator);
74 l[i] = std::exp(logL);
75 } else {
76 a[i] = frequency * std::cos(x) / std::sin(x);
77 logL = std::log(frequency) - std::log(std::sin(x));
78 l[i] = std::exp(logL);
79 }
80 if (!std::isfinite(a[i]) || !std::isfinite(logL) ||
81 !std::isfinite(l[i]))
82 throw std::overflow_error("Nonfinite exact segment coefficient");
83 out.logDetL += logL;
84 }
85 out.A =
86 eig.eigenvectors() * a.asDiagonal() * eig.eigenvectors().transpose();
87 out.L =
88 eig.eigenvectors() * l.asDiagonal() * eig.eigenvectors().transpose();
89 if (!out.A.allFinite() || !out.L.allFinite() || !std::isfinite(out.logDetL))
90 throw std::overflow_error("Nonfinite exact segment matrix");
91 return out;
92}
@ z
Definition Enums.h:28
@ x
Definition Enums.h:28
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out

◆ logRestrictedDensityRatio()

double gpr::quantum::gaussian::logRestrictedDensityRatio ( const Factorization & reference,
const RestrictedDeterminant & restricted,
double action,
double hbar )

Definition at line 287 of file GaussianSegments.cpp.

290{
291 requireHbar(hbar);
292 if (!std::isfinite(action) || !std::isfinite(restricted.logAbsD))
293 throw std::invalid_argument(
294 "Nonfinite action or restricted determinant");
295 if (restricted.physicalDimension != reference.blockDimension() ||
296 !(restricted.duration > 0) || !std::isfinite(restricted.duration) ||
297 std::abs(restricted.duration - reference.duration()) >
298 128 * eps * (restricted.segments + reference.segmentCount()) *
299 std::max(restricted.duration, reference.duration()))
300 throw std::invalid_argument(
301 "Reference and restricted operators require identical physical "
302 "dimension and duration");
303 if (reference.inertia().negative || reference.inertia().zero ||
304 restricted.inertia.negative || restricted.inertia.zero ||
305 restricted.sign != 1)
306 throw std::domain_error(
307 "A real Gaussian density requires positive restricted/reference "
308 "operators");
309 const double result =
310 .5 * (reference.logAbsD() - restricted.logAbsD) -
311 .5 * (std::log(2 * std::numbers::pi) + std::log(hbar)) - action / hbar;
312 if (!std::isfinite(result))
313 throw std::overflow_error("Nonfinite restricted log density");
314 return result;
315}

◆ midpointTaylorAction()

double gpr::quantum::gaussian::midpointTaylorAction ( const Matrix & hessian,
const Vector & gradient,
const Vector & displacement,
double potential,
double duration )

Definition at line 9 of file MidpointTaylorAction.cpp.

12{
13 // The segment validation includes symmetry, finite inputs and caustics.
14 exactSegment(hessian, duration);
15 if (gradient.size() != hessian.rows() ||
16 displacement.size() != hessian.rows() || !gradient.allFinite() ||
17 !displacement.allFinite() || !std::isfinite(potential))
18 throw std::invalid_argument("Invalid midpoint Taylor action data");
19 Eigen::SelfAdjointEigenSolver<Matrix> eig(.5 * hessian +
20 .5 * hessian.transpose());
21 if (eig.info() != Eigen::Success)
22 throw std::runtime_error("Midpoint action eigendecomposition failed");
23 const Vector g = eig.eigenvectors().transpose() * gradient;
24 const Vector d = eig.eigenvectors().transpose() * displacement;
25 long double action = duration * potential;
26 for (Eigen::Index i = 0; i < g.size(); ++i) {
27 const double lambda = eig.eigenvalues()[i];
28 const double x = duration * std::sqrt(std::abs(lambda));
29 double kinetic, affine;
30 if (x < 1e-2) {
31 const double z = std::copysign(x * x, lambda);
32 kinetic =
33 (1 + z / 12 - z * z / 720 + z * z * z / 30240) / (2 * duration);
34 affine = duration * duration * duration *
35 (-1. / 24 + z / 240 - 17 * z * z / 40320 +
36 31 * z * z * z / 725760);
37 } else {
38 const double t = lambda > 0 ? std::tanh(x / 2) : std::tan(x / 2);
39 kinetic = std::sqrt(std::abs(lambda)) / (4 * t);
40 affine = duration * (2 * t / x - 1) / (2 * lambda);
41 }
42 action += static_cast<long double>(kinetic) * d[i] * d[i] +
43 static_cast<long double>(affine) * g[i] * g[i];
44 }
45 const double result = static_cast<double>(action);
46 if (!std::isfinite(result))
47 throw std::overflow_error("Nonfinite midpoint Taylor action");
48 return result;
49}
Segment exactSegment(const Matrix &hessian, double duration)
STL namespace.

◆ nodalCurvatureRateInfluence()

CurvatureRateInfluence gpr::quantum::gaussian::nodalCurvatureRateInfluence ( const Matrix & green,
double duration,
const Matrix & measured )

Definition at line 346 of file GaussianTwistedRestriction.cpp.

349{
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() >
357 1e-10)
358 throw std::invalid_argument(
359 "Symmetric finite inverse and orthonormal measured basis required");
362 -duration * (green + green.transpose()) / 4;
363 result.unmeasuredDimension = d - r;
364 result.unmeasuredDirection = Vector::Zero(d);
365 result.unmeasuredGradient = Matrix::Zero(d, d);
366 if (r == d) return result;
367 const Matrix p = Matrix::Identity(d, d) - measured * measured.transpose();
368 result.unmeasuredGradient = p * result.logGaussianRateGradient * p;
369 result.unmeasuredGradient = (.5 * result.unmeasuredGradient +
370 .5 * result.unmeasuredGradient.transpose())
371 .eval();
372 const Eigen::SelfAdjointEigenSolver<Matrix> spectrum(
373 result.unmeasuredGradient);
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);
380 result.frobeniusInfluence = result.unmeasuredGradient.norm();
381 if (result.largestAbsoluteInfluence > 0) {
382 Vector direction = p * spectrum.eigenvectors().col(selected);
383 if (!(direction.norm() > 0))
384 throw std::runtime_error(
385 "Unmeasured influence direction is unresolved");
386 result.unmeasuredDirection = direction.normalized();
387 }
388 return result;
389}

◆ restrictedProjection()

RestrictedCovarianceProjection gpr::quantum::gaussian::restrictedProjection ( const std::vector< Segment > & segments,
const Matrix & twist,
const Matrix & orthonormalNormals,
const Matrix & features,
double hbar,
bool positive,
RestrictedKernelSolution * retained = nullptr )
static

Definition at line 147 of file GaussianTwistedRestriction.cpp.

151{
152 const auto qualification =
153 restrictedTraceDeterminant(segments, twist, orthonormalNormals);
154 if (positive)
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");
161 const Factorization open(segments);
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) =
169 segments.front().A +
170 twist.transpose() * segments.back().rightEndpoint() * twist;
171 endpoint.topRightCorner(d, k) = orthonormalNormals.topRows(d);
172 endpoint.bottomLeftCorner(k, d) = orthonormalNormals.topRows(d).transpose();
173 Matrix coupling = Matrix::Zero(open.interiorDimension(), d + k);
174 Matrix response = Matrix::Zero(open.interiorDimension(), d + k);
175 Matrix interiorFeatures =
176 Matrix::Zero(open.interiorDimension(), features.cols());
177 if (segments.size() == 1) {
178 endpoint.topLeftCorner(d, d) -=
179 segments.front().L * twist +
180 twist.transpose() * segments.front().L.transpose();
181 } else {
182 coupling.topLeftCorner(d, d) -= segments.front().L.transpose();
183 coupling.bottomLeftCorner(d, d) -= segments.back().L * twist;
184 coupling.rightCols(k) =
185 orthonormalNormals.bottomRows(open.interiorDimension());
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(
190 Vector(features.col(col).tail(open.interiorDimension())));
191 endpoint -= coupling.transpose() * response;
192 }
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);
202 if (open.interiorDimension())
203 physical.bottomRows(open.interiorDimension()) =
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);
210 const Matrix rightNode =
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;
220 else
221 equation.topRows(d) += twist.transpose() * endContribution;
222 }
223 RestrictedCovarianceProjection result;
224 result.relativeEquationResidual =
225 equation.norm() / std::max(1.0, features.norm());
226 result.relativeConstraintResidual =
227 (orthonormalNormals.transpose() * physical).norm() /
228 std::max(1.0, physical.norm());
229 if (!std::isfinite(result.relativeEquationResidual) ||
230 !std::isfinite(result.relativeConstraintResidual) ||
231 result.relativeEquationResidual > 1e-8 ||
232 result.relativeConstraintResidual > 1e-10)
233 throw std::runtime_error(
234 "Restricted covariance fails its original equation residual "
235 "checks");
236 if (retained) {
237 retained->values = physical;
238 retained->multipliers = solved.bottomRows(k);
239 retained->relativeEquationResidual = result.relativeEquationResidual;
241 result.relativeConstraintResidual;
242 }
243 const Matrix raw = hbar * features.transpose() * physical;
244 result.symmetryRoundoffTolerance =
245 64 * std::numeric_limits<double>::epsilon() * total * hbar *
246 features.norm() *
247 (physical.norm() + solved.norm() + interiorFeatures.norm() +
248 response.norm() * solved.norm());
249 if (!raw.allFinite() || !std::isfinite(result.symmetryRoundoffTolerance) ||
250 (raw - raw.transpose()).norm() > result.symmetryRoundoffTolerance)
251 throw std::runtime_error(
252 "Restricted covariance symmetry exceeds arithmetic roundoff");
253 result.covariance = (.5 * raw + .5 * raw.transpose()).eval();
254 return result;
255}
TwistedRestrictedResult restrictedTraceDeterminant(const std::vector< Segment > &segments, const Matrix &twist, const Vector &unitNormal)

◆ restrictedTraceCovarianceProjection()

RestrictedCovarianceProjection gpr::quantum::gaussian::restrictedTraceCovarianceProjection ( const std::vector< Segment > & segments,
const Matrix & twist,
const Matrix & orthonormalNormals,
const Matrix & features,
double hbar )

Definition at line 338 of file GaussianTwistedRestriction.cpp.

341{
342 return restrictedProjection(segments, twist, orthonormalNormals, features,
343 hbar, true);
344}
static RestrictedCovarianceProjection restrictedProjection(const std::vector< Segment > &segments, const Matrix &twist, const Matrix &orthonormalNormals, const Matrix &features, double hbar, bool positive, RestrictedKernelSolution *retained=nullptr)

◆ restrictedTraceDeterminant() [1/2]

TwistedRestrictedResult gpr::quantum::gaussian::restrictedTraceDeterminant ( const std::vector< Segment > & segments,
const Matrix & twist,
const Matrix & orthonormalNormals )

Definition at line 41 of file GaussianTwistedRestriction.cpp.

44{
45 const Factorization open(segments);
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 // Each orthonormal constraint contributes one positive and one negative
131 // direction to the border, independently of the restricted signed inertia.
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}
Eigen::MatrixXd Matrix

◆ restrictedTraceDeterminant() [2/2]

TwistedRestrictedResult gpr::quantum::gaussian::restrictedTraceDeterminant ( const std::vector< Segment > & segments,
const Matrix & twist,
const Vector & unitNormal )

Definition at line 34 of file GaussianTwistedRestriction.cpp.

37{
38 return restrictedTraceDeterminant(segments, twist, Matrix(unitNormal));
39}

◆ restrictedTraceInverseDiagonalBlocks()

RestrictedInverseDiagonalBlocks gpr::quantum::gaussian::restrictedTraceInverseDiagonalBlocks ( const std::vector< Segment > & segments,
const Matrix & orthonormalNormals,
const Matrix & twist )

Definition at line 266 of file GaussianTwistedRestriction.cpp.

269{
270 const Factorization open(segments);
271 const Eigen::Index d = open.blockDimension();
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))
278 .norm() > 1e-10 ||
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");
287
288 Matrix border = Matrix::Zero(d + k, d + k);
289 border.topLeftCorner(d, d) =
290 segments.front().A +
291 twist.transpose() * segments.back().rightEndpoint() * twist;
292 border.topRightCorner(d, k) = orthonormalNormals.topRows(d);
293 border.bottomLeftCorner(k, d) = orthonormalNormals.topRows(d).transpose();
294 Matrix coupling = Matrix::Zero(open.interiorDimension(), d + k);
295 coupling.topLeftCorner(d, d) = -segments.front().L.transpose();
296 coupling.bottomLeftCorner(d, d) = -segments.back().L * twist;
297 coupling.rightCols(k) =
298 orthonormalNormals.bottomRows(open.interiorDimension());
299 const Matrix response = open.solve(coupling);
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));
307 const auto openDiagonal = open.inverseDiagonalBlocks();
308
310 result.blocks.reserve(segments.size());
311 result.blocks.push_back((.5 * borderInverse.topLeftCorner(d, d) +
312 .5 * borderInverse.topLeftCorner(d, d).transpose())
313 .eval());
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());
319 }
320
321 for (const auto &block: result.blocks)
322 if (!block.allFinite())
323 throw std::runtime_error(
324 "Restricted inverse diagonal blocks are nonfinite");
325 return result;
326}

◆ restrictedTraceInverseProjection()

RestrictedInverseProjection gpr::quantum::gaussian::restrictedTraceInverseProjection ( const std::vector< Segment > & segments,
const Matrix & twist,
const Matrix & orthonormalNormals,
const Matrix & features )

Definition at line 328 of file GaussianTwistedRestriction.cpp.

331{
332 const auto value = restrictedProjection(segments, twist, orthonormalNormals,
333 features, 1., false);
334 return {value.covariance, value.relativeEquationResidual,
335 value.relativeConstraintResidual, value.symmetryRoundoffTolerance};
336}

◆ solveRestrictedTraceKernel()

RestrictedKernelSolution gpr::quantum::gaussian::solveRestrictedTraceKernel ( const std::vector< Segment > & segments,
const Matrix & twist,
const Matrix & orthonormalNormals,
const Matrix & sources )

Definition at line 256 of file GaussianTwistedRestriction.cpp.

259{
261 restrictedProjection(segments, twist, orthonormalNormals, sources, 1.,
262 false, &result);
263 return result;
264}

◆ traceDeterminant()

TwistedTraceResult gpr::quantum::gaussian::traceDeterminant ( const std::vector< Segment > & segments,
const Matrix & twist )

Definition at line 27 of file GaussianTwistedTrace.cpp.

29{
30 const Factorization open(segments);
31 const Eigen::Index d = open.blockDimension();
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");
41 if (segments.size() >
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");
45
46 const Matrix endpoint =
47 segments.front().A +
48 twist.transpose() * segments.back().rightEndpoint() * twist;
49 Matrix correction;
50 if (segments.size() == 1) {
51 correction = segments.front().L * twist +
52 twist.transpose() * segments.front().L.transpose();
53 } else {
54 Matrix coupling = Matrix::Zero(open.interiorDimension(), d);
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;
61 }
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");
73
74 TwistedTraceResult result;
75 result.inertia = open.inertia();
76 result.dimension = static_cast<Eigen::Index>(segments.size()) * d;
77 result.duration = open.duration();
78 double logDet = open.logAbsD();
79 for (double value: eig.eigenvalues()) {
80 if (std::abs(value) <= tolerance) {
81 ++result.unresolvedSchurEigenvalues;
82 ++result.inertia.zero;
83 } else {
84 if (value > 0)
85 ++result.inertia.positive;
86 else
87 ++result.inertia.negative;
88 logDet += std::log(std::abs(value));
89 }
90 }
91 if (!result.unresolvedSchurEigenvalues) {
92 if (!std::isfinite(logDet))
93 throw std::overflow_error(
94 "Nonfinite normalized twisted determinant");
95 result.logAbsNormalizedDet = logDet;
96 }
97 return result;
98}