13#ifndef OPTIMIST_ROOTFINDER_HALLEY_HH
14#define OPTIMIST_ROOTFINDER_HALLEY_HH
39 template <
typename Real>
72 template <
typename FunctionLambda,
typename FirstDerivativeLambda,
typename SecondDerivativeLambda>
73 bool solve_impl(FunctionLambda && function, FirstDerivativeLambda && first_derivative,
74 SecondDerivativeLambda && second_derivative, Real x_ini, Real & x_sol)
76 #define CMD "Optimist::RootFinder::Halley::solve(...): "
86 Real residuals, step_norm;
87 Real x_old, x_new, function_old, function_new, step_old, step_new;
88 Real first_derivative_old, second_derivative_old;
92 success = this->
evaluate_function(std::forward<FunctionLambda>(function), x_old, function_old);
94 CMD "function evaluation failed at the initial point.");
105 success = this->
evaluate_first_derivative(std::forward<FirstDerivativeLambda>(first_derivative), x_old, first_derivative_old);
107 CMD "first derivative evaluation failed at iteration " << this->
m_iterations <<
".");
108 success = this->
evaluate_second_derivative(std::forward<SecondDerivativeLambda>(second_derivative), x_old, second_derivative_old);
110 CMD "second derivative evaluation failed at iteration " << this->
m_iterations <<
".");
113 if (std::abs(first_derivative_old) < EPSILON_LOW) {
115 first_derivative_old = (first_derivative_old > 0.0) ? EPSILON_LOW : -EPSILON_LOW;
117 if (std::abs(second_derivative_old) < EPSILON_LOW) {
119 second_derivative_old = (second_derivative_old > 0.0) ? EPSILON_LOW : -EPSILON_LOW;
121 step_old = -(function_old / first_derivative_old) * (1.0 - (function_old * second_derivative_old) /
122 (first_derivative_old * first_derivative_old));
126 residuals = std::abs(function_old);
127 step_norm = std::abs(step_old);
129 if (residuals < tolerance_residuals || step_norm < tolerance_step_norm) {
136 damped = this->
damp(std::forward<FunctionLambda>(function), x_old, function_old, step_old, x_new, function_new, step_new);
140 x_new = x_old + step_old;
141 success = this->
evaluate_function(std::forward<FunctionLambda>(function), x_new, function_new);
143 CMD "function evaluation failed at iteration " << this->
m_iterations <<
".");
148 function_old = function_new;
#define OPTIMIST_WARNING(MSG)
Definition Optimist.hh:53
#define OPTIMIST_ASSERT_WARNING(COND, MSG)
Definition Optimist.hh:61
#define OPTIMIST_BASIC_CONSTANTS(Real)
Definition Optimist.hh:71
#define OPTIMIST_ASSERT(COND, MSG)
Definition Optimist.hh:44
static constexpr bool requires_function
Definition Halley.hh:43
static constexpr bool requires_first_derivative
Definition Halley.hh:44
std::string name_impl() const
Definition Halley.hh:58
Halley()
Definition Halley.hh:52
bool solve_impl(FunctionLambda &&function, FirstDerivativeLambda &&first_derivative, SecondDerivativeLambda &&second_derivative, Real x_ini, Real &x_sol)
Definition Halley.hh:73
static constexpr bool requires_second_derivative
Definition Halley.hh:45
RootFinder()
Definition RootFinder.hh:70
bool m_damped
Definition SolverBase.hh:93
bool evaluate_second_derivative(SecondDerivativeLambda &&function, InputType const &x, SecondDerivativeType &out)
Definition SolverBase.hh:801
void info(Real residuals, std::string const ¬es="-")
Definition SolverBase.hh:924
Integer m_iterations
Definition SolverBase.hh:84
bool damp(FunctionLambda &&function, InputType const &x_old, InputType const &function_old, InputType const &step_old, InputType &x_new, InputType &function_new, InputType &step_new)
Definition SolverBase.hh:828
Integer m_max_iterations
Definition SolverBase.hh:85
void store_trace(InputType const &x)
Definition SolverBase.hh:813
void header()
Definition SolverBase.hh:870
void reset()
Definition SolverBase.hh:748
bool m_verbose
Definition SolverBase.hh:92
bool m_converged
Definition SolverBase.hh:98
void bottom()
Definition SolverBase.hh:900
bool evaluate_function(FunctionLambda &&function, InputType const &x, OutputType &out)
Definition SolverBase.hh:768
Real m_tolerance
Definition SolverBase.hh:91
bool evaluate_first_derivative(FirstDerivativeLambda &&function, InputType const &x, FirstDerivativeType &out)
Definition SolverBase.hh:785
Namespace for the Optimist library.
Definition Optimist.hh:88