24 const std::vector<gaussian::Segment>& segments,
25 const Eigen::MatrixXd& normal)
27 using M = Eigen::MatrixXd;
29 throw std::invalid_argument(
"Nonempty arc search kernel required");
30 const auto d = segments.front().A.rows();
31 if (d < 1 || normal.rows() != Eigen::Index(segments.size()) * d ||
32 normal.cols() < 1 || !normal.allFinite())
33 throw std::invalid_argument(
34 "Matching finite arc phase normals required");
37 for (
const auto& link: segments)
38 scale = std::max(scale, link.A.cwiseAbs().maxCoeff() +
39 link.rightEndpoint().cwiseAbs().maxCoeff() +
40 2 * link.L.cwiseAbs().maxCoeff());
41 auto assess = [&](
double shift) {
44 const M right = link.rightEndpoint();
46 link.A.diagonal().array() += shift;
50 result.
segments, M::Identity(d, d), normal);
51 if (spectrum.inertia.zero || spectrum.unresolvedBorderEigenvalues)
52 throw std::runtime_error(
53 "Arc shift lies on an unresolved eigenvalue");
55 return spectrum.inertia.negative;
57 const auto original = assess(0.);
58 if (original == 1)
return result;
59 double low = 0., high = 0.;
60 bool bracketed =
false;
61 for (
unsigned k = 0; k < 48; ++k) {
62 const double trial = original > 1 ? scale : -scale;
63 const auto negative = assess(trial);
64 if (negative == 1)
return result;
65 if ((original > 1 && negative == 0) ||
66 (original == 0 && negative > 1)) {
67 low = original > 1 ? 0. : trial;
68 high = original > 1 ? trial : 0.;
75 throw std::runtime_error(
"Cannot bracket an index-one arc kernel");
76 for (
unsigned k = 0; k < 80; ++k) {
77 const double middle = low + .5 * (high - low);
78 if (middle == low || middle == high)
break;
81 negative = assess(middle);
82 }
catch (
const std::runtime_error&) {
83 const double offset = 256 * std::numeric_limits<double>::epsilon() *
84 std::max({1., scale, std::abs(middle)});
85 const double adjacent = std::min(high, middle + offset);
86 if (adjacent == high)
throw;
87 negative = assess(adjacent);
89 if (negative == 1)
return result;
95 throw std::runtime_error(
"No resolved index-one arc shift interval exists");
100 const std::vector<Eigen::VectorXd>& displacement)
102 if (displacement.size() != state.
gradient.size() ||
104 throw std::invalid_argument(
105 "Matching arc quadratic displacement required");
107 for (std::size_t j = 0; j < displacement.size(); ++j) {
108 const auto next = (j + 1) % displacement.size();
110 result += state.
gradient[j].dot(displacement[j]) +
111 .5 * displacement[j].dot(link.A * displacement[j]) +
112 .5 * displacement[next].dot(link.rightEndpoint() *
113 displacement[next]) -
114 displacement[j].dot(link.L * displacement[next]);