UtilsLite
Utilities for C++ programming
Loading...
Searching...
No Matches
Utils_AlgoBracket.hh
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*\
2 | |
3 | Copyright (C) 2025 |
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_AlgoBracket.hh
22//
23
24#pragma once
25
26#ifndef UTILS_ALGO_BRACKET_dot_HH
27#define UTILS_ALGO_BRACKET_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 using std::sqrt;
42
47
48
49 /*
50 // ____ _ _
51 // | __ ) _ __ __ _ ___| | _____| |_
52 // | _ \| '__/ _` |/ __| |/ / _ \ __|
53 // | |_) | | | (_| | (__| < __/ |_
54 // |____/|_| \__,_|\___|_|\_\___|\__|
55 //
56 */
57
82 template <typename Real>
84 public:
91 virtual Real eval( Real x ) const = 0;
98 Real operator () ( Real x ) const { return this->eval(x); }
99 };
100
101 #ifndef DOXYGEN_SHOULD_SKIP_THIS
102 template <typename Real, typename PFUN>
103 class Bracket_fun : public Bracket_base_fun<Real> {
104 PFUN m_fun;
105 public:
106 explicit Bracket_fun( PFUN pfun ) : m_fun(pfun) {}
107 Real eval( Real x ) const override { return m_fun(x); };
108 };
109 #endif
110
273 template <typename Real>
275
276 using Method = enum class Method : unsigned {
277 BISECTION=0, ILLINOIS, CHANDRUPATLA, BRENT, RIDDER, MODIFIED_AB, ALGO748
278 };
279
280 using Integer = int;
281
282 Real m_tolerance_x{ 100*machine_eps<Real>() };
283 Real m_tolerance_f{ 100*machine_eps<Real>() };
284 bool m_converged{ false };
285
286 Real m_a{0}, m_fa{0};
287 Real m_b{0}, m_fb{0};
288 Method m_select{Method::CHANDRUPATLA};
289
290 Bracket_base_fun<Real> * m_function{nullptr};
291
292 Integer m_max_fun_evaluation{1000}; // max number of function evaluations
293 Integer m_max_iteration{200}; // max number of iterations
294
295 mutable Integer m_iteration_count{0}; // explore iteration counter
296 mutable Integer m_fun_evaluation_count{0};
297
298 Real evaluate( Real x ) { ++m_fun_evaluation_count; return m_function->eval(x); };
299
300 Real eval();
301 Real eval( Real a, Real b );
302 Real eval( Real a, Real b, Real amin, Real bmax );
303
304 Real p_zero2( Real d, Real fd ) const;
305 Real invp_zero2( Real d, Real fd ) const;
306 Real invp_zero3( Real d, Real fd, Real e, Real fe ) const;
307
308 public:
309
310 AlgoBracket() = default;
311 ~AlgoBracket() = default;
312
313 explicit
314 AlgoBracket( Real tol_x, Real tol_f )
315 : m_tolerance_x( tol_x ), m_tolerance_f( tol_f )
316 {}
317
326 Real
327 eval( Real a, Real b, Bracket_base_fun<Real> * fun ) {
328 m_function = fun;
329 return this->eval( a, b );
330 }
331
342 Real
343 eval( Real a, Real b, Real amin, Real bmax, Bracket_base_fun<Real> * fun ) {
344 m_function = fun;
345 return this->eval( a, b, amin, bmax );
346 }
347
356 template <typename PFUN>
357 Real
358 eval2( Real a, Real b, PFUN pfun ) {
359 Bracket_fun<Real,PFUN> fun( pfun );
360 m_function = &fun;
361 return this->eval( a, b );
362 }
363
374 template <typename PFUN>
375 Real
376 eval2( Real a, Real b, Real amin, Real bmax, PFUN pfun ) {
377 Bracket_fun<Real,PFUN> fun( pfun );
378 m_function = &fun;
379 return this->eval( a, b, amin, bmax );
380 }
381
392 template <typename PFUN>
393 Real
394 eval3( Real a, Real b, Real fa, Real fb, PFUN pfun ) {
395 Bracket_fun<Real,PFUN> fun( pfun );
396 m_function = &fun;
397 m_iteration_count = 0;
398 m_fun_evaluation_count = 0;
399 m_a = a; m_fa = fa;
400 m_b = b; m_fb = fb;
401 return eval();
402 }
403
409 void set_max_iterations( Integer mit );
410
416 void set_max_fun_evaluation( Integer mfev );
417
421 Integer used_iter() const { return m_iteration_count; }
422
426 Integer num_fun_eval() const { return m_fun_evaluation_count; }
427
431 Real tolerance_x() const { return m_tolerance_x; }
432
436 Real tolerance_f() const { return m_tolerance_f; }
437
441 bool converged() const { return m_converged; }
442
443 Real a() const { return m_a; }
444 Real b() const { return m_b; }
445 Real fa() const { return m_fa; }
446 Real fb() const { return m_fb; }
447
448 string algo() const {
449 switch ( m_select ) {
450 case Method::BISECTION: return "bisection"; break;
451 case Method::ILLINOIS: return "illinois"; break;
452 case Method::CHANDRUPATLA: return "Chandrupatla"; break;
453 case Method::BRENT: return "Brent"; break;
454 case Method::RIDDER: return "Ridder"; break;
455 case Method::MODIFIED_AB: return "modified_AB"; break;
456 case Method::ALGO748: return "algo748"; break;
457 }
458 return "";
459 }
460
461 void select( unsigned i_algo ) {
462 switch ( i_algo ) {
463 case 0: m_select = Method::BISECTION; break;
464 case 1: m_select = Method::ILLINOIS; break;
465 case 2: m_select = Method::CHANDRUPATLA; break;
466 case 3: m_select = Method::BRENT; break;
467 case 4: m_select = Method::RIDDER; break;
468 case 5: m_select = Method::MODIFIED_AB; break;
469 case 6: m_select = Method::ALGO748; break;
470 }
471 }
472
473 };
474
475 #ifndef UTILS_OS_WINDOWS
476 extern template class AlgoBracket<float>;
477 extern template class AlgoBracket<double>;
478 #endif
479
481
482}
483
484#endif
485
486//
487// EOF: Utils_AlgoBracket.hh
488//
void set_max_fun_evaluation(Integer mfev)
Definition Utils_AlgoBracket.cc:74
Real eval2(Real a, Real b, PFUN pfun)
Definition Utils_AlgoBracket.hh:358
Real eval2(Real a, Real b, Real amin, Real bmax, PFUN pfun)
Definition Utils_AlgoBracket.hh:376
Real tolerance_f() const
Definition Utils_AlgoBracket.hh:436
AlgoBracket(Real tol_x, Real tol_f)
Definition Utils_AlgoBracket.hh:314
Real eval(Real a, Real b, Real amin, Real bmax, Bracket_base_fun< Real > *fun)
Definition Utils_AlgoBracket.hh:343
Real a() const
Definition Utils_AlgoBracket.hh:443
Integer num_fun_eval() const
Definition Utils_AlgoBracket.hh:426
void set_max_iterations(Integer mit)
Definition Utils_AlgoBracket.cc:60
bool converged() const
Definition Utils_AlgoBracket.hh:441
Real b() const
Definition Utils_AlgoBracket.hh:444
Real fa() const
Definition Utils_AlgoBracket.hh:445
AlgoBracket()=default
Real tolerance_x() const
Definition Utils_AlgoBracket.hh:431
Real eval3(Real a, Real b, Real fa, Real fb, PFUN pfun)
Definition Utils_AlgoBracket.hh:394
Real fb() const
Definition Utils_AlgoBracket.hh:446
~AlgoBracket()=default
Integer used_iter() const
Definition Utils_AlgoBracket.hh:421
string algo() const
Definition Utils_AlgoBracket.hh:448
Real eval(Real a, Real b, Bracket_base_fun< Real > *fun)
Definition Utils_AlgoBracket.hh:327
void select(unsigned i_algo)
Definition Utils_AlgoBracket.hh:461
Abstract base class for defining mathematical functions used in the Bracket search algorithm.
Definition Utils_AlgoBracket.hh:83
Real operator()(Real x) const
Definition Utils_AlgoBracket.hh:98
virtual Real eval(Real x) const =0
Definition SystemUtils.cc:39
T machine_eps()