11#ifndef OPTIMIST_ROOTFINDER_NEWTONRAPHSON_HH
12#define OPTIMIST_ROOTFINDER_NEWTONRAPHSON_HH
37 template <
typename Real>
56 std::string
name_impl()
const {
return "NewtonRaphson";}
68 template <
typename FunctionLambda,
typename FirstDerivativeLambda>
69 bool solve_impl(FunctionLambda && function, FirstDerivativeLambda && first_derivative, Real x_ini,
72 #define CMD "Optimist::RootFinder::NewtonRaphson::solve(...): "
82 Real residuals, step_norm;
83 Real x_old, x_new, function_old, function_new, step_old, step_new;
84 Real first_derivative_old;
88 success = this->
evaluate_function(std::forward<FunctionLambda>(function), x_old, function_old);
90 CMD "function evaluation failed at the initial point.");
101 success = this->
evaluate_first_derivative(std::forward<FirstDerivativeLambda>(first_derivative), x_old, first_derivative_old);
103 CMD "first derivative evaluation failed at iteration " << this->
m_iterations <<
".");
106 if (std::abs(first_derivative_old) < EPSILON_LOW) {
108 first_derivative_old = (first_derivative_old > 0.0) ? EPSILON_LOW : -EPSILON_LOW;
110 step_old = -function_old/first_derivative_old;
114 residuals = std::abs(function_old);
115 step_norm = std::abs(step_old);
117 if (residuals < tolerance_residuals || step_norm < tolerance_step_norm) {
124 damped = this->
damp(std::forward<FunctionLambda>(function), x_old, function_old, step_old, x_new, function_new, step_new);
128 x_new = x_old + step_old;
129 success = this->
evaluate_function(std::forward<FunctionLambda>(function), x_new, function_new);
131 CMD "function evaluation failed at iteration " << this->
m_iterations <<
".");
136 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
bool solve_impl(FunctionLambda &&function, FirstDerivativeLambda &&first_derivative, Real x_ini, Real &x_sol)
Definition NewtonRaphson.hh:69
static constexpr bool requires_first_derivative
Definition NewtonRaphson.hh:42
static constexpr bool requires_second_derivative
Definition NewtonRaphson.hh:43
NewtonRaphson()
Definition NewtonRaphson.hh:50
std::string name_impl() const
Definition NewtonRaphson.hh:56
static constexpr bool requires_function
Definition NewtonRaphson.hh:41
RootFinder()
Definition RootFinder.hh:70
bool m_damped
Definition SolverBase.hh:93
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 multi-dimensional root-finding algorithms.
Definition RootFinder.hh:25
Namespace for the Optimist library.
Definition Optimist.hh:88