NumCpp  2.7.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
Newton.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 Newton : public Iteration
50  {
51  public:
52  //============================================================================
53  // Method Description:
60  Newton(const double epsilon,
61  std::function<double(double)> f,
62  std::function<double(double)> fPrime) noexcept :
63  Iteration(epsilon),
64  f_(std::move(f)),
65  fPrime_(std::move(fPrime))
66  {}
67 
68  //============================================================================
69  // Method Description:
77  Newton(const double epsilon,
78  const uint32 maxNumIterations,
79  std::function<double(double)> f,
80  std::function<double(double)> fPrime) noexcept :
81  Iteration(epsilon, maxNumIterations),
82  f_(std::move(f)),
83  fPrime_(std::move(fPrime))
84  {}
85 
86  //============================================================================
87  // Method Description:
90  ~Newton()noexcept override = default;
91 
92  //============================================================================
93  // Method Description:
99  double solve(double x)
100  {
102 
103  double fx = f_(x);
104  double fxPrime = fPrime_(x);
105 
106  while (std::fabs(fx) >= epsilon_)
107  {
108  x = calculateX(x, fx, fxPrime);
109 
110  fx = f_(x);
111  fxPrime = fPrime_(x);
112 
114  }
115 
116  return x;
117  }
118 
119  private:
120  //============================================================================
121  const std::function<double(double)> f_;
122  const std::function<double(double)> fPrime_;
123 
124  //============================================================================
125  // Method Description:
133  static double calculateX(double x, double fx, double fxPrime) noexcept
134  {
135  return x - fx / fxPrime;
136  }
137  };
138  } // namespace roots
139 } // 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: Newton.hpp:50
~Newton() noexcept override=default
double solve(double x)
Definition: Newton.hpp:99
Newton(const double epsilon, std::function< double(double)> f, std::function< double(double)> fPrime) noexcept
Definition: Newton.hpp:60
Newton(const double epsilon, const uint32 maxNumIterations, std::function< double(double)> f, std::function< double(double)> fPrime) noexcept
Definition: Newton.hpp:77
dtype f(dtype inDofN, dtype inDofD)
Definition: f.hpp:55
Definition: Coordinate.hpp:45
std::uint32_t uint32
Definition: Types.hpp:40