NumCpp  2.10.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
Newton.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::roots
43 {
44  //================================================================================
45  // Class Description:
48  class Newton : public Iteration
49  {
50  public:
51  //============================================================================
52  // Method Description:
59  Newton(const double epsilon, std::function<double(double)> f, std::function<double(double)> fPrime) noexcept :
60  Iteration(epsilon),
61  f_(std::move(f)),
62  fPrime_(std::move(fPrime))
63  {
64  }
65 
66  //============================================================================
67  // Method Description:
75  Newton(const double epsilon,
76  const uint32 maxNumIterations,
77  std::function<double(double)> f,
78  std::function<double(double)> fPrime) noexcept :
79  Iteration(epsilon, maxNumIterations),
80  f_(std::move(f)),
81  fPrime_(std::move(fPrime))
82  {
83  }
84 
85  //============================================================================
86  // Method Description:
89  ~Newton() noexcept override = default;
90 
91  //============================================================================
92  // Method Description:
98  double solve(double x)
99  {
101 
102  double fx = f_(x);
103  double fxPrime = fPrime_(x);
104 
105  while (std::fabs(fx) >= epsilon_)
106  {
107  x = calculateX(x, fx, fxPrime);
108 
109  fx = f_(x);
110  fxPrime = fPrime_(x);
111 
113  }
114 
115  return x;
116  }
117 
118  private:
119  //============================================================================
120  const std::function<double(double)> f_;
121  const std::function<double(double)> fPrime_;
122 
123  //============================================================================
124  // Method Description:
132  static double calculateX(double x, double fx, double fxPrime) noexcept
133  {
134  return x - fx / fxPrime;
135  }
136  };
137 } // namespace nc::roots
ABC for iteration classes to derive from.
Definition: Iteration.hpp:46
Iteration(double epsilon) noexcept
Definition: Iteration.hpp:54
const double epsilon_
Definition: Iteration.hpp:116
void resetNumberOfIterations() noexcept
Definition: Iteration.hpp:94
void incrementNumberOfIterations()
Definition: Iteration.hpp:105
Definition: Newton.hpp:49
~Newton() noexcept override=default
double solve(double x)
Definition: Newton.hpp:98
Newton(const double epsilon, std::function< double(double)> f, std::function< double(double)> fPrime) noexcept
Definition: Newton.hpp:59
Newton(const double epsilon, const uint32 maxNumIterations, std::function< double(double)> f, std::function< double(double)> fPrime) noexcept
Definition: Newton.hpp:75
dtype f(GeneratorType &generator, dtype inDofN, dtype inDofD)
Definition: f.hpp:56
Definition: Bisection.hpp:43
std::uint32_t uint32
Definition: Types.hpp:40