NumCpp  2.12.1
A Templatized Header Only C++ Implementation of the Python NumPy Library
Secant.hpp
Go to the documentation of this file.
1
33#pragma once
34
35#include <cmath>
36#include <functional>
37#include <utility>
38
40#include "NumCpp/Core/Types.hpp"
42
43namespace nc::roots
44{
45 //================================================================================
46 // Class Description:
49 class Secant : public Iteration
50 {
51 public:
52 //============================================================================
53 // Method Description:
59 Secant(const double epsilon, std::function<double(double)> f) noexcept :
60 Iteration(epsilon),
61 f_(std::move(f))
62 {
63 }
64
65 //============================================================================
66 // Method Description:
73 Secant(const double epsilon, const uint32 maxNumIterations, std::function<double(double)> f) noexcept :
74 Iteration(epsilon, maxNumIterations),
75 f_(std::move(f))
76 {
77 }
78
79 //============================================================================
80 // Method Description:
83 ~Secant() override = default;
84
85 //============================================================================
86 // Method Description:
93 double solve(double a, double b)
94 {
96
97 if (f_(a) > f_(b))
98 {
99 std::swap(a, b);
100 }
101
102 double x = b;
103 double lastX = a;
104 double fx = f_(b);
105 double lastFx = f_(a);
106
107 while (std::fabs(fx) >= epsilon_)
108 {
109 const double x_tmp = calculateX(x, lastX, fx, lastFx);
110
111 lastFx = fx;
112 lastX = x;
113 x = x_tmp;
114
115 fx = f_(x);
116
118 }
119
120 return x;
121 }
122
123 private:
124 //============================================================================
125 const std::function<double(double)> f_;
126
127 //============================================================================
128 // Method Description:
137 static double calculateX(double x, double lastX, double fx, double lastFx) noexcept
138 {
139 const double functionDifference = fx - lastFx;
140 return x - fx * (x - lastX) / functionDifference;
141 }
142 };
143} // 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:114
void resetNumberOfIterations() noexcept
Definition: Iteration.hpp:94
void incrementNumberOfIterations()
Definition: Iteration.hpp:103
Definition: Secant.hpp:50
double solve(double a, double b)
Definition: Secant.hpp:93
~Secant() override=default
Secant(const double epsilon, const uint32 maxNumIterations, std::function< double(double)> f) noexcept
Definition: Secant.hpp:73
Secant(const double epsilon, std::function< double(double)> f) noexcept
Definition: Secant.hpp:59
dtype f(GeneratorType &generator, dtype inDofN, dtype inDofD)
Definition: f.hpp:56
Definition: Bisection.hpp:43
void swap(NdArray< dtype > &inArray1, NdArray< dtype > &inArray2) noexcept
Definition: swap.hpp:42
std::uint32_t uint32
Definition: Types.hpp:40