Loading...
Searching...
No Matches
RestrictedPartitionedRfo.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4#pragma once
5
6#include <Eigen/Dense>
7#include <algorithm>
8#include <cmath>
9#include <limits>
10#include <utility>
11#include <vector>
12
13namespace gpr::optim {
14
16 bool valid = false;
18 bool boundary = false;
19 bool hard_case = false;
21 double scaling = 1.0;
22 Eigen::VectorXd eigen_step;
23 Eigen::VectorXd cartesian_step;
24};
25
47 const Eigen::VectorXd &curvatures, const Eigen::VectorXd &gradient,
48 const Eigen::MatrixXd &cartesian_modes, double radius)
49{
51 const Eigen::Index n = curvatures.size();
52 if (n < 2 || gradient.size() != n || cartesian_modes.cols() != n ||
53 cartesian_modes.rows() == 0 || !curvatures.allFinite() ||
54 !gradient.allFinite() || !cartesian_modes.allFinite() ||
55 !(radius > 0.0) || !std::isfinite(radius))
56 return out;
57 for (Eigen::Index k = 1; k < n; ++k)
58 if (curvatures(k) < curvatures(k - 1)) return out;
59
60 // Stable and direct Euclidean norms can round to adjacent values at
61 // the radius. The feasible bracket must satisfy both evaluations;
62 // the stable value also handles overflow in the direct sum of squares.
63 const auto cartesian_length = [](const Eigen::VectorXd &step) {
64 const double stable = step.stableNorm();
65 const double direct = step.norm();
66 return std::isfinite(direct) ? std::max(stable, direct) : stable;
67 };
68
69 const auto boundary_step = [&](Eigen::VectorXd direction) {
71 Eigen::VectorXd cartesian = cartesian_modes * direction;
72 double length = cartesian_length(cartesian);
73 if (!(length > 0.0) || !std::isfinite(length) || !cartesian.allFinite())
74 return result;
75 direction *= radius / length;
76 if (!direction.allFinite()) return result;
77 cartesian = cartesian_modes * direction;
78 length = cartesian_length(cartesian);
79 // Reconstruction is checked in the physical metric. A downward
80 // rounding of the radial factor keeps a boundary point feasible.
81 while (length > radius && std::isfinite(length)) {
82 const double factor = std::nextafter(radius / length, 0.0);
83 if (!(factor > 0.0 && factor < 1.0)) return result;
84 const Eigen::VectorXd smaller = direction * factor;
85 if ((smaller.array() == direction.array()).all()) return result;
86 direction = smaller;
87 cartesian = cartesian_modes * direction;
88 length = cartesian_length(cartesian);
89 }
90 if (!(length > 0.0 && length <= radius) || !direction.allFinite() ||
91 !cartesian.allFinite())
92 return result;
93 result.valid = true;
94 result.boundary = true;
95 result.hard_case = true;
96 result.eigen_step = std::move(direction);
97 result.cartesian_step = std::move(cartesian);
98 return result;
99 };
100
101 // A nonnegative maximized mode with zero gradient has no preferred
102 // affine displacement. Its canonical boundary direction also covers
103 // a flat selected mode without inventing a nonzero gradient.
104 if (gradient(0) == 0.0 && curvatures(0) >= 0.0)
105 return boundary_step(Eigen::VectorXd::Unit(n, 0));
106
107 struct Evaluation {
108 bool valid = false;
109 bool hard_case = false;
110 double length = std::numeric_limits<double>::infinity();
111 Eigen::VectorXd eigen_step;
112 Eigen::VectorXd cartesian_step;
113 Eigen::VectorXd hard_direction;
114 };
115
116 const auto evaluate = [&](double c) {
117 Evaluation value;
118 if (!(c >= 1.0) || !std::isfinite(c)) return value;
119
120 // The lowest arrowhead eigenvalue is mu = b_min - delta. Its
121 // positive gap solves a strictly increasing secular equation:
122 // delta - b_min = sum_i (c*g_i)^2 / (b_i - b_min + delta).
123 // Forming the gap directly retains tiny coupled negative modes
124 // without subtracting equal eigenvalues or deflating their coupling.
125 using Wide = long double;
126 const Wide minimum = static_cast<Wide>(curvatures(1));
127 std::vector<Wide> gaps(static_cast<std::size_t>(n - 1));
128 std::vector<Wide> coupling(static_cast<std::size_t>(n - 1));
129 Wide total_norm = 0.0L;
130 Wide minimum_norm = 0.0L;
131 bool coupled_minimum = false;
132 for (Eigen::Index k = 1; k < n; ++k) {
133 const auto i = static_cast<std::size_t>(k - 1);
134 gaps[i] = static_cast<Wide>(curvatures(k)) - minimum;
135 coupling[i] = static_cast<Wide>(c) * static_cast<Wide>(gradient(k));
136 if (!std::isfinite(coupling[i])) return value;
137 total_norm = std::hypot(total_norm, coupling[i]);
138 if (gaps[i] == 0.0L) {
139 minimum_norm = std::hypot(minimum_norm, coupling[i]);
140 if (gradient(k) != 0.0) coupled_minimum = true;
141 }
142 }
143 Eigen::VectorXd tail_step = Eigen::VectorXd::Zero(n - 1);
144 if (!(total_norm == 0.0L && minimum >= 0.0L)) {
145 if (!coupled_minimum) {
146 Wide at_zero = -minimum;
147 for (Eigen::Index k = 1; k < n; ++k) {
148 const auto i = static_cast<std::size_t>(k - 1);
149 if (gaps[i] > 0.0L)
150 at_zero -= coupling[i] * (coupling[i] / gaps[i]);
151 }
152 if (!std::isfinite(at_zero)) return value;
153 if (minimum < 0.0L && at_zero >= 0.0L) {
154 value.valid = true;
155 value.hard_case = true;
156 value.hard_direction = Eigen::VectorXd::Unit(n, 1);
157 return value;
158 }
159 }
160 // Omitting higher eigenspaces gives a lower gap bound; treating
161 // every denominator as delta gives an upper bound. The
162 // rationalized negative-curvature root avoids cancellation.
163 const auto gap_bound = [&](Wide norm) {
164 if (norm == 0.0L) return std::max(minimum, 0.0L);
165 const Wide root = std::hypot(0.5L * minimum, norm);
166 return minimum >= 0.0L
167 ? 0.5L * minimum + root
168 : (norm / (root - 0.5L * minimum)) * norm;
169 };
170 Wide lower_gap = gap_bound(minimum_norm);
171 Wide upper_gap = gap_bound(total_norm);
172 if (!(upper_gap >= lower_gap) || !std::isfinite(upper_gap))
173 return value;
174 if (!(upper_gap > 0.0L)) {
175 value.valid = true;
176 return value;
177 }
178 for (;;) {
179 Wide middle = lower_gap > 0.0L
180 ? std::sqrt(lower_gap) * std::sqrt(upper_gap)
181 : 0.5L * upper_gap;
182 if (!(middle > lower_gap && middle < upper_gap))
183 middle = lower_gap + 0.5L * (upper_gap - lower_gap);
184 if (!(middle > lower_gap && middle < upper_gap)) break;
185 Wide residual = middle - minimum;
186 for (Eigen::Index k = 1; k < n; ++k) {
187 const auto i = static_cast<std::size_t>(k - 1);
188 residual -=
189 coupling[i] * (coupling[i] / (gaps[i] + middle));
190 }
191 if (!std::isfinite(residual)) return value;
192 if (residual > 0.0L) {
193 upper_gap = middle;
194 } else if (residual < 0.0L) {
195 lower_gap = middle;
196 } else {
197 lower_gap = middle;
198 upper_gap = middle;
199 break;
200 }
201 }
202 for (Eigen::Index k = 1; k < n; ++k) {
203 const auto i = static_cast<std::size_t>(k - 1);
204 const Wide denominator = gaps[i] + upper_gap;
205 if (denominator == 0.0L && gradient(k) == 0.0) continue;
206 const Wide coefficient =
207 -static_cast<Wide>(gradient(k)) / denominator;
208 if (!std::isfinite(coefficient) ||
209 std::abs(coefficient) >
210 static_cast<Wide>(std::numeric_limits<double>::max())) {
211 value.valid = true;
212 return value;
213 }
214 tail_step(k - 1) = static_cast<double>(coefficient);
215 }
216 }
217
218 double up_head = 0.0;
219 double up_tail = 1.0;
220 if (gradient(0) != 0.0) {
221 const double b = curvatures(0) / c;
222 const double up_scale =
223 std::max(std::abs(b), std::abs(gradient(0)));
224 const double half_b = 0.5 * (b / up_scale);
225 const double g = gradient(0) / up_scale;
226 const double root = std::hypot(half_b, g);
227 if (curvatures(0) < 0.0) {
228 up_head = g;
229 up_tail = -half_b + root;
230 } else {
231 up_head = half_b + root;
232 up_tail = g;
233 }
234 const double up_norm =
235 std::max(std::abs(up_head), std::abs(up_tail));
236 up_head /= up_norm;
237 up_tail /= up_norm;
238 if (up_tail < 0.0) {
239 up_head = -up_head;
240 up_tail = -up_tail;
241 }
242 }
243 if (up_tail == 0.0) {
244 value.valid = true;
245 return value;
246 }
247 value.eigen_step.resize(n);
248 value.eigen_step(0) = up_head / (c * up_tail);
249 value.eigen_step.tail(n - 1) = tail_step;
250 value.valid = true;
251 // An unrepresentable affine step cannot supply a finite stored
252 // candidate; larger c is still tried.
253 if (!value.eigen_step.allFinite()) return value;
254 value.cartesian_step = cartesian_modes * value.eigen_step;
255 if (!value.cartesian_step.allFinite()) return value;
256 value.length = cartesian_length(value.cartesian_step);
257 return value;
258 };
259
260 Evaluation candidate = evaluate(1.0);
261 if (!candidate.valid) return out;
262 if (candidate.hard_case) return boundary_step(candidate.hard_direction);
263 if (candidate.length <= radius) {
264 if (!(candidate.length > 0.0)) return out;
265 out.valid = true;
266 out.eigen_step = std::move(candidate.eigen_step);
267 out.cartesian_step = std::move(candidate.cartesian_step);
268 return out;
269 }
270
271 double lower = 1.0;
272 double upper = 1.0;
273 Evaluation feasible;
274 for (;;) {
275 const double largest = std::numeric_limits<double>::max();
276 const double next = upper > largest / 2.0 ? largest : 2.0 * upper;
277 if (!(next > upper)) return out;
278 upper = next;
279 candidate = evaluate(upper);
280 if (!candidate.valid) return out;
281 if (candidate.hard_case) return boundary_step(candidate.hard_direction);
282 if (candidate.length <= radius) {
283 feasible = std::move(candidate);
284 break;
285 }
286 lower = upper;
287 }
288 for (;;) {
289 const double middle = lower + 0.5 * (upper - lower);
290 if (!(middle > lower && middle < upper)) break;
291 candidate = evaluate(middle);
292 if (!candidate.valid) return out;
293 if (candidate.hard_case) return boundary_step(candidate.hard_direction);
294 if (candidate.length <= radius) {
295 upper = middle;
296 feasible = std::move(candidate);
297 } else {
298 lower = middle;
299 }
300 }
301 if (!(feasible.length > 0.0 && feasible.length <= radius) ||
302 !feasible.eigen_step.allFinite() ||
303 !feasible.cartesian_step.allFinite())
304 return out;
305 out.valid = true;
306 out.boundary = true;
307 out.scaling = upper;
308 out.eigen_step = std::move(feasible.eigen_step);
309 out.cartesian_step = std::move(feasible.cartesian_step);
310 return out;
311}
312
313} // namespace gpr::optim
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
RestrictedPartitionedRfoStep restrictedPartitionedRfoStep(const Eigen::VectorXd &curvatures, const Eigen::VectorXd &gradient, const Eigen::MatrixXd &cartesian_modes, double radius)
bool boundary
A radius restriction or a homogeneous hard-case boundary is used.
double scaling
c = sqrt(alpha); finite-alpha stationarity applies to regular steps.