Loading...
Searching...
No Matches
LBFGS.cpp
Go to the documentation of this file.
1/*
2** This file is part of eOn.
3**
4** SPDX-License-Identifier: BSD-3-Clause
5**
6** Copyright (c) 2010--present, eOn Development Team
7** All rights reserved.
8**
9** Repo:
10** https://github.com/TheochemUI/eOn
11*/
12// Two-loop L-BFGS (Nocedal 1980; Liu-Nocedal 1989; Nocedal-Wright 7.4).
13// Optional Zhang-Xu modified secant, Li-Fukushima cautious update,
14// Powell / Al-Baali damping, Packwood-Kermode pair preconditioner,
15// Al-Baali extra-updates, and Grippo nonmonotone accept.
16
17#include "eon/LBFGS.h"
18#include "eon/ExactReduction.h"
20#include "eon/PairHessian.h"
21#include "eon/SafeMath.h"
22
23// Eigen's packet saxpy is an fma on NEON and a multiply followed by an add
24// on a nocona x86 unit. The curvature probe and the two-loop step follow
25// the multiply-then-add form.
26#if defined(__clang__)
27#pragma clang fp contract(off)
28#elif defined(__GNUC__)
29#pragma GCC optimize("fp-contract=off")
30#endif
31
32#include <algorithm>
33#include <cmath>
34#include <limits>
35#include <vector>
36
37namespace {
38constexpr double kLbfgsEps = 1e-30;
39
40// y <- y + a * x, one rounding for the product and one for the add.
41void mulAddInto(Eigen::VectorXd &y, double a, const Eigen::VectorXd &x) {
42 const Eigen::Index n = y.size();
43 double *yd = y.data();
44 const double *xd = x.data();
45 for (Eigen::Index i = 0; i < n; ++i) {
46 yd[i] = yd[i] + a * xd[i];
47 }
48}
49
50[[nodiscard]] Eigen::VectorXd mulAdd(const Eigen::VectorXd &y, double a,
51 const Eigen::VectorXd &x) {
52 Eigen::VectorXd out = y;
53 mulAddInto(out, a, x);
54 return out;
55}
56
57void maybeProjectRigid(Eigen::VectorXd &vec, const Eigen::VectorXd &pos,
58 bool enabled) {
59 if (!enabled || pos.size() < 6 || pos.size() % 3 != 0) {
60 return;
61 }
62 const long nat = pos.size() / 3;
63 const AtomMatrix coords = Eigen::Map<const AtomMatrix>(pos.data(), nat, 3);
65}
66
67std::vector<int> twoLoopIndex(int nPairs, int extra) {
68 std::vector<int> idx;
69 idx.reserve(static_cast<size_t>(nPairs + std::max(extra, 0)));
70 for (int i = 0; i < nPairs; ++i) {
71 idx.push_back(i);
72 }
73 for (int k = 0; k < extra && nPairs > 0; ++k) {
74 idx.push_back(nPairs - 1);
75 }
76 return idx;
77}
78} // namespace
79
80namespace eonc {
81
82Eigen::Vector3d LBFGS::micRij(const Eigen::VectorXd &pos, int i, int j) const {
83 Eigen::Vector3d dr = pos.segment<3>(3 * i) - pos.segment<3>(3 * j);
84 m_objf->minimumImage(dr);
85 return dr;
86}
87
88bool LBFGS::usesPrecon() const {
89 const std::string &p = m_optConfig.opts.lbfgs.precon;
90 return p == "exp" || p == "c1" || p == "lindh" || p == "lindh_full" ||
91 p == "pair" || p == "pair_abs" || p == "pair_full" || p == "fischer" ||
92 p == "schlegel" || p == "swart";
93}
94
95Eigen::MatrixXd LBFGS::buildPrecon(const Eigen::VectorXd &pos) const {
96 const auto &cfg = m_optConfig.opts.lbfgs;
97 return pairhess::build(pos, cfg.precon, m_optConfig.potential, cfg.precon_A,
98 cfg.precon_mu, cfg.precon_rcut, *m_objf);
99}
100
101Eigen::VectorXd LBFGS::applyH0(const Eigen::VectorXd &q, double H0,
102 const Eigen::VectorXd &pos) const {
103 const auto &cfg = m_optConfig.opts.lbfgs;
104 if (usesPrecon() && pos.size() >= 6 && pos.size() % 3 == 0) {
105 const Eigen::MatrixXd P = buildPrecon(pos);
106 // PartialPivLU, not LDLT: manylinux Eigen + C++20 rewrites
107 // Array==Scalar in LDLT::unblocked as a non-bool comparison.
108 Eigen::PartialPivLU<Eigen::MatrixXd> lu(P);
109 const Eigen::VectorXd z = lu.solve(q);
110 if (z.allFinite()) {
111 return z;
112 }
113 QUILL_LOG_DEBUG(m_log, "[LBFGS] Packwood P failed to factor, using H0 I");
114 }
115 return H0 * q;
116}
117
118Eigen::VectorXd LBFGS::hessianStep(double a_maxMove,
119 const Eigen::VectorXd &a_f) {
120 Eigen::VectorXd r = m_objf->getPositions();
121 Eigen::VectorXd f = a_f;
122 if (!usesPrecon()) {
124 m_optConfig.opts.lbfgs.inverse_curvature * f, a_maxMove);
125 }
126 maybeProjectRigid(f, r, m_optConfig.opts.lbfgs.project_rigid);
127 const Eigen::VectorXd g = -f;
128 Eigen::MatrixXd H = buildPrecon(r);
129 Eigen::VectorXd d;
130 if (m_optConfig.opts.lbfgs.step == "rfo") {
131 // Banerjee, Adams, Simons, Shepard, J. Phys. Chem. 1985.
132 // Baker, J. Comput. Chem. 1986. Lowest mode of the augmented Hessian.
133 const int n = static_cast<int>(H.rows());
134 Eigen::MatrixXd A = Eigen::MatrixXd::Zero(n + 1, n + 1);
135 A.topLeftCorner(n, n) = H;
136 A.col(n).head(n) = g;
137 A.row(n).head(n) = g.transpose();
138 Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> es(A);
139 if (es.info() != Eigen::Success) {
140 return eonc::geometry::maxAtomMotionAppliedV(f, a_maxMove);
141 }
142 const Eigen::VectorXd v = es.eigenvectors().col(0);
143 if (std::abs(v(n)) < 1.0e-14) {
144 return eonc::geometry::maxAtomMotionAppliedV(f, a_maxMove);
145 }
146 d = v.head(n) / v(n);
147 } else {
148 // Regularized Newton: H + mu I with mu = max(0, eps - lambda_min).
149 Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> es(H);
150 if (es.info() != Eigen::Success) {
151 return eonc::geometry::maxAtomMotionAppliedV(f, a_maxMove);
152 }
153 Eigen::VectorXd ev = es.eigenvalues();
154 const double lmin = ev.minCoeff();
155 const double mu = (lmin < 1.0e-8) ? (1.0e-8 - lmin) : 0.0;
156 ev.array() += mu;
157 d = -es.eigenvectors() *
158 (es.eigenvectors().transpose() * g).cwiseQuotient(ev);
159 }
160 maybeProjectRigid(d, r, m_optConfig.opts.lbfgs.project_rigid);
161 return eonc::geometry::maxAtomMotionAppliedV(d, a_maxMove);
162}
163
164Eigen::VectorXd LBFGS::getStep(double a_maxMove, const Eigen::VectorXd &a_f) {
165 const std::string &step = m_optConfig.opts.lbfgs.step;
166 if (step == "newton" || step == "rfo") {
167 return hessianStep(a_maxMove, a_f);
168 }
169 double H0 = m_optConfig.opts.lbfgs.inverse_curvature;
170 Eigen::VectorXd r = m_objf->getPositions();
171 Eigen::VectorXd f = a_f;
172 maybeProjectRigid(f, r, m_optConfig.opts.lbfgs.project_rigid);
173
174 if (m_iteration > 0) {
175 Eigen::VectorXd dr = m_objf->difference(r, m_rPrev);
176 const Eigen::VectorXd y = m_fPrev - f;
177 const double sy = eonc::exact::dot(dr, y);
178 const double yy = eonc::exact::squaredNorm(y);
179 const double ss = eonc::exact::squaredNorm(dr);
180 const double C = eonc::safemath::safe_div(yy, sy, -1.0);
181 if (C < 0) {
182 QUILL_LOG_DEBUG(
183 m_log, "[LBFGS] Negative curvature: {:.4f} eV/A^2 take max move step",
184 C);
185 if (m_optConfig.opts.lbfgs.curvature == "reset") {
186 reset();
187 }
188 return eonc::geometry::maxAtomMotionAppliedV(1000 * f, a_maxMove);
189 }
190
191 if (m_optConfig.opts.lbfgs.auto_scale && sy > 0.0 && yy > 0.0) {
192 const double g_syyy = sy / yy;
193 const double g_sssy = ss / sy;
194 const std::string &h0 = m_optConfig.opts.lbfgs.h0;
195 if (h0 == "ss_sy") {
196 H0 = g_sssy;
197 } else if (h0 == "adaptive") {
198 H0 = std::min(g_syyy, g_sssy);
199 } else {
200 H0 = g_syyy;
201 }
202 if (m_optConfig.opts.lbfgs.max_inverse_curvature > 0.0) {
203 H0 = std::min(H0, m_optConfig.opts.lbfgs.max_inverse_curvature);
204 }
205 QUILL_LOG_DEBUG(m_log, "[LBFGS] H0={:.4e} ({} scale)", H0, h0);
206 }
207 }
208
209 const std::optional<double> known =
210 m_iteration == 0 && m_optConfig.opts.lbfgs.auto_scale
211 ? m_objf->knownCurvature()
212 : std::nullopt;
213 // A reported curvature is the classic dimer's |C_tau|. The translated
214 // force on a negative mode is F - 2 (F·tau) tau. When |C_tau| is steep
215 // and H0 = 1/|C_tau| already asks for more than max_move, the cap
216 // chooses the length and the first step leaves the basin r2549 finds
217 // with one finite-difference probe of F_eff. A milder mode keeps the
218 // capped |C_tau| step. Improved dimer and Lanczos report nothing, so
219 // they take that probe on every first step.
220 constexpr double kSteepMode = 30.0;
221 bool knownFits = false;
222 if (known && std::isfinite(*known) && *known > 0.0) {
223 double hKnown = 1.0 / *known;
224 if (m_optConfig.opts.lbfgs.max_inverse_curvature > 0.0) {
225 hKnown = std::min(hKnown, m_optConfig.opts.lbfgs.max_inverse_curvature);
226 }
227 const double knownMotion = eonc::geometry::maxAtomMotionV(hKnown * f);
228 if (knownMotion < a_maxMove || *known < kSteepMode) {
229 H0 = hKnown;
230 knownFits = true;
231 QUILL_LOG_DEBUG(m_log, "[LBFGS] H0={:.4e} (known curvature)", H0);
232 }
233 }
234 if (!knownFits && m_iteration == 0 && m_optConfig.opts.lbfgs.auto_scale &&
235 m_objf->supportsFiniteDifferenceCurvature()) {
236 const Eigen::VectorXd fhat = eonc::exact::normalized(a_f);
237 m_objf->setPositions(mulAdd(r, m_optConfig.finiteDifference, fhat));
238 Eigen::VectorXd dg = m_objf->getGradient(true) + a_f;
239 const double C = eonc::exact::dot(dg, fhat) / m_optConfig.finiteDifference;
240 H0 = eonc::safemath::safe_recip(C, -1.0);
241 m_objf->setPositions(r);
242 if (H0 < 0) {
243 QUILL_LOG_WARNING(m_log,
244 "[LBFGS] Negative curvature calculated via FD: {:.4e} "
245 "eV/A^2, take max move step",
246 C);
247 reset();
248 return eonc::geometry::maxAtomMotionAppliedV(1000 * a_f, a_maxMove);
249 }
250 QUILL_LOG_DEBUG(m_log, "[LBFGS] Curvature calculated via FD: {:.4e} eV/A^2",
251 C);
252 }
253
254 const auto idx =
255 twoLoopIndex(static_cast<int>(m_s.size()),
256 static_cast<int>(m_optConfig.opts.lbfgs.extra_updates));
257 const int loopmax = static_cast<int>(idx.size());
258 std::vector<double> a(static_cast<size_t>(loopmax));
259
260 Eigen::VectorXd q = -f;
261
262 for (int k = loopmax - 1; k >= 0; k--) {
263 const int i = idx[static_cast<size_t>(k)];
264 a[static_cast<size_t>(k)] =
265 m_rho[static_cast<size_t>(i)] *
266 eonc::exact::orderedDot(m_s[static_cast<size_t>(i)], q);
267 mulAddInto(q, -a[static_cast<size_t>(k)], m_y[static_cast<size_t>(i)]);
268 }
269
270 Eigen::VectorXd z = applyH0(q, H0, r);
271
272 for (int k = 0; k < loopmax; k++) {
273 const int i = idx[static_cast<size_t>(k)];
274 const double b = m_rho[static_cast<size_t>(i)] *
275 eonc::exact::orderedDot(m_y[static_cast<size_t>(i)], z);
276 mulAddInto(z, a[static_cast<size_t>(k)] - b, m_s[static_cast<size_t>(i)]);
277 }
278
279 Eigen::VectorXd d = -z;
280
281 double distance = eonc::geometry::maxAtomMotionV(d);
282 if (distance >= a_maxMove && m_optConfig.opts.lbfgs.distance_reset) {
283 QUILL_LOG_DEBUG(m_log,
284 "[LBFGS] reset memory, proposed step too large: {:.4f}",
285 distance);
286 reset();
287 return eonc::geometry::maxAtomMotionAppliedV(H0 * f, a_maxMove);
288 }
289
290 double vd =
292 if (vd > 1.0)
293 vd = 1.0;
294 if (vd < -1.0)
295 vd = -1.0;
296 double angle = eonc::safemath::safe_acos(vd) * (180.0 / eonc::helpers::pi);
297 if (angle > 90.0 && m_optConfig.opts.lbfgs.angle_reset) {
298 QUILL_LOG_DEBUG(m_log,
299 "[LBFGS] reset memory, angle between LBFGS angle and "
300 "force too large: {:.4f}",
301 angle);
302 reset();
303 return eonc::geometry::maxAtomMotionAppliedV(H0 * f, a_maxMove);
304 }
305
306 maybeProjectRigid(d, r, m_optConfig.opts.lbfgs.project_rigid);
307 return eonc::geometry::maxAtomMotionAppliedV(d, a_maxMove);
308}
309
311 m_s.clear();
312 m_y.clear();
313 m_rho.clear();
314 m_eHist.clear();
315}
316
317int LBFGS::update(const Eigen::VectorXd &a_r1, const Eigen::VectorXd &a_r0,
318 const Eigen::VectorXd &a_f1, const Eigen::VectorXd &a_f0,
319 double a_e1, double a_e0) {
320 Eigen::VectorXd s0 = m_objf->difference(a_r1, a_r0);
321
322 // y0 is the change in the gradient, not the force
323 Eigen::VectorXd y0 = a_f0 - a_f1;
324 double sy = eonc::exact::dot(s0, y0);
325 const auto &cfg = m_optConfig.opts.lbfgs;
326 const std::string &curv = cfg.curvature;
327 const double H0 = std::max(cfg.inverse_curvature, 1.0e-16);
328 const double ss = eonc::exact::squaredNorm(s0);
329
330 if (cfg.secant == "zhangxu" && m_objf->gradientMatchesEnergy() &&
331 ss > kLbfgsEps) {
332 // Zhang, Deng, Chen, JOTA 102, 147 (1999); Zhang and Xu, JOTA 2001.
333 // t = 6(f_k - f_{k+1}) + 3(g_k + g_{k+1})·s, ŷ = y + (t - y·s)/||s||^2 s.
334 // Forces f = -g, so (g_k + g_{k+1})·s = -(f0 + f1)·s.
335 const double t =
336 6.0 * (a_e0 - a_e1) - 3.0 * eonc::exact::dot(a_f0 + a_f1, s0);
337 const double theta = t - sy;
338 mulAddInto(y0, theta / ss, s0);
339 sy = eonc::exact::dot(s0, y0);
340 QUILL_LOG_DEBUG(m_log, "[LBFGS] Zhang-Xu θ={:.4e} s·ŷ={:.4e}", theta, sy);
341 }
342
343 double sBs = ss / H0;
344 if (usesPrecon() && a_r1.size() >= 6 && a_r1.size() % 3 == 0) {
345 const Eigen::MatrixXd P = buildPrecon(a_r1);
346 sBs = eonc::exact::dot(s0, P * s0);
347 }
348
349 const double gnorm = eonc::exact::norm(a_f1);
350 const double sn = std::sqrt(ss);
351 const double yn = eonc::exact::norm(y0);
352
353 if (curv == "cautious") {
354 // Li and Fukushima, J. Comput. Appl. Math. 129, 15 (2001).
355 const double thresh =
356 cfg.cautious_eps * ss *
357 std::pow(std::max(gnorm, 1.0e-30), cfg.cautious_alpha);
358 if (sy < thresh) {
359 QUILL_LOG_DEBUG(m_log, "[LBFGS] Li-Fukushima skip, s·y={:.4e} < {:.4e}",
360 sy, thresh);
361 return 0;
362 }
363 } else if (std::abs(sy) < kLbfgsEps || (curv != "reset" && sy < 0.2 * sBs)) {
364 if (curv == "skip") {
365 QUILL_LOG_DEBUG(m_log, "[LBFGS] skip pair, s·y={:.4e}", sy);
366 return 0;
367 }
368 if (curv == "damped" && sBs > sy) {
369 const double theta = std::clamp(0.8 * sBs / (sBs - sy), 0.0, 1.0);
370 const Eigen::VectorXd B0s =
371 (usesPrecon() && a_r1.size() >= 6 && a_r1.size() % 3 == 0)
372 ? Eigen::VectorXd(buildPrecon(a_r1) * s0)
373 : Eigen::VectorXd(s0 / H0);
374 y0 = theta * y0 + (1.0 - theta) * B0s;
375 sy = eonc::exact::dot(s0, y0);
376 QUILL_LOG_DEBUG(m_log, "[LBFGS] Powell damp θ={:.3f} s·ŷ={:.4e}", theta,
377 sy);
378 } else if (std::abs(sy) < kLbfgsEps) {
379 QUILL_LOG_WARNING(m_log,
380 "[LBFGS] s0.y0 too small ({:.4e}), resetting memory",
381 eonc::exact::dot(s0, y0));
382 reset();
383 return 0;
384 }
385 }
386
387 if (!std::isfinite(sy)) {
388 QUILL_LOG_WARNING(m_log, "[LBFGS] non-finite s·y, resetting memory");
389 reset();
390 return 0;
391 }
392 // Relative overlap skip belongs to skip/damped/cautious. reset
393 // keeps the ASE rule: only |s·y| < kLbfgsEps drops the pair.
394 if (curv != "reset" && sy <= 1.0e-8 * sn * yn) {
395 QUILL_LOG_DEBUG(m_log, "[LBFGS] overlap skip, s·y={:.4e}", sy);
396 return 0;
397 }
398
399 m_rho.push_back(eonc::safemath::safe_recip(sy, 0.0));
400 m_s.push_back(std::move(s0));
401 m_y.push_back(std::move(y0));
402
403 if (static_cast<int>(m_s.size()) > m_memory) {
404 m_s.pop_front();
405 m_y.pop_front();
406 m_rho.pop_front();
407 }
408 return 0;
409}
410
411int LBFGS::step(double a_maxMove) {
412 int status = 0;
413 Eigen::VectorXd r = m_objf->getPositions();
414 Eigen::VectorXd f = -m_objf->getGradient();
415 const std::string &accept = m_optConfig.opts.lbfgs.accept;
416 const bool energyTests = m_objf->gradientMatchesEnergy();
417 const bool energyRequested = accept == "energy" || accept == "nonmonotone" ||
418 m_optConfig.opts.lbfgs.secant == "zhangxu";
419 if (energyRequested && !energyTests && m_iteration == 0) {
420 EONC_LOG_WARNING("[LBFGS] energy tests off (lbfgs_accept = {}, "
421 "lbfgs_secant = {}): a band or saddle objective's "
422 "gradient is not its energy gradient",
423 accept, m_optConfig.opts.lbfgs.secant);
424 }
425 const bool energyAccept =
426 energyTests && (accept == "energy" || accept == "nonmonotone");
427 const bool needE =
428 energyAccept ||
429 (energyTests && m_optConfig.opts.lbfgs.secant == "zhangxu");
430 const double e0 = needE ? m_objf->getEnergy() : 0.0;
431
432 if (m_iteration > 0) {
433 status = update(r, m_rPrev, f, m_fPrev, e0, m_ePrev);
434 }
435 if (status < 0)
436 return -1;
437
438 Eigen::VectorXd dr = getStep(a_maxMove, f);
439 if (energyAccept) {
440 constexpr double max_erise = 1.0e-8;
441 double ref = e0;
442 if (accept == "nonmonotone" && !m_eHist.empty()) {
443 ref = *std::max_element(m_eHist.begin(), m_eHist.end());
444 }
445 double alpha = 1.0;
446 bool accepted = false;
447 double e_acc = e0;
448 for (int dec = 0; dec < 10; ++dec) {
449 m_objf->setPositions(mulAdd(r, alpha, dr));
450 e_acc = m_objf->getEnergy();
451 if (e_acc - ref <= max_erise) {
452 accepted = true;
453 break;
454 }
455 alpha *= 0.5;
456 }
457 if (!accepted) {
458 m_objf->setPositions(r);
459 reset();
460 m_objf->setPositions(
461 r + eonc::geometry::maxAtomMotionAppliedV(0.1 * f, a_maxMove));
462 e_acc = m_objf->getEnergy();
463 }
464 m_eHist.push_back(e_acc);
465 if (m_eHist.size() > 5) {
466 m_eHist.pop_front();
467 }
468 } else {
469 m_objf->setPositions(r + dr);
470 }
471
472 m_rPrev = r;
473 m_fPrev = f;
474 m_ePrev = e0;
475
476 m_iteration++;
477
478 return m_objf->isConverged() ? 1 : 0;
479}
480
481int LBFGS::run(size_t a_maxSteps, double a_maxMove) {
482 int status;
483 while (!m_objf->isConverged() && m_iteration < a_maxSteps) {
484 status = step(a_maxMove);
485 if (status < 0)
486 return -1;
487 }
488 return m_objf->isConverged() ? 1 : 0;
489}
490
491} // namespace eonc
Eigen::Matrix< double, Eigen::Dynamic, 3, eOnStorageOrder > AtomMatrix
Definition Eigen.h:37
#define EONC_LOG_WARNING(...)
Definition EonLogger.h:322
std::deque< double > m_rho
Definition LBFGS.h:65
int update(const Eigen::VectorXd &a_r1, const Eigen::VectorXd &a_r0, const Eigen::VectorXd &a_f1, const Eigen::VectorXd &a_f0, double a_e1, double a_e0)
Definition LBFGS.cpp:317
Eigen::VectorXd m_fPrev
Definition LBFGS.h:69
int m_iteration
Definition LBFGS.h:60
std::deque< Eigen::VectorXd > m_y
Definition LBFGS.h:64
std::deque< double > m_eHist
Definition LBFGS.h:66
std::deque< Eigen::VectorXd > m_s
Definition LBFGS.h:63
int step(double a_maxMove) override
Definition LBFGS.cpp:411
Eigen::VectorXd m_rPrev
Definition LBFGS.h:68
bool usesPrecon() const
Definition LBFGS.cpp:88
int run(size_t a_maxIterations, double a_maxMove) override
Definition LBFGS.cpp:481
int m_memory
Definition LBFGS.h:61
double m_ePrev
Definition LBFGS.h:70
Eigen::VectorXd hessianStep(double a_maxMove, const Eigen::VectorXd &a_f)
Definition LBFGS.cpp:118
void reset()
Definition LBFGS.cpp:310
eonc::log::FileScoped m_log
Definition LBFGS.h:71
Eigen::MatrixXd buildPrecon(const Eigen::VectorXd &pos) const
Definition LBFGS.cpp:95
Eigen::VectorXd applyH0(const Eigen::VectorXd &q, double H0, const Eigen::VectorXd &pos) const
Definition LBFGS.cpp:101
Eigen::VectorXd getStep(double a_maxMove, const Eigen::VectorXd &a_f)
Definition LBFGS.cpp:164
Eigen::Vector3d micRij(const Eigen::VectorXd &pos, int i, int j) const
Definition LBFGS.cpp:82
const OptimizerConfig m_optConfig
Definition Optimizer.h:73
std::shared_ptr< ObjectiveFunction > m_objf
Definition Optimizer.h:74
double squaredNorm(const Eigen::MatrixBase< A > &x)
A::PlainObject normalized(const Eigen::MatrixBase< A > &x, double min_norm=1e-300)
x / norm(x), or zero below min_norm (the safemath convention).
double norm(const Eigen::MatrixBase< A > &x)
sqrt of the correctly rounded squared norm; IEEE sqrt is itself correctly rounded,...
double dot(const double *x, const double *y, std::size_t n)
Round-to-nearest-even value of the exact sum of x[i] * y[i].
double orderedDot(const double *x, const double *y, std::size_t n)
A fixed summation tree: four lanes (term i goes to lane i mod 4, each lane a left-to-right sum of rou...
void projectOutRotTrans(Eigen::VectorXd &step, const AtomMatrix &positions)
VectorXd maxAtomMotionAppliedV(const VectorXd v1, double maxMotion)
double maxAtomMotionV(const VectorXd v1)
constexpr double pi
Eigen::MatrixXd build(const Eigen::VectorXd &pos, const std::string &kind, PotType pot, double A, double mu, double rcut_in, ObjectiveFunction &objf)
Analytic pair or model Hessian.
constexpr double safe_div(double num, double denom, double fallback=0.0)
Definition SafeMath.h:21
double safe_acos(double x)
Definition SafeMath.h:34
constexpr double safe_recip(double x, double fallback=0.0)
Definition SafeMath.h:29
RAII resource manager for the ARTn C library with global synchronization.