Loading...
Searching...
No Matches
ArcTailNewton.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2#include "ArcTailNewton.h"
3
4#include <limits>
5
7
8namespace gpr::quantum {
9namespace {
10using M = Eigen::MatrixXd;
11using V = Eigen::VectorXd;
12M applyKernel(const std::vector<gaussian::Segment>& links, const M& x)
13{
14 const auto n = links.size();
15 const auto d = links.front().A.rows();
16 M result = M::Zero(x.rows(), x.cols());
17 for (std::size_t j = 0; j < n; ++j) {
18 const auto k = (j + 1) % n;
19 result.middleRows(j * d, d) += links[j].A * x.middleRows(j * d, d) -
20 links[j].L * x.middleRows(k * d, d);
21 result.middleRows(k * d, d) +=
22 links[j].rightEndpoint() * x.middleRows(k * d, d) -
23 links[j].L.transpose() * x.middleRows(j * d, d);
24 }
25 return result;
26}
27} // namespace
29 const std::vector<M>& normals,
30 const V& correction)
31{
32 const auto n = state.gradient.size();
33 if (n < 2 || state.fluctuationSegments.size() != n ||
34 state.durationCrossGradient.size() != n || normals.size() != n ||
35 correction.size() < 1 || !correction.allFinite() ||
36 !std::isfinite(state.durationGradient) ||
37 !std::isfinite(state.durationHessian))
38 throw std::invalid_argument(
39 "Matching finite arc-tail derivatives, links and constraint blocks "
40 "required");
41 const auto d = state.gradient.front().size(), k = correction.size();
42 M normal(n * d, k);
43 V gradient(n * d), cross(n * d);
44 for (std::size_t j = 0; j < n; ++j) {
45 if (state.gradient[j].size() != d ||
46 state.durationCrossGradient[j].size() != d ||
47 normals[j].rows() != d || normals[j].cols() != k ||
48 !state.gradient[j].allFinite() ||
49 !state.durationCrossGradient[j].allFinite() ||
50 !normals[j].allFinite())
51 throw std::invalid_argument(
52 "Arc-tail Newton metric or derivative dimensions differ");
53 normal.middleRows(j * d, d) = normals[j];
54 gradient.segment(j * d, d) = state.gradient[j];
55 cross.segment(j * d, d) = state.durationCrossGradient[j];
56 }
57 if ((normal.transpose() * normal - M::Identity(k, k)).norm() > 1e-10)
58 throw std::invalid_argument(
59 "Arc-tail constraint normals must be jointly orthonormal");
60 const V particular = normal * correction;
61 M sources(n * d, 2);
62 sources.col(0) =
63 -gradient - applyKernel(state.fluctuationSegments, particular);
64 sources.col(1) = cross;
65 const auto solved = gaussian::solveRestrictedTraceKernel(
66 state.fluctuationSegments, M::Identity(d, d), normal, sources);
67 const V first = solved.values.col(0), response = solved.values.col(1);
68 const double contraction = cross.dot(response);
69 const double schur = state.durationHessian - contraction;
70 const double tolerance =
71 128 * std::numeric_limits<double>::epsilon() *
72 (std::abs(state.durationHessian) + std::abs(contraction));
73 if (!std::isfinite(schur) || std::abs(schur) <= tolerance)
74 throw std::runtime_error(
75 "Arc-tail duration equation has an unresolved Schur complement");
76 ArcTailNewtonStep result;
77 result.durationSchurComplement = schur;
79 (-state.durationGradient - cross.dot(particular) - cross.dot(first)) /
80 schur;
81 const V step = particular + first - response * result.durationDisplacement;
82 result.multipliers =
83 solved.multipliers.col(0) -
84 solved.multipliers.col(1) * result.durationDisplacement;
85 const V spatial = applyKernel(state.fluctuationSegments, step) +
86 cross * result.durationDisplacement +
87 normal * result.multipliers + gradient;
88 const double temporal =
89 cross.dot(step) + state.durationHessian * result.durationDisplacement +
90 state.durationGradient;
92 std::hypot(spatial.norm(), temporal) /
93 std::max(1., std::hypot(gradient.norm(), state.durationGradient));
94 result.constraintResidual = (normal.transpose() * step - correction).norm();
95 if (!step.allFinite() || !std::isfinite(result.durationDisplacement) ||
96 result.relativeEquationResidual > 1e-8 ||
97 result.constraintResidual > 1e-10)
98 throw std::runtime_error(
99 "Arc-tail Newton step fails its joint coordinate and energy "
100 "equations");
101 for (std::size_t j = 0; j < n; ++j)
102 result.displacement.push_back(step.segment(j * d, d));
103 return result;
104}
105} // namespace gpr::quantum
@ x
Definition Enums.h:28
RestrictedKernelSolution solveRestrictedTraceKernel(const std::vector< Segment > &segments, const Matrix &twist, const Matrix &orthonormalNormals, const Matrix &sources)
Models for quantum-path (instanton) searches: arc and tail actions, amplitude continuation.
ArcTailNewtonStep solveArcTailNewton(const ArcTailEvaluation &state, const std::vector< M > &normals, const V &correction)
std::vector< Eigen::VectorXd > gradient
std::vector< Eigen::VectorXd > durationCrossGradient
std::vector< gpr::quantum::gaussian::Segment > fluctuationSegments
std::vector< Eigen::VectorXd > displacement