11#ifndef OPTIMIST_ROOTFINDER_NEWTONRAPHSON_HH
12#define OPTIMIST_ROOTFINDER_NEWTONRAPHSON_HH
36 template <
typename Scalar>
55 return "NewtonRaphson";
69 template <
typename FunctionLambda,
typename FirstDerivativeLambda>
71 FirstDerivativeLambda &&first_derivative,
74#define CMD "Optimist::RootFinder::NewtonRaphson::solve(...): "
86 Scalar residuals, step_norm;
87 Scalar x_old, x_new, function_old, function_new, step_old, step_new;
88 Scalar first_derivative_old;
97 CMD "function evaluation failed at the initial point.");
107 std::forward<FirstDerivativeLambda>(first_derivative),
109 first_derivative_old);
111 CMD "first derivative evaluation failed at iteration "
115 if (std::abs(first_derivative_old) < NewtonRaphson::SQRT_EPSILON) {
117 "close-to-singular first derivative detected.");
118 first_derivative_old = (first_derivative_old > 0)
119 ? NewtonRaphson::SQRT_EPSILON
120 : -NewtonRaphson::SQRT_EPSILON;
122 step_old = -function_old / first_derivative_old;
124 std::isfinite(step_old),
128 residuals = std::abs(function_old);
129 step_norm = std::abs(step_old);
131 this->
info(residuals);
133 if (residuals < tolerance_residuals ||
134 step_norm < tolerance_step_norm) {
141 damped = this->
damp(std::forward<FunctionLambda>(function),
151 x_new = x_old + step_old;
157 CMD "function evaluation failed at iteration "
163 function_old = function_new;
#define OPTIMIST_WARNING(MSG)
Definition Optimist.hh:55
#define OPTIMIST_BASIC_CONSTANTS(Scalar)
Definition Optimist.hh:69
#define OPTIMIST_ASSERT_WARNING(COND, MSG)
Definition Optimist.hh:61
#define OPTIMIST_ASSERT(COND, MSG)
Definition Optimist.hh:47
static constexpr bool RequiresFunction
Definition NewtonRaphson.hh:39
constexpr std::string name_impl() const
Definition NewtonRaphson.hh:54
static constexpr bool RequiresSecondDerivative
Definition NewtonRaphson.hh:41
NewtonRaphson()
Definition NewtonRaphson.hh:48
bool solve_impl(FunctionLambda &&function, FirstDerivativeLambda &&first_derivative, Scalar x_ini, Scalar &x_sol)
Definition NewtonRaphson.hh:70
static constexpr bool RequiresFirstDerivative
Definition NewtonRaphson.hh:40
typename TypeTrait< Scalar >::Scalar Scalar
Definition RootFinder.hh:53
RootFinder()
Definition RootFinder.hh:66
bool damp(FunctionLambda &&function, const Scalar &x_old, const Scalar &function_old, const Scalar &step_old, Scalar &x_new, Scalar &function_new, Scalar &step_new)
Definition SolverBase.hh:1109
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
bool evaluate_first_derivative(FirstDerivativeLambda &&function, const Scalar &x, FirstDerivative &out)
Definition SolverBase.hh:1061
bool m_damped
Definition SolverBase.hh:131
Integer m_max_iterations
Definition SolverBase.hh:121
Scalar m_tolerance
Definition SolverBase.hh:128
void header()
Definition SolverBase.hh:1168
void reset_counters()
Definition SolverBase.hh:1022
Integer m_iterations
Definition SolverBase.hh:120
bool m_verbose
Definition SolverBase.hh:130
bool m_converged
Definition SolverBase.hh:136
void bottom()
Definition SolverBase.hh:1204
Namespace for multi-dimensional root-finding algorithms.
Definition RootFinder.hh:23
Namespace for the Optimist library.
Definition Optimist.hh:89