Optimist  0.0.0
A C++ library for optimization
Loading...
Searching...
No Matches
Varona.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
2 * Copyright (c) 2025, 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_VARONA_HH
14#define OPTIMIST_ROOTFINDER_VARONA_HH
15
17
18namespace Optimist {
19
20 namespace RootFinder {
21
27 template <typename Scalar>
28 requires TypeTrait<Scalar>::IsScalar
29 class Varona : public OpenMethod<Scalar, Varona<Scalar>> {
30 public:
31 static constexpr bool RequiresFunction{true};
32 static constexpr bool RequiresFirstDerivative{true};
33 static constexpr bool RequiresSecondDerivative{false};
34
36
37
40 using Method = enum class Method : Integer {
41 ORDER_4 = 4,
42 ORDER_8 = 8,
43 ORDER_16 = 16,
44 ORDER_32 = 32
45 };
46
47 private:
48 Method m_method{Method::ORDER_4};
49
50 public:
54 Varona() {}
55
60 constexpr std::string name_impl() const {
61 return "Varona";
62 }
63
68 Method method() const {
69 return this->m_method;
70 }
71
76 void method(const Method t_method) {
77 this->m_method = t_method;
78 }
79
84 this->m_method = Method::ORDER_4;
85 }
86
91 this->m_method = Method::ORDER_8;
92 }
93
98 this->m_method = Method::ORDER_16;
99 }
100
105 this->m_method = Method::ORDER_32;
106 }
107
120 template <typename FunctionLambda>
121 Scalar step_impl(FunctionLambda &function,
122 const Scalar x,
123 const Scalar f,
124 const Scalar df,
125 [[maybe_unused]] const Scalar ddf) {
126#define CMD "Optimist::RootFinder::Varona::step_impl(...): "
127
128 const Integer order{static_cast<Integer>(this->m_method)};
129 const Scalar tolerance{this->m_tolerance};
130 Scalar step{-f / df};
131
132 // Evaluate the function at x + step, returning false if converged
133 auto next = [&](Scalar &value) -> bool {
134 OPTIMIST_ASSERT(this->evaluate_function(function, x + step, value),
135 CMD "function evaluation failed.");
136 return std::abs(value) >= tolerance;
137 };
138
139 Scalar f_y, f_z, f_w, f_h;
140 if (!next(f_y)) {
141 return step;
142 }
143 const Scalar t{f_y / f};
144 if (std::abs(t) > 0.5) {
145 return step;
146 }
147 step -= Q(t) * f_y / df;
148 if (order < 8 || !next(f_z)) {
149 return step;
150 }
151 const Scalar s{f_z / f_y};
152 step -= W(t, s) * f_z / df;
153 if (order < 16 || !next(f_w)) {
154 return step;
155 }
156 const Scalar u{f_w / f_z};
157 step -= H(t, s, u) * f_w / df;
158 if (order < 32 || !next(f_h)) {
159 return step;
160 }
161 const Scalar w{f_h / f_w};
162 return step - J(t, s, u, w) * f_h / df;
163
164#undef CMD
165 }
166
167 private:
173 static Scalar Q(Scalar t) {
174 return 1.0 + 2.0 * t;
175 }
176
183 static Scalar W(Scalar t, Scalar s) {
184 return t * t * (1.0 - 4.0 * t) + (4.0 * s + 2.0) * t + s + 1.0;
185 }
186
194 static Scalar H(Scalar t, Scalar s, Scalar u) {
195 Scalar t1{t * t};
196 Scalar t2{t1 * t1};
197 Scalar t8{s * s};
198 Scalar t17{s * t8};
199 Scalar t23{static_cast<Scalar>(2.0) * u};
200 return ((8.0 * u + 6.0 * t2 + 4.0) * s -
201 (6.0 * t8 + 4.0 * (s + u + 1.0)) * t1 + 2.0 * t8 - 4.0 * t17 +
202 t23 + 2.0) *
203 t +
204 t1 * (t8 + s + u + 1.0) + (1.0 - 3.0 * t2 + t23) * s + u - t17 +
205 1.0;
206 }
207
216 static Scalar J(Scalar t, Scalar s, Scalar u, Scalar v) {
217 Scalar t1{s * s};
218 Scalar t2{t1 * t1};
219 Scalar t17{t * t};
220 Scalar t22{u * u};
221 Scalar t32{t17 * t17};
222 Scalar t34{t * t32};
223 Scalar t37{t * t17};
224 Scalar t46{static_cast<Scalar>(1.0) + v};
225 Scalar t65{u + static_cast<Scalar>(1.0) + v};
226 Scalar t76{static_cast<Scalar>(-2.0 * t22 + u + 4.0 * v + 2.0) * u};
227 return (-1.0 + 2.0 * t) * (2.0 + 5.0 * t) * u * t * t2 +
228 (4.0 * t + 1.0) * u * s * t2 +
229 (u * t22 - 2.0 * u * v - u - v - 1.0) *
230 (4.0 * t17 + 3.0 * t + 1.0) * (-1.0 + t) -
231 8.0 *
232 (t22 * (t17 / 2.0 - 1.0 / 4.0) +
233 u * (t17 * t32 - 5.0 / 8.0 * t34 - 3.0 / 4.0 * t32 +
234 3.0 / 8.0 * t37 + 3.0 / 4.0 * t17 - t / 8.0 -
235 1.0 / 4.0) +
236 3.0 / 4.0 * t46 * (t + 1.0 / 2.0) * (t - 2.0 / 3.0)) *
237 t * t1 +
238 4.0 *
239 (t22 * (-3.0 / 2.0 * t - 1.0 / 4.0) +
240 u * (t34 - t32 - 3.0 / 2.0 * t37 + t17 / 4.0 - t -
241 1.0 / 4.0) -
242 t46 * (t + 1.0 / 4.0)) *
243 s * t1 +
244 (1.0 + v + t65 * t17 - 4.0 * t65 * t37 - 3.0 * t65 * t32 +
245 6.0 * t65 * t34 + t76 + 4.0 * (1.0 + v + t76) * t) *
246 s;
247 }
248
249 }; // class Varona
250
251 } // namespace RootFinder
252
253} // namespace Optimist
254
255#endif // OPTIMIST_ROOTFINDER_VARONA_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
typename TypeTrait< Scalar >::Scalar Scalar
Definition RootFinder.hh:47
void enable_16th_order_method()
Definition Varona.hh:97
static Scalar W(Scalar t, Scalar s)
Definition Varona.hh:183
Method method() const
Definition Varona.hh:68
static Scalar J(Scalar t, Scalar s, Scalar u, Scalar v)
Definition Varona.hh:216
void enable_4th_order_method()
Definition Varona.hh:83
void enable_8th_order_method()
Definition Varona.hh:90
static Scalar H(Scalar t, Scalar s, Scalar u)
Definition Varona.hh:194
void enable_32th_order_method()
Definition Varona.hh:104
Method m_method
Definition Varona.hh:48
constexpr std::string name_impl() const
Definition Varona.hh:60
static constexpr bool RequiresFirstDerivative
Definition Varona.hh:32
Varona()
Definition Varona.hh:54
Scalar step_impl(FunctionLambda &function, const Scalar x, const Scalar f, const Scalar df, const Scalar ddf)
Definition Varona.hh:121
static constexpr bool RequiresSecondDerivative
Definition Varona.hh:33
static Scalar Q(Scalar t)
Definition Varona.hh:173
void method(const Method t_method)
Definition Varona.hh:76
static constexpr bool RequiresFunction
Definition Varona.hh:31
enum class Method :Integer { ORDER_4=4, ORDER_8=8, ORDER_16=16, ORDER_32=32 } Method
Definition Varona.hh:40
bool evaluate_function(FunctionLambda &&function, const Scalar &x, Scalar &out)
Definition SolverBase.hh:1026
Scalar m_tolerance
Definition SolverBase.hh:108
Scalar tolerance() const
Definition SolverBase.hh:458
Namespace for the Optimist library.
Definition Optimist.hh:90
OPTIMIST_DEFAULT_INTEGER_TYPE Integer
The Integer type as used for the API.
Definition Optimist.hh:98