NumCpp  2.6.2
A Templatized Header Only C++ Implementation of the Python NumPy Library
Dekker.hpp
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include "NumCpp/Core/Types.hpp"
36 
37 #include <cmath>
38 #include <functional>
39 #include <utility>
40 
41 namespace nc
42 {
43  namespace roots
44  {
45  //================================================================================
46  // Class Description:
49  class Dekker : public Iteration
50  {
51  public:
52  //============================================================================
53  // Method Description:
59  Dekker(const double epsilon,
60  std::function<double(double)> f) noexcept :
61  Iteration(epsilon),
62  f_(std::move(f))
63  {}
64 
65  //============================================================================
66  // Method Description:
73  Dekker(const double epsilon,
74  const uint32 maxNumIterations,
75  std::function<double(double)> f) noexcept :
76  Iteration(epsilon, maxNumIterations),
77  f_(std::move(f))
78  {}
79 
80  //============================================================================
81  // Method Description:
84  ~Dekker() override = default;
85 
86  //============================================================================
87  // Method Description:
94  double solve(double a, double b)
95  {
97 
98  double fa = f_(a);
99  double fb = f_(b);
100 
101  checkAndFixAlgorithmCriteria(a, b, fa, fb);
102 
103  double lastB = a;
104  double lastFb = fa;
105 
106  while (std::fabs(fb) > epsilon_ && std::fabs(b - a) > epsilon_)
107  {
108  const double s = calculateSecant(b, fb, lastB, lastFb);
109  const double m = calculateBisection(a, b);
110 
111  lastB = b;
112 
113  b = useSecantMethod(b, s, m) ? s : m;
114 
115  lastFb = fb;
116  fb = f_(b);
117 
118  if (fa * fb > 0 && fb * lastFb < 0)
119  {
120  a = lastB;
121  }
122 
123  fa = f_(a);
124  checkAndFixAlgorithmCriteria(a, b, fa, fb);
125 
127  }
128 
129  return b;
130  }
131 
132  private:
133  //============================================================================
134  const std::function<double(double)> f_;
135 
136  //============================================================================
137  // Method Description:
145  static void checkAndFixAlgorithmCriteria(double &a, double &b, double &fa, double &fb) noexcept
146  {
147  //Algorithm works in range [a,b] if criteria f(a)*f(b) < 0 and f(a) > f(b) is fulfilled
148  if (std::fabs(fa) < std::fabs(fb))
149  {
150  std::swap(a, b);
151  std::swap(fa, fb);
152  }
153  }
154 
155  //============================================================================
156  // Method Description:
165  static double calculateSecant(double b, double fb, double lastB, double lastFb) noexcept
166  {
167  //No need to check division by 0, in this case the method returns NAN which is taken care by useSecantMethod method
168  return b - fb * (b - lastB) / (fb - lastFb);
169  }
170 
171  //============================================================================
172  // Method Description:
179  static double calculateBisection(double a, double b) noexcept
180  {
181  return 0.5 * (a + b);
182  }
183 
184  //============================================================================
185  // Method Description:
193  static bool useSecantMethod(double b, double s, double m) noexcept
194  {
195  //Value s calculated by secant method has to be between m and b
196  return (b > m && s > m && s < b) ||
197  (b < m && s > b && s < m);
198  }
199  };
200  } // namespace roots
201 } // namespace nc
Definition: Dekker.hpp:50
~Dekker() override=default
double solve(double a, double b)
Definition: Dekker.hpp:94
Dekker(const double epsilon, std::function< double(double)> f) noexcept
Definition: Dekker.hpp:59
Dekker(const double epsilon, const uint32 maxNumIterations, std::function< double(double)> f) noexcept
Definition: Dekker.hpp:73
ABC for iteration classes to derive from.
Definition: Iteration.hpp:47
Iteration(double epsilon) noexcept
Definition: Iteration.hpp:55
const double epsilon_
Definition: Iteration.hpp:114
void resetNumberOfIterations() noexcept
Definition: Iteration.hpp:93
void incrementNumberOfIterations()
Definition: Iteration.hpp:104
dtype f(dtype inDofN, dtype inDofD)
Definition: f.hpp:56
Definition: Coordinate.hpp:45
void swap(NdArray< dtype > &inArray1, NdArray< dtype > &inArray2) noexcept
Definition: swap.hpp:42
std::uint32_t uint32
Definition: Types.hpp:40