NumCpp  2.9.0
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
43 {
44  namespace roots
45  {
46  //================================================================================
47  // Class Description:
50  class Newton : public Iteration
51  {
52  public:
53  //============================================================================
54  // Method Description:
61  Newton(const double epsilon, std::function<double(double)> f, std::function<double(double)> fPrime) noexcept
62  :
63  Iteration(epsilon),
64  f_(std::move(f)),
65  fPrime_(std::move(fPrime))
66  {
67  }
68 
69  //============================================================================
70  // Method Description:
78  Newton(const double epsilon,
79  const uint32 maxNumIterations,
80  std::function<double(double)> f,
81  std::function<double(double)> fPrime) noexcept :
82  Iteration(epsilon, maxNumIterations),
83  f_(std::move(f)),
84  fPrime_(std::move(fPrime))
85  {
86  }
87 
88  //============================================================================
89  // Method Description:
92  ~Newton() noexcept override = default;
93 
94  //============================================================================
95  // Method Description:
101  double solve(double x)
102  {
104 
105  double fx = f_(x);
106  double fxPrime = fPrime_(x);
107 
108  while (std::fabs(fx) >= epsilon_)
109  {
110  x = calculateX(x, fx, fxPrime);
111 
112  fx = f_(x);
113  fxPrime = fPrime_(x);
114 
116  }
117 
118  return x;
119  }
120 
121  private:
122  //============================================================================
123  const std::function<double(double)> f_;
124  const std::function<double(double)> fPrime_;
125 
126  //============================================================================
127  // Method Description:
135  static double calculateX(double x, double fx, double fxPrime) noexcept
136  {
137  return x - fx / fxPrime;
138  }
139  };
140  } // namespace roots
141 } // namespace nc
ABC for iteration classes to derive from.
Definition: Iteration.hpp:48
Iteration(double epsilon) noexcept
Definition: Iteration.hpp:56
const double epsilon_
Definition: Iteration.hpp:118
void resetNumberOfIterations() noexcept
Definition: Iteration.hpp:96
void incrementNumberOfIterations()
Definition: Iteration.hpp:107
Definition: Newton.hpp:51
~Newton() noexcept override=default
double solve(double x)
Definition: Newton.hpp:101
Newton(const double epsilon, std::function< double(double)> f, std::function< double(double)> fPrime) noexcept
Definition: Newton.hpp:61
Newton(const double epsilon, const uint32 maxNumIterations, std::function< double(double)> f, std::function< double(double)> fPrime) noexcept
Definition: Newton.hpp:78
dtype f(GeneratorType &generator, dtype inDofN, dtype inDofD)
Definition: f.hpp:58
Definition: Coordinate.hpp:45
std::uint32_t uint32
Definition: Types.hpp:40