6#include <Eigen/Cholesky>
8#include <Eigen/Eigenvalues>
38 out <<
"Secant Hessian-consistency rejections: "
53 const Eigen::VectorXd& gradient,
double energy,
57 const bool had_reference =
62 const bool accepted = secant.
observe(position, gradient, energy);
88 return "no accepted pair";
90 return "secant and model dimensions differ";
92 return "no resolved curvature";
94 return "no usable current direction";
96 return "not positive definite";
110 if (!secant.
available())
return std::nullopt;
112 if (basis.rows() != secant.
hessian().rows() || basis.cols() == 0)
114 const Eigen::MatrixXd projected =
115 basis.transpose() * secant.
hessian() * basis;
116 const Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> spectrum(
117 0.5 * (projected + projected.transpose()));
118 if (spectrum.info() != Eigen::Success ||
119 !spectrum.eigenvalues().allFinite() ||
120 !spectrum.eigenvectors().allFinite())
121 throw std::runtime_error(
"secant curvature seed eigensolve failed");
122 const double scale = spectrum.eigenvalues().cwiseAbs().maxCoeff();
123 const double tolerance =
124 scale * std::sqrt(std::numeric_limits<double>::epsilon());
125 if (!(spectrum.eigenvalues()(0) < -tolerance))
return std::nullopt;
126 Eigen::VectorXd mode = basis * spectrum.eigenvectors().col(0);
127 const double norm = mode.norm();
128 if (!(norm > 0.0) || !std::isfinite(norm))
129 throw std::runtime_error(
"secant curvature seed mode is degenerate");
145 const SecantHessian& secant,
const Eigen::VectorXd& current_direction)
148 const double norm = current_direction.norm();
149 const bool usable = current_direction.size() > 0 &&
150 current_direction.allFinite() && norm > 0.0 &&
152 if (usable) seed.
direction = current_direction / norm;
170 if (mode->dot(seed.
direction) < 0.0) *mode *= -1.0;
182 if (!secant.
available())
return std::nullopt;
184 const double ss = step.squaredNorm();
185 if (!(ss > 0.0) || !step.allFinite())
return std::nullopt;
186 const double curvature = step.dot(secant.
hessian() * step) / ss;
187 if (!(curvature > 0.0) || !std::isfinite(curvature))
return std::nullopt;
188 return 1.0 / curvature;
205 const Eigen::MatrixXd symmetric =
207 const Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> spectrum(
208 symmetric, Eigen::EigenvaluesOnly);
209 if (spectrum.info() != Eigen::Success ||
210 !spectrum.eigenvalues().allFinite()) {
214 const double scale = spectrum.eigenvalues().cwiseAbs().maxCoeff();
215 if (!(scale > 0.0)) {
219 if (!(spectrum.eigenvalues().minCoeff() >
220 scale * std::sqrt(std::numeric_limits<double>::epsilon()))) {
224 out.factor.compute(symmetric);
225 out.status =
out.factor.info() == Eigen::Success
245 Eigen::MatrixXd& model,
const Eigen::MatrixXd& ritz,
246 const Eigen::MatrixXd* basis)
248 const Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> spectrum(ritz);
249 if (spectrum.info() != Eigen::Success ||
250 !spectrum.eigenvalues().allFinite() ||
251 !spectrum.eigenvectors().allFinite())
252 throw std::runtime_error(
"secant curvature seed eigensolve failed");
253 const double scale = spectrum.eigenvalues().cwiseAbs().maxCoeff();
254 const double tolerance =
255 scale * std::sqrt(std::numeric_limits<double>::epsilon());
256 Eigen::MatrixXd span(model.rows(), 0);
257 Eigen::VectorXd values(0);
258 for (Eigen::Index k = 0; k < spectrum.eigenvalues().size(); ++k) {
259 if (!(std::abs(spectrum.eigenvalues()(k)) > tolerance))
continue;
260 const Eigen::Index column = span.cols();
261 span.conservativeResize(model.rows(), column + 1);
262 values.conservativeResize(column + 1);
265 ? Eigen::VectorXd(*basis * spectrum.eigenvectors().col(k))
266 :
Eigen::VectorXd(spectrum.eigenvectors().col(k));
267 values(column) = spectrum.eigenvalues()(k);
270 const Eigen::MatrixXd complement =
271 Eigen::MatrixXd::Identity(model.rows(), model.cols()) -
272 span * span.transpose();
273 model = complement * model * complement +
274 span * values.asDiagonal() * span.transpose();
275 model = 0.5 * (model + model.transpose());
287 Eigen::MatrixXd& model,
const Eigen::MatrixXd& secant_hessian)
289 if (model.rows() == 0 || model.rows() != model.cols() ||
290 secant_hessian.rows() != model.rows() ||
291 secant_hessian.cols() != model.cols() || !model.allFinite() ||
292 !secant_hessian.allFinite())
293 throw std::invalid_argument(
294 "secant curvature seed requires matching finite Hessians");
295 const Eigen::MatrixXd symmetric =
296 0.5 * (secant_hessian + secant_hessian.transpose());
297 return detail::seedModelOnRitzSpan(model, symmetric,
nullptr);
306 Eigen::MatrixXd& model,
const Eigen::MatrixXd& secant_hessian,
307 const Eigen::MatrixXd& measured_steps)
309 if (model.rows() == 0 || model.rows() != model.cols() ||
310 secant_hessian.rows() != model.rows() ||
311 secant_hessian.cols() != model.cols() || !model.allFinite() ||
312 !secant_hessian.allFinite() || measured_steps.rows() != model.rows() ||
313 !measured_steps.allFinite())
314 throw std::invalid_argument(
315 "secant curvature seed requires matching finite Hessians and "
317 if (measured_steps.cols() == 0)
319 const Eigen::ColPivHouseholderQR<Eigen::MatrixXd> step_qr(measured_steps);
320 const Eigen::Index rank = step_qr.rank();
322 const Eigen::MatrixXd basis =
323 step_qr.householderQ() * Eigen::MatrixXd::Identity(model.rows(), rank);
324 const Eigen::MatrixXd symmetric =
325 0.5 * (secant_hessian + secant_hessian.transpose());
326 const Eigen::MatrixXd projected = basis.transpose() * symmetric * basis;
327 return detail::seedModelOnRitzSpan(
328 model, 0.5 * (projected + projected.transpose()), &basis);
344 const Eigen::MatrixXd& weight,
348 if (secant.
hessian().rows() != model.rows() ||
349 weight.rows() != model.rows() || weight.cols() != model.cols())
351 const Eigen::MatrixXd weighted = secant.
hessian().cwiseProduct(weight);
354 const Eigen::VectorXd diagonal = weight.diagonal();
355 if (!(diagonal.array() > 0.0).all() || !diagonal.allFinite())
356 throw std::invalid_argument(
357 "measured-step secant seed requires a positive coordinate weight");
358 const Eigen::MatrixXd steps =
359 (secant.
measuredSteps().array().colwise() / diagonal.array().sqrt())
375template <
class Prior>
383 double prior_energy = 0.0;
384 Eigen::VectorXd prior_gradient;
385 prior(sample.position, prior_energy, prior_gradient);
386 if (!std::isfinite(prior_energy) ||
387 prior_gradient.size() != sample.gradient.size() ||
388 !prior_gradient.allFinite())
389 throw std::runtime_error(
390 "secant quadratic mean requires a finite prior gradient");
392 sample.gradient - prior_gradient,
393 sample.energy - prior_energy, counts);
bool available() const noexcept
const Eigen::VectorXd & lastDirection() const noexcept
const Eigen::MatrixXd & hessian() const noexcept
const Eigen::MatrixXd & measuredSteps() const noexcept
const Eigen::VectorXd & referencePosition() const noexcept
bool observe(const Eigen::VectorXd &position, const Eigen::VectorXd &gradient)
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
std::optional< double > secantPositiveInverseScale(const SecantHessian &secant)
SecantHessian secantFittedToResidual(SecantHessianMethod method, const std::vector< SecantForceSample > &samples, Prior &&prior)
std::optional< Eigen::VectorXd > secantResolvedNegativeMode(const SecantHessian &secant)
void recordSecantObservation(SecantHessian &secant, const Eigen::VectorXd &position, const Eigen::VectorXd &gradient, double energy, SecantCurvatureCounts &counts, SecantAccounting accounting=SecantAccounting::Count)
SecantSeedStatus seedHessianOnSecantRange(Eigen::MatrixXd &model, const Eigen::MatrixXd &secant_hessian)
SecantNewtonFactor secantNewtonFactor(const SecantHessian &secant)
SecantSeed secantSeedDirection(const SecantHessian &secant, const Eigen::VectorXd ¤t_direction)
SecantSeedStatus seedHessianFromSecant(Eigen::MatrixXd &model, const SecantHessian &secant, const Eigen::MatrixXd &weight, SecantSeedSupport support=SecantSeedSupport::WholeHessian)
void writeSecantCurvatureCounts(std::ostream &out, const SecantCurvatureCounts &counts)
SecantSeedStatus seedHessianOnMeasuredStepRange(Eigen::MatrixXd &model, const Eigen::MatrixXd &secant_hessian, const Eigen::MatrixXd &measured_steps)
const char * secantSeedStatusName(SecantSeedStatus status) noexcept
int consistency_rejections
Eigen::LLT< Eigen::MatrixXd > factor
Eigen::VectorXd direction