NumCpp  2.6.2
A Templatized Header Only C++ Implementation of the Python NumPy Library
Secant.hpp
Go to the documentation of this file.
1 
32 #pragma once
33 
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 Secant : public Iteration
51  {
52  public:
53  //============================================================================
54  // Method Description:
60  Secant(const double epsilon,
61  std::function<double(double)> f) noexcept :
62  Iteration(epsilon),
63  f_(std::move(f))
64  {}
65 
66  //============================================================================
67  // Method Description:
74  Secant(const double epsilon,
75  const uint32 maxNumIterations,
76  std::function<double(double)> f) noexcept :
77  Iteration(epsilon, maxNumIterations),
78  f_(std::move(f))
79  {}
80 
81  //============================================================================
82  // Method Description:
85  ~Secant() override = default;
86 
87  //============================================================================
88  // Method Description:
95  double solve(double a, double b)
96  {
98 
99  if (f_(a) > f_(b))
100  {
101  std::swap(a, b);
102  }
103 
104  double x = b;
105  double lastX = a;
106  double fx = f_(b);
107  double lastFx = f_(a);
108 
109  while (std::fabs(fx) >= epsilon_)
110  {
111  const double x_tmp = calculateX(x, lastX, fx, lastFx);
112 
113  lastFx = fx;
114  lastX = x;
115  x = x_tmp;
116 
117  fx = f_(x);
118 
120  }
121 
122  return x;
123  }
124 
125  private:
126  //============================================================================
127  const std::function<double(double)> f_;
128 
129  //============================================================================
130  // Method Description:
139  static double calculateX(double x, double lastX, double fx, double lastFx) noexcept
140  {
141  const double functionDifference = fx - lastFx;
142  return x - fx * (x - lastX) / functionDifference;
143  }
144  };
145  } // namespace roots
146 } // namespace nc
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
Definition: Secant.hpp:51
double solve(double a, double b)
Definition: Secant.hpp:95
~Secant() override=default
Secant(const double epsilon, const uint32 maxNumIterations, std::function< double(double)> f) noexcept
Definition: Secant.hpp:74
Secant(const double epsilon, std::function< double(double)> f) noexcept
Definition: Secant.hpp:60
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