16namespace meshprojection_detail {
17using Vector = Eigen::VectorXd;
18using Matrix = Eigen::MatrixXd;
21 std::vector<Vector> tangent, leftUnit, rightUnit;
22 std::vector<double> leftWeight, rightWeight;
24inline Geometry geometry(
const std::vector<Vector>& path,
25 const std::vector<double>& weights)
27 if (path.size() < 3 || path.front().size() < 1 ||
28 (!weights.empty() && weights.size() + 1 != path.size()))
29 throw std::invalid_argument(
30 "Compatible geodesic mesh geometry required");
31 for (
double weight: weights)
32 if (!(weight > 0.) || !std::isfinite(weight))
33 throw std::invalid_argument(
34 "Positive finite geodesic mesh weights required");
35 const auto n = path.size();
36 for (
const auto& point: path)
37 if (point.size() != path.front().size() || !point.allFinite())
38 throw std::invalid_argument(
39 "Finite matching mesh coordinates required");
41 result.constraint.resize(n - 2);
42 result.tangent.resize(n);
43 result.leftUnit.resize(n);
44 result.rightUnit.resize(n);
45 result.leftWeight.resize(n);
46 result.rightWeight.resize(n);
47 for (std::size_t j = 1; j + 1 < n; ++j) {
48 const Vector left = path[j] - path[j - 1],
49 right = path[j + 1] - path[j];
50 const double a = left.stableNorm(), b = right.stableNorm();
51 if (!(a > 0.) || !(b > 0.) || !std::isfinite(a + b))
52 throw std::domain_error(
53 "Distinct finite geodesic mesh links required");
54 double hm = weights.empty() ? 1. : weights[j - 1],
55 hp = weights.empty() ? 1. : weights[j];
56 const double scale = std::max(hm, hp);
59 if (!(hm > 0.) || !(hp > 0.))
60 throw std::domain_error(
61 "Resolved graded geodesic weights required");
62 result.leftUnit[j] = left / a;
63 result.rightUnit[j] = right / b;
64 const Vector tangent =
66 ?
Vector(left / a + right / b)
67 : Vector((hp / hm * left + hm / hp * right) / (hm + hp));
68 const double norm = tangent.stableNorm();
69 if (!(norm > 0.) || !std::isfinite(norm))
70 throw std::domain_error(
"Resolved geodesic mesh tangent required");
71 result.tangent[j] = tangent / norm;
72 result.leftWeight[j] = hm;
73 result.rightWeight[j] = hp;
74 result.constraint[j - 1] = 2 * (hp * a - hm * b) / (hm + hp);
81 const std::vector<Eigen::VectorXd>& path,
82 const std::vector<double>& weights = {})
84 return meshprojection_detail::geometry(path, weights).constraint;
94 const std::vector<Eigen::VectorXd>& path,
95 const std::vector<double>& weights,
double tolerance,
96 const Eigen::VectorXd& target = {})
98 using namespace meshprojection_detail;
99 if (!(tolerance > 0.) || !std::isfinite(tolerance))
100 throw std::invalid_argument(
101 "Positive mesh projection tolerance required");
103 auto data =
geometry(current, weights);
104 const auto m = data.constraint.size();
105 Vector goal = Vector::Zero(m);
106 if (target.size()) goal = target;
107 if (goal.size() != m || !goal.allFinite())
108 throw std::invalid_argument(
109 "Finite matching mesh constraint target required");
110 for (
unsigned iteration = 0; iteration < 32; ++iteration) {
111 const Vector residual = data.constraint - goal;
112 const double error = residual.cwiseAbs().maxCoeff();
113 if (error <= tolerance)
return current;
114 Matrix jacobian = Matrix::Zero(m, m);
115 for (Eigen::Index row = 0; row < m; ++row) {
116 const auto j =
static_cast<std::size_t
>(row + 1);
117 const double hm = data.leftWeight[j], hp = data.rightWeight[j],
121 (hp * data.leftUnit[j] + hm * data.rightUnit[j])
122 .dot(data.tangent[j]) /
125 jacobian(row, row - 1) =
126 -2 * hp * data.leftUnit[j].dot(data.tangent[j - 1]) / sum;
128 jacobian(row, row + 1) =
129 -2 * hm * data.rightUnit[j].dot(data.tangent[j + 1]) / sum;
131 Eigen::FullPivLU<Matrix> factor(jacobian);
132 if (factor.rank() != m)
133 throw std::domain_error(
134 "Geodesic tangential mesh Jacobian is singular");
135 const Vector step = factor.solve(-residual);
136 if (!step.allFinite() ||
137 (jacobian * step + residual).stableNorm() >
138 1e-10 * std::max(1., residual.stableNorm()))
139 throw std::domain_error(
"Unresolved geodesic mesh correction");
140 bool accepted =
false;
142 for (
unsigned trial = 0; trial < 24; ++trial, scale *= .5) {
143 auto candidate = current;
144 for (Eigen::Index row = 0; row < m; ++row)
145 candidate[row + 1] += scale * step[row] * data.tangent[row + 1];
147 auto next = geometry(candidate, weights);
148 if ((next.constraint - goal).cwiseAbs().maxCoeff() < error) {
149 current = std::move(candidate);
150 data = std::move(next);
154 }
catch (
const std::domain_error&) {
158 throw std::domain_error(
159 "Geodesic mesh correction does not decrease the constraint");
161 throw std::domain_error(
"Geodesic mesh projection iteration limit");
Models for quantum-path (instanton) searches: arc and tail actions, amplitude continuation.
std::vector< Eigen::VectorXd > retractGeodesicMesh(const std::vector< Eigen::VectorXd > &path, const std::vector< double > &weights, double tolerance, const Eigen::VectorXd &target={})
Eigen::VectorXd geodesicMeshConstraints(const std::vector< Eigen::VectorXd > &path, const std::vector< double > &weights={})