Optimist  0.0.0
A C++ library for optimization
Loading...
Searching...
No Matches
Newton.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#pragma once
12
13#ifndef OPTIMIST_SCALAR_ROOT_FINDER_NEWTON_HH
14#define OPTIMIST_SCALAR_ROOT_FINDER_NEWTON_HH
15
17
18namespace Optimist
19{
20 namespace ScalarRootFinder
21 {
22
23 /*\
24 | _ _ _
25 | | \ | | _____ _| |_ ___ _ __
26 | | \| |/ _ \ \ /\ / / __/ _ \| '_ \
27 | | |\ | __/\ V V /| || (_) | | | |
28 | |_| \_|\___| \_/\_/ \__\___/|_| |_|
29 |
30 \*/
31
39 template <typename Real>
40 class Newton : public ScalarRootFinder<Real, Newton<Real>>
41 {
42 public:
43 static constexpr bool requires_function{true};
44 static constexpr bool requires_first_derivative{true};
45 static constexpr bool requires_second_derivative{false};
46
48
49 // Function types
53
57 Newton() {}
58
63 std::string name_impl() const {return "Newton";}
64
73 bool solve_impl(FunctionWrapper function, FirstDerivativeWrapper first_derivative, Real x_ini,
74 Real & x_sol)
75 {
76 #define CMD "Optimist::ScalarRootFinder::Newton::solve(...): "
77
78 // Setup internal variables
79 this->reset();
80
81 // Print header
82 if (this->m_verbose) {this->header();}
83
84 // Initialize variables
85 bool damped;
86 Real residuals, step_norm;
87 Real x_old, x_new, function_old, function_new, step_old, step_new;
88 Real first_derivative_old;
89
90 // Set initial iteration
91 x_old = x_ini;
92 this->evaluate_function(function, x_old, function_old);
93
94 // Algorithm iterations
95 Real tolerance_residuals{this->m_tolerance};
96 Real tolerance_step_norm{this->m_tolerance * this->m_tolerance};
97 for (this->m_iterations = static_cast<Integer>(1); this->m_iterations < this->m_max_iterations; ++this->m_iterations)
98 {
99 // Store trace
100 this->store_trace(x_old);
101
102 // Evaluate first derivative
103 this->evaluate_first_derivative(first_derivative, x_old, first_derivative_old);
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(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 this->evaluate_function(function, x_new, function_new);
130 }
131
132 // Update internal variables
133 x_old = x_new;
134 function_old = function_new;
135 step_old = step_new;
136 }
137
138 // Print bottom
139 if (this->m_verbose) {this->bottom();}
140
141 // Convergence data
142 x_sol = x_old;
143 return this->m_converged;
144
145 #undef CMD
146 }
147
148 }; // class Newton
149
150 } // namespace ScalarRootFinder
151
152} // namespace Optimist
153
154#endif // OPTIMIST_SCALAR_ROOT_FINDER_NEWTON_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 ScalarRootFinder< Real, Newton< Real > >::FunctionWrapper FunctionWrapper
Definition Newton.hh:50
static constexpr bool requires_second_derivative
Definition Newton.hh:45
static constexpr bool requires_first_derivative
Definition Newton.hh:44
typename ScalarRootFinder< Real, Newton< Real > >::FirstDerivativeWrapper FirstDerivativeWrapper
Definition Newton.hh:51
static constexpr bool requires_function
Definition Newton.hh:43
Newton()
Definition Newton.hh:57
typename ScalarRootFinder< Real, Newton< Real > >::SecondDerivativeWrapper SecondDerivativeWrapper
Definition Newton.hh:52
bool solve_impl(FunctionWrapper function, FirstDerivativeWrapper first_derivative, Real x_ini, Real &x_sol)
Definition Newton.hh:73
std::string name_impl() const
Definition Newton.hh:63
void store_trace(const InputType &x)
Definition Solver.hh:750
void header()
Definition Solver.hh:799
void reset()
Definition Solver.hh:693
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 Solver.hh:763
void evaluate_first_derivative(FirstDerivativeWrapper function, const InputType &x, FirstDerivativeType &out)
Definition Solver.hh:724
void bottom()
Definition Solver.hh:829
void evaluate_function(FunctionWrapper function, const InputType &x, OutputType &out)
Definition Solver.hh:710
void info(Real residuals, std::string const &notes="-")
Definition Solver.hh:853
Integer m_iterations
Definition Solver.hh:85
Integer m_max_iterations
Definition Solver.hh:86
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