13#ifndef OPTIMIST_ROOTFINDER_ALGO748_HH
14#define OPTIMIST_ROOTFINDER_ALGO748_HH
19 namespace RootFinder {
41 template <
typename Scalar>
74 return a != b && a != c && a != d && b != c && b != d && c != d;
91 template <
typename FunctionLambda>
93#define CMD "Optimist::RootFinder::Algo748::bracketing(...): "
100 this->
m_c = this->
m_a + hba;
114 CMD "function evaluation failed during bracketing.");
115 if (this->
m_fc == 0) {
167 (dd_0 - this->
m_fb * (ddd_0 - this->
m_fd * dddd_0))};
171 root = (this->
m_a + this->
m_b) / 2.0;
193 return this->
m_a - a_0 / a_1;
200 bool is_nonzero{
true};
202 for (
Integer i{0}; i < n && is_nonzero; ++i) {
203 pc = a_0 + (a_1 + a_2 * (c - this->
m_b)) * (c - this->
m_a);
204 pdc = a_1 + a_2 * ((2.0 * c) - (this->
m_a + this->
m_b));
205 is_nonzero = pdc != 0;
214 return this->
m_a - a_0 / a_1;
237 template <
typename FunctionLambda>
239#define CMD "Optimist::RootFinder::Algo748::find_root_impl(...): "
262 this->
m_e = QUIET_NAN;
263 this->
m_fe = QUIET_NAN;
266 while (!(std::isfinite(this->
m_fa) && std::isfinite(this->
m_fb))) {
268 this->
m_c = (this->
m_a + this->
m_b) / 2.0;
274 CMD "function evaluation failed at iteration "
297 this->
info(abs_fa < abs_fb ? abs_fa : abs_fb);
300 abs_fa < tolerance_function ||
301 abs_fb < tolerance_function;
303 return abs_fb < abs_fa ? this->
m_b : this->
m_a;
311 this->
m_c = std::abs(this->
m_fb) < std::abs(this->
m_fa)
315 if (this->
m_c < (a_min = this->
m_a + delta)) {
317 }
else if (this->
m_c > (b_max = this->
m_b - delta)) {
325 this->
bracketing(std::forward<FunctionLambda>(function));
345 this->
info(abs_fa < abs_fb ? abs_fa : abs_fb);
348 abs_fa < tolerance_function || abs_fb < tolerance_function;
350 return abs_fa < abs_fb ? this->
m_a : this->
m_b;
356 bool do_newton_quadratic{
false};
357 if (!std::isfinite(this->
m_fe)) {
358 do_newton_quadratic =
true;
363 do_newton_quadratic =
true;
366 do_newton_quadratic =
367 (this->
m_c - this->
m_a) * (this->
m_c - this->
m_b) >= 0.0;
369 if (do_newton_quadratic) {
379 this->
bracketing(std::forward<FunctionLambda>(function)) ||
383 ? std::abs(this->
m_fa)
384 : std::abs(this->
m_fb));
387 return std::abs(this->
m_fa) < std::abs(this->
m_fb) ? this->
m_a
394 do_newton_quadratic =
true;
397 do_newton_quadratic =
398 (this->
m_c - this->
m_a) * (this->
m_c - this->
m_b) >= 0.0;
400 if (do_newton_quadratic) {
408 this->
bracketing(std::forward<FunctionLambda>(function)) ||
413 this->
info(abs_fa < abs_fb ? abs_fa : abs_fb);
416 return abs_fa < abs_fb ? this->
m_a : this->
m_b;
424 if (abs_fa < abs_fb) {
432 this->
m_c = u - 4.0 * (fu / (this->
m_fb - this->
m_fa)) * hba;
433 if (std::abs(this->
m_c - u) > hba) {
434 this->
m_c = this->
m_a + hba;
441 this->
bracketing(std::forward<FunctionLambda>(function)) ||
444 return std::abs(this->
m_fa) < std::abs(this->
m_fb) ? this->
m_a
449 if ((this->
m_b - this->
m_a) < this->
m_mu * a0_b0) {
460 this->
m_c = this->
m_a + b_a / 2.0;
462 this->
bracketing(std::forward<FunctionLambda>(function)) ||
467 return std::abs(this->
m_fa) < std::abs(this->
m_fb) ? this->
m_a
#define OPTIMIST_BASIC_CONSTANTS(Scalar)
Definition Optimist.hh:69
#define OPTIMIST_ASSERT(COND, MSG)
Definition Optimist.hh:47
Scalar find_root_impl(FunctionLambda &&function)
Definition Algo748.hh:238
bool all_different(Scalar a, Scalar b, Scalar c, Scalar d) const
Definition Algo748.hh:73
static constexpr bool RequiresFunction
Definition Algo748.hh:44
Algo748()
Definition Algo748.hh:53
constexpr std::string name_impl() const
Definition Algo748.hh:59
static constexpr bool RequiresFirstDerivative
Definition Algo748.hh:45
static constexpr bool RequiresSecondDerivative
Definition Algo748.hh:46
Scalar cubic_interpolation()
Definition Algo748.hh:149
Scalar newton_quadratic(Integer n)
Definition Algo748.hh:184
bool bracketing(FunctionLambda &&function)
Definition Algo748.hh:92
Scalar m_mu
Definition Bracketing.hh:45
Scalar m_fa
Definition Bracketing.hh:48
Scalar m_fd
Definition Bracketing.hh:51
Scalar m_c
Definition Bracketing.hh:50
Scalar m_e
Definition Bracketing.hh:52
Scalar m_a
Definition Bracketing.hh:48
Scalar m_b
Definition Bracketing.hh:49
Scalar m_fc
Definition Bracketing.hh:50
Bracketing()
Definition Bracketing.hh:58
Scalar m_d
Definition Bracketing.hh:51
Scalar m_fe
Definition Bracketing.hh:52
Scalar m_interval_shink
Definition Bracketing.hh:46
Scalar m_fb
Definition Bracketing.hh:49
Scalar m_tolerance_bracketing
Definition Bracketing.hh:43
typename TypeTrait< Scalar >::Scalar Scalar
Definition RootFinder.hh:53
void info(Scalar residuals, const std::string ¬es="-")
Definition SolverBase.hh:1230
bool evaluate_function(FunctionLambda &&function, const Scalar &x, Scalar &out)
Definition SolverBase.hh:1040
Integer m_max_iterations
Definition SolverBase.hh:121
Scalar m_tolerance
Definition SolverBase.hh:128
Scalar tolerance() const
Definition SolverBase.hh:513
Integer m_iterations
Definition SolverBase.hh:120
bool m_verbose
Definition SolverBase.hh:130
bool m_converged
Definition SolverBase.hh:136
Namespace for the Optimist library.
Definition Optimist.hh:89
OPTIMIST_DEFAULT_INTEGER_TYPE Integer
The Integer type as used for the API.
Definition Optimist.hh:97