ROOT
ROOT project
Loading...
Searching...
No Matches
stepper.hpp
Go to the documentation of this file.
1#ifndef ROOT_STEPPER_HPP
2#define ROOT_STEPPER_HPP
3
4#include <Eigen/Dense>
5#include <functional>
6#include <iostream>
7
8#include "stepper_def.hpp"
9
10template <typename T>
11StepperBase<T>::StepperBase(std::function<double(double)> fun, bool aitken_mode) {
12 this->function = fun;
13 this->aitken_requirement = aitken_mode;
14}
15
16template <typename T>
17Eigen::Vector2d StepperBase<T>::step(Eigen::Vector2d previous_step) {
18 if (!this->aitken_requirement) {
19 return this->compute_step(previous_step);
20 } else {
21 return this->aitken_step(previous_step);
22 }
23}
24
25template <typename T>
26Eigen::Vector2d StepperBase<T>::aitken_step(Eigen::Vector2d previous_iter) {
27 Eigen::Vector2d iter_one = this->compute_step(previous_iter);
28 Eigen::Vector2d iter_two = this->compute_step(iter_one);
29 double denominator = (iter_two(0) - iter_one(0)) / (iter_one(0) - previous_iter(0));
30 if (denominator == 0) {
31 std::cerr << "\033[31mCaught error: Division by 0. The method will diverge\033[0m" << std::endl;
32 }
33 double new_point = iter_two(0) - (pow(iter_two(0) - iter_one(0), 2) / denominator);
34 return {new_point, function(new_point)};
35}
36
37template <>
38NewtonRaphsonStepper<double>::NewtonRaphsonStepper(std::function<double(double)> fun, bool aitken_mode,
39 std::function<double(double)> der)
40 : StepperBase<double>(fun, aitken_mode) {
41 this->derivative = der;
42}
43
44template <>
45Eigen::Vector2d NewtonRaphsonStepper<double>::compute_step(Eigen::Vector2d previous_iteration) {
46 double denominator = derivative(previous_iteration(0));
47 if (denominator == 0) {
48 std::cerr << "\033[31mCaught error: Division by 0. The method will diverge\033[0m" << std::endl;
49 }
50 double new_point = previous_iteration(0) - previous_iteration(1) / denominator;
51 double new_eval = this->function(new_point);
52 return {new_point, new_eval};
53}
54
55template <>
56FixedPointStepper<double>::FixedPointStepper(std::function<double(double)> fun, bool aitken_mode,
57 std::function<double(double)> g_fun)
58 : StepperBase<double>(fun, aitken_mode) {
59 this->fixed_point_function = g_fun;
60}
61
62template <>
63Eigen::Vector2d FixedPointStepper<double>::compute_step(Eigen::Vector2d previous_iteration) {
64 double new_point = this->fixed_point_function(previous_iteration(0));
65 double new_eval = this->function(new_point);
66 return {new_point, new_eval};
67}
68
69template <>
70ChordsStepper<Eigen::Vector2d>::ChordsStepper(std::function<double(double)> fun, bool aitken_mode, Eigen::Vector2d _int)
71 : StepperBase<Eigen::Vector2d>(fun, aitken_mode) {
72 auto interval = _int;
73 this->iter_minus_1 = interval(0);
74 this->iter_zero = interval(1);
75}
76
77template <>
78Eigen::Vector2d ChordsStepper<Eigen::Vector2d>::compute_step(Eigen::Vector2d last_iter) {
79 double numerator = this->iter_zero - this->iter_minus_1;
80 double denominator = last_iter(1) - this->function(this->iter_minus_1);
81 if (denominator == 0) {
82 std::cerr << "\033[31mCaught error: Division by 0. The method will diverge\033[0m" << std::endl;
83 }
84 double new_point = this->iter_zero - last_iter(1) * numerator / denominator;
85 this->iter_minus_1 = this->iter_zero;
86 this->iter_zero = new_point;
87 return {new_point, this->function(new_point)};
88}
89
90template <>
91BisectionStepper<Eigen::Vector2d>::BisectionStepper(std::function<double(double)> fun, bool aitken_mode,
92 Eigen::Vector2d _int)
93 : StepperBase<Eigen::Vector2d>(fun, aitken_mode) {
94 auto interval = _int;
95 this->left_edge = interval(0);
96 this->right_edge = interval(1);
97}
98
99template <>
100Eigen::Vector2d BisectionStepper<Eigen::Vector2d>::compute_step(Eigen::Vector2d last_iter) {
101 if (this->function(left_edge) == 0) {
102 return {left_edge, this->function(left_edge)};
103 }
104 if (this->function(right_edge) == 0) {
105 return {right_edge, this->function(right_edge)};
106 }
107 double x_new = (this->left_edge + this->right_edge) / 2;
108 if (this->function(x_new) * this->function(this->left_edge) < 0) {
109 this->right_edge = x_new;
110 } else {
111 this->left_edge = x_new;
112 }
113 return {x_new, this->function(x_new)};
114}
115
116template class StepperBase<double>;
117template class StepperBase<Eigen::Vector2d>;
118
119template class NewtonRaphsonStepper<double>;
120template class FixedPointStepper<double>;
122template class ChordsStepper<Eigen::Vector2d>;
123
124#endif // ROOT_STEPPER_HPP
The specialized Stepper to compute a step with the Bisection Method.
Definition stepper_def.hpp:136
Eigen::Vector2d compute_step(Eigen::Vector2d previous_iteration) override
Specialized method to compute and return a new step with Bisection. Let left_edge = a,...
BisectionStepper(std::function< double(double)> fun, bool aitken_mode, Eigen::Vector2d _int)
Constructor of a BisectionStepper object.
double right_edge
Bounds of the interval to use (updated at each step)
Definition stepper_def.hpp:138
double left_edge
Definition stepper_def.hpp:138
The specialized Stepper to compute a step with the Chords Method (also called Secants in literature)
Definition stepper_def.hpp:110
double iter_zero
The two previous guesses required at each iteration.
Definition stepper_def.hpp:112
Eigen::Vector2d compute_step(Eigen::Vector2d previous_iteration) override
Specialized method to compute and return a new step with Chords.
ChordsStepper(std::function< double(double)> fun, bool aitken_mode, Eigen::Vector2d _int)
Constructor for the ChordsStepper class.
double iter_minus_1
Definition stepper_def.hpp:112
The specialized Stepper to compute a step with the Fixed Point method.
Definition stepper_def.hpp:87
FixedPointStepper(std::function< double(double)> fun, bool aitken_mode, std::function< double(double)> g_fun)
The specialized constructor - initializes the function and the fixed point function.
std::function< double(double)> fixed_point_function
tores the fixed point to use in the steps
Definition stepper_def.hpp:89
Eigen::Vector2d compute_step(Eigen::Vector2d previous_iteration) override
Specialized method to compute and return a new step with FP.
The specialized Stepper to compute a step with the Newton-Raphson method.
Definition stepper_def.hpp:65
std::function< double(double)> derivative
Stores the derivative of the function.
Definition stepper_def.hpp:67
Eigen::Vector2d compute_step(Eigen::Vector2d previous_iteration) override
Specialized method to compute and return a new step with NR.
NewtonRaphsonStepper(std::function< double(double)> fun, bool aitken_mode, std::function< double(double)> der)
The specialized constructor - initializes the function and the derivative.
The virtual mother stepper class which defines constructor and method in common for all the methods.
Definition stepper_def.hpp:24
Eigen::Vector2d step(Eigen::Vector2d previous_step)
Method handling all the steps involved in computing the new guess.
Definition stepper.hpp:17
Eigen::Vector2d aitken_step(Eigen::Vector2d previous_iter)
Method to handle the computation of a step using Aitken's acceleration.
Definition stepper.hpp:26
StepperBase(std::function< double(double)> fun, bool aitken_mode)
Constructor for virtual Stepper class, which will be inherited by the daughters.
Definition stepper.hpp:11
Contains definitions for all the Classes Stepper to compute steps of numerical methods to find the ro...