Optimist  0.0.0
A C++ library for optimization
Loading...
Searching...
No Matches
OpenMethod.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
2 * Copyright (c) 2026, Davide Stocco and Enrico Bertolazzi. *
3 * *
4 * The Optimist project is distributed under the BSD 2-Clause License. *
5 * *
6 * Davide Stocco Enrico Bertolazzi *
7 * University of Trento University of Trento *
8 * davide.stocco@unitn.it enrico.bertolazzi@unitn.it *
9\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
10
11#pragma once
12
13#ifndef OPTIMIST_ROOTFINDER_OPENMETHOD_HH
14#define OPTIMIST_ROOTFINDER_OPENMETHOD_HH
15
17
18namespace Optimist {
19
20 namespace RootFinder {
21
44 template <typename Scalar, typename DerivedSolver>
45 requires TypeTrait<Scalar>::IsScalar
46 class OpenMethod : public RootFinder<Scalar, DerivedSolver> {
47 public:
49
50
58 template <typename FunctionLambda, typename FirstDerivativeLambda>
59 bool solve_impl(FunctionLambda &&function,
60 FirstDerivativeLambda &&first_derivative,
61 Scalar x_ini,
62 Scalar &x_sol) {
63 return this->iterate(function, first_derivative, nullptr, x_ini, x_sol);
64 }
65
75 template <typename FunctionLambda,
76 typename FirstDerivativeLambda,
77 typename SecondDerivativeLambda>
78 bool solve_impl(FunctionLambda &&function,
79 FirstDerivativeLambda &&first_derivative,
80 SecondDerivativeLambda &&second_derivative,
81 Scalar x_ini,
82 Scalar &x_sol) {
83 return this->iterate(function,
84 first_derivative,
85 &second_derivative,
86 x_ini,
87 x_sol);
88 }
89
90 private:
95 template <typename FunctionLambda,
96 typename FirstDerivativeLambda,
97 typename SecondDerivativePointer>
98 bool iterate(FunctionLambda &function,
99 FirstDerivativeLambda &first_derivative,
100 SecondDerivativePointer second_derivative,
101 const Scalar x_ini,
102 Scalar &x_sol) {
103#define CMD "Optimist::RootFinder::" << this->name() << "::solve(...): "
104
105 this->reset_counters();
106 if (this->m_verbose) {
107 this->header();
108 }
109
110 Scalar x{x_ini}, f{0}, df{0}, ddf{0}, step{0};
111 Scalar x_new{0}, f_new{0}, step_new{0};
112 OPTIMIST_ASSERT(this->evaluate_function(function, x, f),
113 CMD "function evaluation failed at the initial point.");
114
115 const Scalar tolerance_residuals{this->m_tolerance};
116 const Scalar tolerance_step{this->m_tolerance * this->m_tolerance};
117 for (this->m_iterations = 1;
118 this->m_iterations < this->m_max_iterations;
119 ++this->m_iterations) {
120 // Derivatives
121 OPTIMIST_ASSERT(this->evaluate_first_derivative(first_derivative,
122 x,
123 df),
124 CMD "first derivative evaluation failed at iteration "
125 << this->m_iterations << ".");
126 if constexpr (!std::is_same_v<SecondDerivativePointer,
127 std::nullptr_t>) {
129 this->evaluate_second_derivative(*second_derivative, x, ddf),
130 CMD "second derivative evaluation failed at iteration "
131 << this->m_iterations << ".");
132 }
133
134 // Step, keeping the first derivative away from zero
135 std::string notes{"-"};
136 if (std::abs(df) < SQRT_EPSILON) {
137 df = std::copysign(SQRT_EPSILON, df);
138 notes = "Singular derivative";
139 }
140 step = static_cast<DerivedSolver *>(this)->step_impl(function,
141 x,
142 f,
143 df,
144 ddf);
145 OPTIMIST_ASSERT(std::isfinite(step),
146 CMD "step " << this->m_iterations
147 << " is not finite.");
148
149 // Convergence
150 if (this->m_verbose) {
151 this->info(std::abs(f), notes);
152 }
153 if (std::abs(f) < tolerance_residuals ||
154 std::abs(step) < tolerance_step) {
155 this->m_converged = true;
156 break;
157 }
158
159 // Update
160 if (this->m_damped) {
161 this->damp(function, x, f, step, x_new, f_new, step_new);
162 } else {
163 x_new = x + step;
164 OPTIMIST_ASSERT(this->evaluate_function(function, x_new, f_new),
165 CMD "function evaluation failed at iteration "
166 << this->m_iterations << ".");
167 }
168 x = x_new;
169 f = f_new;
170 }
171
172 if (this->m_verbose) {
173 this->bottom();
174 }
175 x_sol = x;
176 return this->m_converged;
177
178#undef CMD
179 }
180
181 }; // class OpenMethod
182
183 } // namespace RootFinder
184
185} // namespace Optimist
186
187#endif // OPTIMIST_ROOTFINDER_OPENMETHOD_HH
#define CMD
#define OPTIMIST_BASIC_CONSTANTS(Scalar)
Definition Optimist.hh:70
#define OPTIMIST_ASSERT(COND, MSG)
Definition Optimist.hh:48
Base class for the open (i.e., non-bracketing) one-dimensional root finders of Newton type.
Definition OpenMethod.hh:46
bool solve_impl(FunctionLambda &&function, FirstDerivativeLambda &&first_derivative, Scalar x_ini, Scalar &x_sol)
Definition OpenMethod.hh:59
bool solve_impl(FunctionLambda &&function, FirstDerivativeLambda &&first_derivative, SecondDerivativeLambda &&second_derivative, Scalar x_ini, Scalar &x_sol)
Definition OpenMethod.hh:78
bool iterate(FunctionLambda &function, FirstDerivativeLambda &first_derivative, SecondDerivativePointer second_derivative, const Scalar x_ini, Scalar &x_sol)
Definition OpenMethod.hh:98
typename TypeTrait< Scalar >::Scalar Scalar
Definition RootFinder.hh:47
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:1095
void info(Scalar residuals, const std::string &notes="-")
Definition SolverBase.hh:1218
bool evaluate_function(FunctionLambda &&function, const Scalar &x, Scalar &out)
Definition SolverBase.hh:1026
bool evaluate_first_derivative(FirstDerivativeLambda &&function, const Scalar &x, FirstDerivative &out)
Definition SolverBase.hh:1047
bool evaluate_second_derivative(SecondDerivativeLambda &&function, const Scalar &x, SecondDerivative &out)
Definition SolverBase.hh:1068
Integer m_max_iterations
Definition SolverBase.hh:101
Scalar m_tolerance
Definition SolverBase.hh:108
void header()
Definition SolverBase.hh:1154
void reset_counters()
Definition SolverBase.hh:1008
Integer m_iterations
Definition SolverBase.hh:100
bool m_converged
Definition SolverBase.hh:116
void bottom()
Definition SolverBase.hh:1191
Namespace for the Optimist library.
Definition Optimist.hh:90