NumCpp  2.7.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Bisection.hpp
Go to the documentation of this file.
1 #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 Bisection : public Iteration
50  {
51  public:
52  //============================================================================
53  // Method Description:
59  Bisection(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  Bisection(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  ~Bisection() override = default;
85 
86  //============================================================================
87  // Method Description:
94  double solve(double a, double b)
95  {
97  checkAndFixAlgorithmCriteria(a, b);
98 
99  double x = 0.5 * (a + b);
100  double fx = f_(x);
101 
102  while (std::fabs(fx) >= epsilon_)
103  {
104  x = calculateX(x, a, b, fx);
105  fx = f_(x);
106 
108  }
109 
110  return x;
111  }
112 
113  private:
114  //============================================================================
115  const std::function<double(double)> f_;
116 
117  //============================================================================
118  // Method Description:
124  void checkAndFixAlgorithmCriteria(double &a, double &b) const noexcept
125  {
126  //Algorithm works in range [a,b] if criteria f(a)*f(b) < 0 and f(a) > f(b) is fulfilled
127  if (f_(a) < f_(b))
128  {
129  std::swap(a, b);
130  }
131  }
132 
133  //============================================================================
134  // Method Description:
143  static double calculateX(double x, double &a, double &b, double fx) noexcept
144  {
145  if (fx < 0)
146  {
147  b = x;
148  }
149  else
150  {
151  a = x;
152  }
153 
154  return 0.5 * (a + b);
155  }
156  };
157  } // namespace roots
158 } // namespace nc
Definition: Bisection.hpp:50
Bisection(const double epsilon, const uint32 maxNumIterations, std::function< double(double)> f) noexcept
Definition: Bisection.hpp:73
double solve(double a, double b)
Definition: Bisection.hpp:94
~Bisection() override=default
Bisection(const double epsilon, std::function< double(double)> f) noexcept
Definition: Bisection.hpp:59
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:55
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