NumCpp  2.1.0
A C++ implementation of the Python Numpy library
Secant.hpp
Go to the documentation of this file.
1 #pragma once
34 
36 #include "NumCpp/Core/Types.hpp"
38 
39 #include <cmath>
40 #include <functional>
41 #include <utility>
42 
43 namespace nc
44 {
45  namespace roots
46  {
47  //================================================================================
48  // Class Description:
51  class Secant : public Iteration
52  {
53  public:
54  //============================================================================
55  // Method Description:
61  Secant(const double epsilon,
62  std::function<double(double)> f) noexcept :
63  Iteration(epsilon),
64  f_(std::move(f))
65  {}
66 
67  //============================================================================
68  // Method Description:
75  Secant(const double epsilon,
76  const uint32 maxNumIterations,
77  std::function<double(double)> f) noexcept :
78  Iteration(epsilon, maxNumIterations),
79  f_(std::move(f))
80  {}
81 
82  //============================================================================
83  // Method Description:
86  ~Secant() override = default;
87 
88  //============================================================================
89  // Method Description:
96  double solve(double a, double b)
97  {
99 
100  if (f_(a) > f_(b))
101  {
102  std::swap(a, b);
103  }
104 
105  double x = b;
106  double lastX = a;
107  double fx = f_(b);
108  double lastFx = f_(a);
109 
110  while (std::fabs(fx) >= epsilon_)
111  {
112  const double x_tmp = calculateX(x, lastX, fx, lastFx);
113 
114  lastFx = fx;
115  lastX = x;
116  x = x_tmp;
117 
118  fx = f_(x);
119 
121  }
122 
123  return x;
124  }
125 
126  private:
127  //============================================================================
128  const std::function<double(double)> f_;
129 
130  //============================================================================
131  // Method Description:
140  static double calculateX(double x, double lastX, double fx, double lastFx) noexcept
141  {
142  const double functionDifference = fx - lastFx;
143  return x - fx * (x - lastX) / functionDifference;
144  }
145  };
146  } // namespace roots
147 } // namespace nc
nc::roots::Iteration::Iteration
Iteration(double epsilon) noexcept
Definition: Iteration.hpp:56
nc::roots::Secant::~Secant
~Secant() override=default
nc::roots::Iteration
ABC for iteration classes to derive from.
Definition: Iteration.hpp:47
nc::roots::Secant
Definition: Secant.hpp:51
nc::roots::Iteration::incrementNumberOfIterations
void incrementNumberOfIterations()
Definition: Iteration.hpp:105
nc::roots::Secant::Secant
Secant(const double epsilon, const uint32 maxNumIterations, std::function< double(double)> f) noexcept
Definition: Secant.hpp:75
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:41
nc::roots::Iteration::epsilon_
const double epsilon_
Definition: Iteration.hpp:115
nc::roots::Secant::solve
double solve(double a, double b)
Definition: Secant.hpp:96
nc::roots::Secant::Secant
Secant(const double epsilon, std::function< double(double)> f) noexcept
Definition: Secant.hpp:61
Iteration.hpp
nc::roots::Iteration::resetNumberOfIterations
void resetNumberOfIterations() noexcept
Definition: Iteration.hpp:94
nc
Definition: Coordinate.hpp:45
nc::swap
void swap(NdArray< dtype > &inArray1, NdArray< dtype > &inArray2) noexcept
Definition: swap.hpp:43
DtypeInfo.hpp
Types.hpp
nc::random::f
dtype f(dtype inDofN, dtype inDofD)
Definition: f.hpp:58