NumCpp  1.0
A C++ implementation of the Python Numpy library
Bisection.hpp
Go to the documentation of this file.
1 #pragma once
34 
35 #include "NumCpp/Core/Types.hpp"
37 
38 #include <cmath>
39 #include <functional>
40 #include <utility>
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,
61  const std::function<double(double)>& f) noexcept :
62  Iteration(epsilon),
63  f_(f)
64  {}
65 
66  //============================================================================
67  // Method Description:
74  Bisection(const double epsilon,
75  const uint32 maxNumIterations,
76  const std::function<double(double)>& f) noexcept :
77  Iteration(epsilon, maxNumIterations),
78  f_(f)
79  {}
80 
81  //============================================================================
82  // Method Description:
85  ~Bisection() = default;
86 
87  //============================================================================
88  // Method Description:
95  double solve(double a, double b)
96  {
98  checkAndFixAlgorithmCriteria(a, b);
99 
100  double x = 0.5 * (a + b);
101  double fx = f_(x);
102 
103  while (std::fabs(fx) >= epsilon_)
104  {
105  x = calculateX(x, a, b, fx);
106  fx = f_(x);
107 
109  }
110 
111  return x;
112  }
113 
114  private:
115  //============================================================================
116  const std::function<double(double)> f_;
117 
118  //============================================================================
119  // Method Description:
125  void checkAndFixAlgorithmCriteria(double &a, double &b) const noexcept
126  {
127  //Algorithm works in range [a,b] if criteria f(a)*f(b) < 0 and f(a) > f(b) is fulfilled
128  if (f_(a) < f_(b))
129  {
130  std::swap(a, b);
131  }
132  }
133 
134  //============================================================================
135  // Method Description:
144  double calculateX(double x, double &a, double &b, double fx) noexcept
145  {
146  if (fx < 0)
147  {
148  b = x;
149  }
150  else
151  {
152  a = x;
153  }
154 
155  return 0.5 * (a + b);
156  }
157  };
158  }
159 }
nc::roots::Iteration::Iteration
Iteration(double epsilon) noexcept
Definition: Iteration.hpp:56
nc::roots::Iteration
ABC for iteration classes to derive from.
Definition: Iteration.hpp:47
nc::roots::Bisection::Bisection
Bisection(const double epsilon, const uint32 maxNumIterations, const std::function< double(double)> &f) noexcept
Definition: Bisection.hpp:74
nc::roots::Iteration::incrementNumberOfIterations
void incrementNumberOfIterations()
Definition: Iteration.hpp:105
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:41
nc::roots::Bisection
Definition: Bisection.hpp:50
nc::roots::Iteration::epsilon_
const double epsilon_
Definition: Iteration.hpp:115
Iteration.hpp
nc::roots::Iteration::resetNumberOfIterations
void resetNumberOfIterations() noexcept
Definition: Iteration.hpp:94
nc::roots::Bisection::~Bisection
~Bisection()=default
nc
Definition: Coordinate.hpp:45
nc::swap
void swap(NdArray< dtype > &inArray1, NdArray< dtype > &inArray2) noexcept
Definition: swap.hpp:43
nc::roots::Bisection::Bisection
Bisection(const double epsilon, const std::function< double(double)> &f) noexcept
Definition: Bisection.hpp:60
Types.hpp
nc::random::f
dtype f(dtype inDofN, dtype inDofD)
Definition: f.hpp:58
nc::roots::Bisection::solve
double solve(double a, double b)
Definition: Bisection.hpp:95