UtilsLite
Utilities for C++ programming
Loading...
Searching...
No Matches
Utils_Trichotomy.hh
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*\
2 | |
3 | Copyright (C) 2022 |
4 | |
5 | , __ , __ |
6 | /|/ \ /|/ \ |
7 | | __/ _ ,_ | __/ _ ,_ |
8 | | \|/ / | | | | \|/ / | | | |
9 | |(__/|__/ |_/ \_/|/|(__/|__/ |_/ \_/|/ |
10 | /| /| |
11 | \| \| |
12 | |
13 | Enrico Bertolazzi |
14 | Dipartimento di Ingegneria Industriale |
15 | Università degli Studi di Trento |
16 | email: enrico.bertolazzi@unitn.it |
17 | |
18\*--------------------------------------------------------------------------*/
19
20//
21// file: Utils_Trichotomy.hh
22//
23
24#pragma once
25
26#ifndef UTILS_TRICHOTOMY_dot_HH
27#define UTILS_TRICHOTOMY_dot_HH
28
29#include <iostream>
30#include <sstream>
31#include <string>
32#include <algorithm>
33#include <cmath>
34
35#include "Utils.hh"
36
37namespace Utils {
38
39 using std::pow;
40 using std::abs;
41
46
70 template <typename Real>
72 public:
79 virtual Real eval( Real x ) const = 0;
86 Real operator () ( Real x ) const { return this->eval(x); }
87 };
88
89 #ifndef DOXYGEN_SHOULD_SKIP_THIS
90 template <typename Real, typename PFUN>
91 class Trichotomy_fun : public Trichotomy_base_fun<Real> {
92 PFUN m_fun;
93 public:
94 explicit Trichotomy_fun( PFUN pfun ) : m_fun(pfun) {}
95 Real eval( Real x ) const override { return m_fun(x); };
96 };
97 #endif
98
129 template <typename Real>
131
132 using Integer = int;
133
134 Integer m_num_iter_done{0};
135 Integer m_num_fun_eval{0};
136 Integer m_max_iteration{100};
137 Integer m_max_fun_evaluation{1000};
138 Real m_tolerance{pow(machine_eps<Real>(),Real(2./3.))};
139
140 bool m_converged{false};
141
142 Real m_a{0}, m_fa{0};
143 Real m_x1{0}, m_f1{0};
144 Real m_x2{0}, m_f2{0};
145 Real m_x3{0}, m_f3{0};
146 Real m_x4{0}, m_f4{0};
147 Real m_x5{0}, m_f5{0};
148 Real m_b{0}, m_fb{0};
149
150 Trichotomy_base_fun<Real> * m_function{nullptr};
151
152 bool bracketing();
153 Real minimize();
154 //void set_tolerance( Real tol );
155 Real evaluate( Real x ) { ++m_num_fun_eval; return m_function->eval(x); };
156 Real eval( Real a, Real b );
157 Real search( Real x, Real delta );
158
159 public:
160
161 Trichotomy() = default;
162 ~Trichotomy() = default;
163
172 Real
173 eval( Real a, Real b, Trichotomy_base_fun<Real> * fun ) {
174 m_function = fun;
175 return this->eval( a, b );
176 }
177
186 template <typename PFUN>
187 Real
188 eval2( Real a, Real b, PFUN pfun ) {
189 Trichotomy_fun<Real,PFUN> fun( pfun );
190 m_function = &fun;
191 return this->eval(a,b);
192 }
193
202 Real
203 search( Real x, Real delta, Trichotomy_base_fun<Real> * fun ) {
204 m_function = fun;
205 return this->search( x, delta );
206 }
207
216 template <typename PFUN>
217 Real
218 search2( Real x, Real delta, PFUN pfun ) {
219 Trichotomy_fun<Real,PFUN> fun( pfun );
220 m_function = &fun;
221 return this->search( x, delta );
222 }
223
227 Integer used_iter() const { return m_num_iter_done; }
231 Integer num_fun_eval() const { return m_num_fun_eval; }
235 Real tolerance() const { return m_tolerance; }
239 bool converged() const { return m_converged; }
240
246 void set_max_iterations( Integer mit );
252 void set_max_fun_evaluation( Integer mfev );
253
254 };
255
256 #ifndef UTILS_OS_WINDOWS
257 extern template class Trichotomy<float>;
258 extern template class Trichotomy<double>;
259 #endif
260
262
263}
264
265#endif
266
267//
268// eof: Utils_Trichotomy.hh
269//
Abstract base class for defining mathematical functions used in the minimization algorithm.
Definition Utils_Trichotomy.hh:71
virtual Real eval(Real x) const =0
Real operator()(Real x) const
Definition Utils_Trichotomy.hh:86
Integer num_fun_eval() const
Definition Utils_Trichotomy.hh:231
void set_max_iterations(Integer mit)
Definition Utils_Trichotomy.cc:43
Real search2(Real x, Real delta, PFUN pfun)
Definition Utils_Trichotomy.hh:218
Real tolerance() const
Definition Utils_Trichotomy.hh:235
bool converged() const
Definition Utils_Trichotomy.hh:239
~Trichotomy()=default
Real search(Real x, Real delta, Trichotomy_base_fun< Real > *fun)
Definition Utils_Trichotomy.hh:203
void set_max_fun_evaluation(Integer mfev)
Definition Utils_Trichotomy.cc:57
Real eval2(Real a, Real b, PFUN pfun)
Definition Utils_Trichotomy.hh:188
Integer used_iter() const
Definition Utils_Trichotomy.hh:227
Trichotomy()=default
Real eval(Real a, Real b, Trichotomy_base_fun< Real > *fun)
Definition Utils_Trichotomy.hh:173
Definition SystemUtils.cc:39
T machine_eps()