NumCpp  2.1.0
A C++ implementation of the Python Numpy library
Newton.hpp
Go to the documentation of this file.
1 #pragma once
34 
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 Newton : public Iteration
51  {
52  public:
53  //============================================================================
54  // Method Description:
61  Newton(const double epsilon,
62  std::function<double(double)> f,
63  std::function<double(double)> fPrime) noexcept :
64  Iteration(epsilon),
65  f_(std::move(f)),
66  fPrime_(std::move(fPrime))
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  // Method Description:
91  ~Newton()noexcept override = default;
92 
93  //============================================================================
94  // Method Description:
100  double solve(double x)
101  {
103 
104  double fx = f_(x);
105  double fxPrime = fPrime_(x);
106 
107  while (std::fabs(fx) >= epsilon_)
108  {
109  x = calculateX(x, fx, fxPrime);
110 
111  fx = f_(x);
112  fxPrime = fPrime_(x);
113 
115  }
116 
117  return x;
118  }
119 
120  private:
121  //============================================================================
122  const std::function<double(double)> f_;
123  const std::function<double(double)> fPrime_;
124 
125  //============================================================================
126  // Method Description:
134  static double calculateX(double x, double fx, double fxPrime) noexcept
135  {
136  return x - fx / fxPrime;
137  }
138  };
139  } // namespace roots
140 } // namespace nc
nc::roots::Iteration::Iteration
Iteration(double epsilon) noexcept
Definition: Iteration.hpp:56
nc::roots::Newton::Newton
Newton(const double epsilon, std::function< double(double)> f, std::function< double(double)> fPrime) noexcept
Definition: Newton.hpp:61
nc::roots::Iteration
ABC for iteration classes to derive from.
Definition: Iteration.hpp:47
nc::roots::Iteration::incrementNumberOfIterations
void incrementNumberOfIterations()
Definition: Iteration.hpp:105
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:41
nc::roots::Iteration::epsilon_
const double epsilon_
Definition: Iteration.hpp:115
Iteration.hpp
nc::roots::Iteration::resetNumberOfIterations
void resetNumberOfIterations() noexcept
Definition: Iteration.hpp:94
nc
Definition: Coordinate.hpp:45
nc::roots::Newton::~Newton
~Newton() noexcept override=default
nc::roots::Newton::Newton
Newton(const double epsilon, const uint32 maxNumIterations, std::function< double(double)> f, std::function< double(double)> fPrime) noexcept
Definition: Newton.hpp:78
Types.hpp
nc::random::f
dtype f(dtype inDofN, dtype inDofD)
Definition: f.hpp:58
nc::roots::Newton::solve
double solve(double x)
Definition: Newton.hpp:100
nc::roots::Newton
Definition: Newton.hpp:50