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 // Function types
48 using FunctionWrapper = typename RootFinder<Real, 1, NewtonRaphson<Real>>::FunctionWrapper;
51
56
61 std::string name_impl() const {return "NewtonRaphson";}
62
71 bool solve_impl(FunctionWrapper function, FirstDerivativeWrapper first_derivative, Real x_ini,
72 Real & x_sol)
73 {
74 #define CMD "Optimist::RootFinder::NewtonRaphson::solve(...): "
75
76 // Setup internal variables
77 this->reset();
78
79 // Print header
80 if (this->m_verbose) {this->header();}
81
82 // Initialize variables
83 bool damped;
84 Real residuals, step_norm;
85 Real x_old, x_new, function_old, function_new, step_old, step_new;
86 Real first_derivative_old;
87
88 // Set initial iteration
89 x_old = x_ini;
90 this->evaluate_function(function, x_old, function_old);
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 = static_cast<Integer>(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 this->evaluate_first_derivative(first_derivative, x_old, first_derivative_old);
102
103 // Calculate step
104 if (std::abs(first_derivative_old) < EPSILON_LOW) {
105 OPTIMIST_WARNING( CMD "singular first derivative detected.");
106 first_derivative_old = (first_derivative_old > 0.0) ? EPSILON_LOW : -EPSILON_LOW;
107 }
108 step_old = -function_old/first_derivative_old;
109 OPTIMIST_ASSERT(std::isfinite(step_old), CMD "step " << this->m_iterations << " is not finite.");
110
111 // Check convergence
112 residuals = std::abs(function_old);
113 step_norm = std::abs(step_old);
114 if (this->m_verbose) {this->info(residuals);}
115 if (residuals < tolerance_residuals || step_norm < tolerance_step_norm) {
116 this->m_converged = true;
117 break;
118 }
119
120 if (this->m_damped) {
121 // Relax the iteration process
122 damped = this->damp(function, x_old, function_old, step_old, x_new, function_new, step_new);
123 OPTIMIST_ASSERT_WARNING(damped, CMD "damping failed.");
124 } else {
125 // Update point
126 x_new = x_old + step_old;
127 this->evaluate_function(function, x_new, function_new);
128 }
129
130 // Update internal variables
131 x_old = x_new;
132 function_old = function_new;
133 step_old = step_new;
134 }
135
136 // Print bottom
137 if (this->m_verbose) {this->bottom();}
138
139 // Convergence data
140 x_sol = x_old;
141 return this->m_converged;
142
143 #undef CMD
144 }
145
146 }; // class NewtonRaphson
147
148 } // namespace RootFinder
149
150} // namespace Optimist
151
152#endif // OPTIMIST_ROOTFINDER_NEWTONRAPHSON_HH
#define OPTIMIST_WARNING(MSG)
Definition Optimist.hh:52
#define OPTIMIST_ASSERT_WARNING(COND, MSG)
Definition Optimist.hh:60
#define OPTIMIST_BASIC_CONSTANTS(Real)
Definition Optimist.hh:70
#define OPTIMIST_ASSERT(COND, MSG)
Definition Optimist.hh:43
#define CMD
typename RootFinder< Real, 1, NewtonRaphson< Real > >::FirstDerivativeWrapper FirstDerivativeWrapper
Definition NewtonRaphson.hh:49
static constexpr bool requires_first_derivative
Definition NewtonRaphson.hh:42
static constexpr bool requires_second_derivative
Definition NewtonRaphson.hh:43
NewtonRaphson()
Definition NewtonRaphson.hh:55
std::string name_impl() const
Definition NewtonRaphson.hh:61
static constexpr bool requires_function
Definition NewtonRaphson.hh:41
typename RootFinder< Real, 1, NewtonRaphson< Real > >::FunctionWrapper FunctionWrapper
Definition NewtonRaphson.hh:48
bool solve_impl(FunctionWrapper function, FirstDerivativeWrapper first_derivative, Real x_ini, Real &x_sol)
Definition NewtonRaphson.hh:71
typename RootFinder< Real, 1, NewtonRaphson< Real > >::SecondDerivativeWrapper SecondDerivativeWrapper
Definition NewtonRaphson.hh:50
void info(Real residuals, std::string const &notes="-")
Definition SolverBase.hh:860
void evaluate_first_derivative(FirstDerivativeWrapper function, const InputType &x, FirstDerivativeType &out)
Definition SolverBase.hh:731
bool damp(FunctionWrapper 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:770
void store_trace(const InputType &x)
Definition SolverBase.hh:757
void evaluate_function(FunctionWrapper function, const InputType &x, OutputType &out)
Definition SolverBase.hh:717
Namespace for multi-dimensional root-finding algorithms.
Definition RootFinder.hh:25
Namespace for the Optimist library.
Definition Optimist.hh:87
OPTIMIST_DEFAULT_INTEGER_TYPE Integer
The Integer type as used for the API.
Definition Optimist.hh:95