UtilsLite
Utilities for C++ programming
Loading...
Searching...
No Matches
Utils_AlgoHNewton.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_HNewton.hh
22//
23
24#pragma once
25
26#ifndef UTILS_ALGO_HNEWTON_dot_HH
27#define UTILS_ALGO_HNEWTON_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
47 /*
48 // _ _ _ _ _
49 // | | | | \ | | _____ _| |_ ___ _ __
50 // | |_| | \| |/ _ \ \ /\ / / __/ _ \| '_ \
51 // | _ | |\ | __/\ V V /| || (_) | | | |
52 // |_| |_|_| \_|\___| \_/\_/ \__\___/|_| |_|
53 */
54
80 template <typename Real>
82 public:
89 virtual Real eval( Real x ) const = 0;
96 virtual Real D( Real x ) const = 0;
97 };
98
99 #ifndef DOXYGEN_SHOULD_SKIP_THIS
100 template <typename Real, typename PFUN, typename PFUN_D>
101 class AlgoHNewton_fun : public AlgoHNewton_base_fun<Real> {
102 PFUN m_fun;
103 PFUN_D m_fun_D;
104 public:
105 explicit AlgoHNewton_fun( PFUN f, PFUN_D Df ) : m_fun(f), m_fun_D(Df) {}
106 Real eval( Real x ) const override { return m_fun(x); };
107 Real D ( Real x ) const override { return m_fun_D(x); };
108 };
109 #endif
110
209 template <typename Real>
211
212 using Integer = int;
213
214 Real m_tolerance{pow(machine_eps<Real>(),Real(2./3.))};
215 bool m_converged{false};
216
217 Real m_a{0}, m_fa{0};
218 Real m_b{0}, m_fb{0};
219 Real m_c{0}, m_fc{0};
220 Real m_d{0}, m_fd{0};
221 Real m_ba{0};
222 Real m_kappa{0.05};
223
224 AlgoHNewton_base_fun<Real> const * m_function{nullptr};
225
226 Integer m_max_iteration{200}; // max number of iterations
227
228 mutable Integer m_iteration_count{0}; // explore iteration counter
229 mutable Integer m_fun_evaluation_count{0};
230 mutable Integer m_fun_D_evaluation_count{0};
231
232 Real evaluate ( Real x ) const { ++m_fun_evaluation_count; return m_function->eval(x); };
233 Real evaluate_D( Real x ) const { ++m_fun_D_evaluation_count; return m_function->D(x); };
234
235 Real eval();
236 Real eval( Real a, Real b );
237 Real eval( Real a, Real b, Real amin, Real bmax );
238
239 void set_tolerance( Real tol );
240
241 Real p_zero2() const;
242 Real invp_zero2() const;
243 Real invp_zero3() const;
244
245 public:
246
247 AlgoHNewton() = default;
248 ~AlgoHNewton() = default;
249
258 Real
259 eval( Real a, Real b, AlgoHNewton_base_fun<Real> const * fun ) {
260 m_function = fun;
261 return this->eval( a, b );
262 }
263
274 Real
275 eval( Real a, Real b, Real amin, Real bmax, AlgoHNewton_base_fun<Real> const * fun ) {
276 m_function = fun;
277 return this->eval( a, b, amin, bmax );
278 }
279
285 void set_max_iterations( Integer mit );
286
290 Integer used_iter() const { return m_iteration_count; }
291
295 Integer num_fun_eval() const { return m_fun_evaluation_count; }
296
300 Integer num_fun_D_eval() const { return m_fun_D_evaluation_count; }
301
305 Real tolerance() const { return m_tolerance; }
306
310 bool converged() const { return m_converged; }
311
312 Real a() const { return m_a; }
313 Real b() const { return m_b; }
314 Real fa() const { return m_fa; }
315 Real fb() const { return m_fb; }
316
317 };
318
319 #ifndef UTILS_OS_WINDOWS
320 extern template class AlgoHNewton<float>;
321 extern template class AlgoHNewton<double>;
322 #endif
323
325
326}
327
328#endif
329
330//
331// EOF: Utils_AlgoHNewton.hh
332//
Abstract base class for defining mathematical functions used in the zero search algorithm.
Definition Utils_AlgoHNewton.hh:81
virtual Real eval(Real x) const =0
virtual Real D(Real x) const =0
Real b() const
Definition Utils_AlgoHNewton.hh:313
Integer num_fun_D_eval() const
Definition Utils_AlgoHNewton.hh:300
AlgoHNewton()=default
void set_max_iterations(Integer mit)
Definition Utils_AlgoHNewton.cc:49
Real eval(Real a, Real b, Real amin, Real bmax, AlgoHNewton_base_fun< Real > const *fun)
Definition Utils_AlgoHNewton.hh:275
Real fa() const
Definition Utils_AlgoHNewton.hh:314
Real tolerance() const
Definition Utils_AlgoHNewton.hh:305
Integer used_iter() const
Definition Utils_AlgoHNewton.hh:290
Real eval(Real a, Real b, AlgoHNewton_base_fun< Real > const *fun)
Definition Utils_AlgoHNewton.hh:259
Integer num_fun_eval() const
Definition Utils_AlgoHNewton.hh:295
bool converged() const
Definition Utils_AlgoHNewton.hh:310
~AlgoHNewton()=default
Real fb() const
Definition Utils_AlgoHNewton.hh:315
Real a() const
Definition Utils_AlgoHNewton.hh:312
Definition SystemUtils.cc:39
T machine_eps()