NumCpp  2.8.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Bisection.hpp
Go to the documentation of this file.
1 #pragma once
34 
35 #include <cmath>
36 #include <functional>
37 #include <utility>
38 
39 #include "NumCpp/Core/Types.hpp"
41 
42 namespace nc
43 {
44  namespace roots
45  {
46  //================================================================================
47  // Class Description:
50  class Bisection : public Iteration
51  {
52  public:
53  //============================================================================
54  // Method Description:
60  Bisection(const double epsilon, std::function<double(double)> f) noexcept :
61  Iteration(epsilon),
62  f_(std::move(f))
63  {
64  }
65 
66  //============================================================================
67  // Method Description:
74  Bisection(const double epsilon, const uint32 maxNumIterations, std::function<double(double)> f) noexcept :
75  Iteration(epsilon, maxNumIterations),
76  f_(std::move(f))
77  {
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:51
Bisection(const double epsilon, const uint32 maxNumIterations, std::function< double(double)> f) noexcept
Definition: Bisection.hpp:74
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:60
ABC for iteration classes to derive from.
Definition: Iteration.hpp:48
Iteration(double epsilon) noexcept
Definition: Iteration.hpp:56
const double epsilon_
Definition: Iteration.hpp:118
void resetNumberOfIterations() noexcept
Definition: Iteration.hpp:96
void incrementNumberOfIterations()
Definition: Iteration.hpp:107
dtype f(GeneratorType &generator, dtype inDofN, dtype inDofD)
Definition: f.hpp:58
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