Loading...
Searching...
No Matches
TwistedConstraintNewton.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
3
4#include <Eigen/LU>
5#include <algorithm>
6#include <cmath>
7#include <limits>
8#include <stdexcept>
9
10namespace gpr::quantum {
12 const std::vector<Eigen::MatrixXd> &diagonal,
13 const std::vector<Eigen::VectorXd> &gradient,
14 const std::vector<Eigen::MatrixXd> &normals, double coupling,
15 const Eigen::MatrixXd &twist, const Eigen::VectorXd &correction)
16{
17 if (!(coupling > 0) || !std::isfinite(coupling) || diagonal.empty())
18 throw std::invalid_argument("Finite positive scalar coupling required");
19 const Eigen::MatrixXd matrix =
20 coupling * Eigen::MatrixXd::Identity(diagonal.front().rows(),
21 diagonal.front().rows());
22 return solveTwistedConstraints(diagonal, gradient, normals, matrix, twist,
23 correction);
24}
25
27 const std::vector<Eigen::MatrixXd> &diagonal,
28 const std::vector<Eigen::VectorXd> &gradient,
29 const std::vector<Eigen::MatrixXd> &normals,
30 const Eigen::MatrixXd &coupling, const Eigen::MatrixXd &twist,
31 const Eigen::VectorXd &correction)
32{
33 using Matrix = Eigen::MatrixXd;
34 using Vector = Eigen::VectorXd;
35 if (diagonal.empty() || gradient.size() != diagonal.size() ||
36 normals.size() != diagonal.size() || !coupling.allFinite() ||
37 !correction.allFinite())
38 throw std::invalid_argument("Invalid twisted constrained chain");
39 const std::size_t n = diagonal.size();
40 const Eigen::Index d = gradient.front().size(), k = correction.size();
41 if (coupling.rows() != d || coupling.cols() != d ||
42 (coupling - coupling.transpose()).norm() >
43 64 * std::numeric_limits<double>::epsilon() * d *
44 std::max(1., coupling.norm()))
45 throw std::invalid_argument(
46 "Finite symmetric matrix coupling required");
47 if (!d || twist.rows() != d || twist.cols() != d || !twist.allFinite() ||
48 (twist.transpose() * twist - Matrix::Identity(d, d)).norm() > 1e-12 * d)
49 throw std::invalid_argument("Expected orthogonal twist");
50 Matrix gram = Matrix::Zero(k, k);
51 for (std::size_t j = 0; j < n; ++j) {
52 if (diagonal[j].rows() != d || diagonal[j].cols() != d ||
53 gradient[j].size() != d || normals[j].rows() != d ||
54 normals[j].cols() != k || !diagonal[j].allFinite() ||
55 !gradient[j].allFinite() || !normals[j].allFinite() ||
56 (diagonal[j] - diagonal[j].transpose()).norm() >
57 64 * std::numeric_limits<double>::epsilon() * d *
58 diagonal[j].norm())
59 throw std::invalid_argument("Inconsistent constrained blocks");
60 gram += normals[j].transpose() * normals[j];
61 }
62 if ((gram - Matrix::Identity(k, k)).norm() > 1e-10 * k)
63 throw std::invalid_argument(
64 "Constraint columns must be globally orthonormal");
66 Matrix reduced = Matrix::Zero(d + k, d + k);
67 Vector right(d + k);
68 std::vector<Matrix> solved, inverse;
69 if (n == 1) {
70 reduced.topLeftCorner(d, d) =
71 diagonal[0] - coupling * twist - twist.transpose() * coupling;
72 reduced.topRightCorner(d, k) = normals[0];
73 reduced.bottomLeftCorner(k, d) = normals[0].transpose();
74 right.head(d) = -gradient[0];
75 right.tail(k) = correction;
76 } else {
77 std::vector<Matrix> forward;
78 for (std::size_t j = 1; j < n; ++j) {
79 Matrix pivot = diagonal[j], rhs = Matrix::Zero(d, d + 1 + k);
80 if (j == 1) rhs.leftCols(d) -= coupling;
81 if (j + 1 == n) rhs.leftCols(d) -= coupling * twist;
82 rhs.col(d) = gradient[j];
83 rhs.rightCols(k) = normals[j];
84 if (j > 1) {
85 pivot -= coupling * inverse.back() * coupling;
86 rhs += coupling * forward.back();
87 }
88 Eigen::FullPivLU<Matrix> factor(pivot);
89 if (!factor.isInvertible()) return result;
90 inverse.push_back(factor.solve(Matrix::Identity(d, d)));
91 forward.push_back(factor.solve(rhs));
92 if (!inverse.back().allFinite() || !forward.back().allFinite())
93 return result;
94 }
95 solved = forward;
96 for (std::size_t j = n - 2; j > 0; --j)
97 solved[j - 1] += inverse[j - 1] * coupling * solved[j];
98 const Matrix btSolve = -coupling * solved.front() -
99 twist.transpose() * coupling * solved.back();
100 reduced.topLeftCorner(d, d) = diagonal[0] - btSolve.leftCols(d);
101 reduced.topRightCorner(d, k) = normals[0] - btSolve.rightCols(k);
102 reduced.bottomLeftCorner(k, d) =
103 reduced.topRightCorner(d, k).transpose();
104 Matrix qSolveQ = Matrix::Zero(k, k);
105 Vector qSolveG = Vector::Zero(k);
106 for (std::size_t j = 1; j < n; ++j) {
107 qSolveQ += normals[j].transpose() * solved[j - 1].rightCols(k);
108 qSolveG += normals[j].transpose() * solved[j - 1].col(d);
109 }
110 reduced.bottomRightCorner(k, k) = -qSolveQ;
111 right.head(d) = -gradient[0] + btSolve.col(d);
112 right.tail(k) = correction + qSolveG;
113 }
114 if (!reduced.allFinite() || !right.allFinite()) return result;
115 Eigen::FullPivLU<Matrix> border(reduced);
116 if (!border.isInvertible()) return result;
117 const Vector endpoint = border.solve(right);
118 if (!endpoint.allFinite()) return result;
119 result.multipliers = endpoint.tail(k);
120 result.displacement.push_back(endpoint.head(d));
121 for (std::size_t j = 1; j < n; ++j)
122 result.displacement.push_back(
123 -solved[j - 1].col(d) -
124 solved[j - 1].leftCols(d) * endpoint.head(d) -
125 solved[j - 1].rightCols(k) * result.multipliers);
126 double error = 0, scale = 0;
127 Vector projected = Vector::Zero(k);
128 for (std::size_t j = 0; j < n; ++j) {
129 Vector residual = diagonal[j] * result.displacement[j] +
130 normals[j] * result.multipliers + gradient[j];
131 if (j) residual -= coupling * result.displacement[j - 1];
132 if (j + 1 < n) residual -= coupling * result.displacement[j + 1];
133 if (!j)
134 residual -=
135 twist.transpose() * coupling * result.displacement.back();
136 if (j + 1 == n)
137 residual -= coupling * twist * result.displacement.front();
138 error = std::max(error, residual.norm());
139 scale = std::max(scale, gradient[j].norm());
140 projected += normals[j].transpose() * result.displacement[j];
141 if (!result.displacement[j].allFinite()) return result;
142 }
143 result.relativeResidual = error / std::max(1.0, scale);
144 result.constraintResidual = (projected - correction).norm();
145 result.invertible = std::isfinite(result.relativeResidual) &&
146 std::isfinite(result.constraintResidual);
147 return result;
148}
149} // namespace gpr::quantum
Models for quantum-path (instanton) searches: arc and tail actions, amplitude continuation.
Eigen::MatrixXd Matrix
Eigen::VectorXd Vector
TwistedConstraintStep solveTwistedConstraints(const std::vector< Eigen::MatrixXd > &diagonal, const std::vector< Eigen::VectorXd > &gradient, const std::vector< Eigen::MatrixXd > &normals, double coupling, const Eigen::MatrixXd &twist, const Eigen::VectorXd &correction)
std::vector< Eigen::VectorXd > displacement