46 const Eigen::VectorXd& curvatures,
const Eigen::MatrixXd& modes,
47 Eigen::Index mode_index,
const Eigen::VectorXd& force,
double radius,
48 const std::vector<std::pair<Eigen::VectorXd, double>>& measured_negative)
51 const Eigen::Index m = curvatures.size();
52 if (m == 0 || modes.cols() != m || modes.rows() != force.size() ||
53 mode_index < 0 || mode_index >= m || !(radius > 0.0) ||
54 !std::isfinite(radius) || !force.allFinite() ||
55 !(curvatures(mode_index) < 0.0))
57 Eigen::VectorXd mu = curvatures;
58 mu(mode_index) = -mu(mode_index);
59 Eigen::VectorXd c = modes.transpose() * force;
60 c(mode_index) = -c(mode_index);
61 if (!c.allFinite())
return out;
62 double requested_residual = -1.0;
63 for (Eigen::Index k = 0; k < m; ++k) {
64 if (k == mode_index || mu(k) > 0.0) {
65 out.kept_residual_sq += c(k) * c(k);
68 bool measured =
false;
69 for (
const auto& [direction, measured_curvature]: measured_negative) {
70 if (direction.size() != modes.rows())
continue;
71 const double overlap = direction.dot(modes.col(k));
72 if (overlap * overlap > 0.5 * direction.squaredNorm()) {
73 mu(k) = measured_curvature;
79 out.kept_residual_sq += c(k) * c(k);
80 if (c(k) != 0.0)
out.descends_measured =
true;
83 out.flat_residual_sq += c(k) * c(k);
84 if (std::abs(c(k)) > requested_residual) {
86 requested_residual = std::abs(c(k));
92 Eigen::Index minimum_mode;
93 const double mu_min = mu.minCoeff(&minimum_mode);
94 const double sigma_floor = std::max(0.0, -mu_min);
95 auto step_norm = [&](
double sigma) {
97 for (Eigen::Index k = 0; k < m; ++k) {
98 const double d = mu(k) + sigma;
100 if (c(k) != 0.0)
return std::numeric_limits<double>::infinity();
103 sum += (c(k) / d) * (c(k) / d);
105 return std::sqrt(sum);
107 double sigma = sigma_floor;
108 double length = step_norm(sigma);
109 if (!(length <= radius)) {
112 double lower = sigma_floor;
113 double upper = sigma_floor + c.norm() / radius;
114 while (step_norm(upper) > radius)
115 upper = 2.0 * upper + c.norm() / radius;
117 for (
int iteration = 0; iteration < 200; ++iteration) {
118 const double s_norm = step_norm(sigma);
119 if (std::isfinite(s_norm) && s_norm > 0.0) {
120 double derivative = 0.0;
121 for (Eigen::Index k = 0; k < m; ++k) {
122 const double d = mu(k) + sigma;
123 if (d > 0.0) derivative += (c(k) * c(k)) / (d * d * d);
125 derivative /= (s_norm * s_norm * s_norm);
126 const double phi = 1.0 / s_norm - 1.0 / radius;
127 if (std::abs(phi) * radius <= 1.0e-10)
break;
132 double next = sigma - phi / derivative;
133 if (!(next > lower) || !(next < upper) || !std::isfinite(next))
134 next = 0.5 * (lower + upper);
138 sigma = 0.5 * (lower + upper);
140 if (upper - lower <= 1.0e-14 * std::max(1.0, upper))
break;
142 length = step_norm(sigma);
144 Eigen::VectorXd step = Eigen::VectorXd::Zero(modes.rows());
145 for (Eigen::Index k = 0; k < m; ++k) {
146 const double d = mu(k) + sigma;
147 if (d > 0.0) step += (c(k) / d) * modes.col(k);
149 if (sigma_floor > 0.0 && sigma == sigma_floor && length < radius) {
153 const double relative_length = length / radius;
154 const double remaining =
155 radius * std::sqrt(std::max(0.0, (1.0 - relative_length) *
156 (1.0 + relative_length)));
157 step += remaining * modes.col(minimum_mode);
158 out.descends_measured =
true;
160 if (!step.allFinite() || !(step.norm() > 0.0))
return out;
164 out.length = step.norm();