Optimist  0.0.0
A C++ library for optimization
Loading...
Searching...
No Matches
NewtonRaphson.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
2 * Copyright (c) 2025, Davide Stocco, Mattia Piazza and Enrico Bertolazzi. *
3 * *
4 * The Optimist project is distributed under the BSD 2-Clause License. *
5 * *
6 * Davide Stocco Mattia Piazza Enrico Bertolazzi *
7 * University of Trento University of Trento University of Trento *
8 * davide.stocco@unitn.it mattia.piazza@unitn.it enrico.bertolazzi@unitn.it *
9\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
10
11#ifndef OPTIMIST_ROOTFINDER_NEWTONRAPHSON_HH
12#define OPTIMIST_ROOTFINDER_NEWTONRAPHSON_HH
13
15
16namespace Optimist
17{
18 namespace RootFinder
19 {
20
21 /*\
22 | _ _ _ ____ _
23 | | \ | | _____ _| |_ ___ _ __ | _ \ __ _ _ __ | |__ ___ ___ _ __
24 | | \| |/ _ \ \ /\ / / __/ _ \| '_ \| |_) / _` | '_ \| '_ \/ __|/ _ \| '_ \
25 | | |\ | __/\ V V /| || (_) | | | | _ < (_| | |_) | | | \__ \ (_) | | | |
26 | |_| \_|\___| \_/\_/ \__\___/|_| |_|_| \_\__,_| .__/|_| |_|___/\___/|_| |_|
27 | |_|
28 \*/
29
37 template <typename Real>
38 class NewtonRaphson : public RootFinder<Real, 1, NewtonRaphson<Real>>
39 {
40 public:
41 static constexpr bool requires_function{true};
42 static constexpr bool requires_first_derivative{true};
43 static constexpr bool requires_second_derivative{false};
44
46
47
51
56 std::string name_impl() const {return "NewtonRaphson";}
57
68 template <typename FunctionLambda, typename FirstDerivativeLambda>
69 bool solve_impl(FunctionLambda && function, FirstDerivativeLambda && first_derivative, Real x_ini,
70 Real & x_sol)
71 {
72 #define CMD "Optimist::RootFinder::NewtonRaphson::solve(...): "
73
74 // Setup internal variables
75 this->reset();
76
77 // Print header
78 if (this->m_verbose) {this->header();}
79
80 // Initialize variables
81 bool damped, success;
82 Real residuals, step_norm;
83 Real x_old, x_new, function_old, function_new, step_old, step_new;
84 Real first_derivative_old;
85
86 // Set initial iteration
87 x_old = x_ini;
88 success = this->evaluate_function(std::forward<FunctionLambda>(function), x_old, function_old);
90 CMD "function evaluation failed at the initial point.");
91
92 // Algorithm iterations
93 Real tolerance_residuals{this->m_tolerance};
94 Real tolerance_step_norm{this->m_tolerance * this->m_tolerance};
95 for (this->m_iterations = 1; this->m_iterations < this->m_max_iterations; ++this->m_iterations)
96 {
97 // Store trace
98 this->store_trace(x_old);
99
100 // Evaluate first derivative
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 << ".");
104
105 // Calculate step
106 if (std::abs(first_derivative_old) < EPSILON_LOW) {
107 OPTIMIST_WARNING( CMD "singular first derivative detected.");
108 first_derivative_old = (first_derivative_old > 0.0) ? EPSILON_LOW : -EPSILON_LOW;
109 }
110 step_old = -function_old/first_derivative_old;
111 OPTIMIST_ASSERT(std::isfinite(step_old), CMD "step " << this->m_iterations << " is not finite.");
112
113 // Check convergence
114 residuals = std::abs(function_old);
115 step_norm = std::abs(step_old);
116 if (this->m_verbose) {this->info(residuals);}
117 if (residuals < tolerance_residuals || step_norm < tolerance_step_norm) {
118 this->m_converged = true;
119 break;
120 }
121
122 if (this->m_damped) {
123 // Relax the iteration process
124 damped = this->damp(std::forward<FunctionLambda>(function), x_old, function_old, step_old, x_new, function_new, step_new);
125 OPTIMIST_ASSERT_WARNING(damped, CMD "damping failed.");
126 } else {
127 // Update point
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 << ".");
132 }
133
134 // Update internal variables
135 x_old = x_new;
136 function_old = function_new;
137 step_old = step_new;
138 }
139
140 // Print bottom
141 if (this->m_verbose) {this->bottom();}
142
143 // Convergence data
144 x_sol = x_old;
145 return this->m_converged;
146
147 #undef CMD
148 }
149
150 }; // class NewtonRaphson
151
152 } // namespace RootFinder
153
154} // namespace Optimist
155
156#endif // OPTIMIST_ROOTFINDER_NEWTONRAPHSON_HH
#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
#define CMD
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
void info(Real residuals, std::string const &notes="-")
Definition SolverBase.hh:924
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
void store_trace(InputType const &x)
Definition SolverBase.hh:813
bool evaluate_function(FunctionLambda &&function, InputType const &x, OutputType &out)
Definition SolverBase.hh:768
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