ROOT
ROOT project
Loading...
Searching...
No Matches
config.hpp
Go to the documentation of this file.
1
15#ifndef CONFIG_HPP
16#define CONFIG_HPP
17
18#include <functional>
19#include <libROOT/method.hpp>
20
28 public:
33 virtual ~ConfigBase() {};
35 double tolerance;
37 bool aitken;
38 bool verbose;
39 std::function<double(double)> function;
40};
41
49 public:
51 double final_point;
62 BisectionConfig(double tolerance, int max_iterations, bool aitken, std::function<double(double)> function,
63 double initial_point, double final_point, bool verbose) {
64 this->tolerance = tolerance;
65 this->max_iterations = max_iterations;
66 this->aitken = aitken;
67 this->function = function;
68 this->initial_point = initial_point;
69 this->final_point = final_point;
71 this->verbose = verbose;
72 // validation
73 double f_a = function(initial_point);
74 double f_b = function(final_point);
75 if (f_a * f_b > 0) {
76 std::cerr << "033[31Caught error: For Bisection method, function values at initial points must have "
77 "opposite signs.\n";
78 std::cerr << "033[31f(" << initial_point << ") = " << f_a << "\n";
79 std::cerr << "033[31f(" << final_point << ") = " << f_b << "\n";
80 // LLM
81 std::exit(EXIT_FAILURE);
82 }
83 }
84};
85
92class NewtonConfig : public ConfigBase {
93 public:
95 std::function<double(double)> derivative;
106 NewtonConfig(double tolerance, int max_iterations, bool aitken, std::function<double(double)> function,
107 std::function<double(double)> derivative, double initial_guess, bool verbose) {
108 this->tolerance = tolerance;
109 this->max_iterations = max_iterations;
110 this->aitken = aitken;
111 this->function = function;
112 this->derivative = derivative;
113 this->initial_guess = initial_guess;
114 this->method = Method::NEWTON;
115 this->verbose = verbose;
116 }
117};
118
125class ChordsConfig : public ConfigBase {
126 public:
139 ChordsConfig(double tolerance, int max_iterations, bool aitken, std::function<double(double)> function,
140 double initial_point, double final_point, bool verbose) {
141 this->tolerance = tolerance;
142 this->max_iterations = max_iterations;
143 this->aitken = aitken;
144 this->function = function;
145 this->initial_point1 = initial_point;
146 this->initial_point2 = final_point;
147 this->method = Method::CHORDS;
148 this->verbose = verbose;
149 }
150};
151
159 public:
161 std::function<double(double)> g_function;
172 FixedPointConfig(double tolerance, int max_iterations, bool aitken, std::function<double(double)> function,
173 double initial_guess, std::function<double(double)> g_function, bool verbose) {
174 this->tolerance = tolerance;
175 this->max_iterations = max_iterations;
176 this->aitken = aitken;
177 this->function = function;
178 this->initial_guess = initial_guess;
179 this->g_function = g_function;
181 this->verbose = verbose;
182 }
183};
184
185#endif // CONFIG_HPP
Configuration (data) class for the Bisection method.
Definition config.hpp:48
double final_point
The final point of the interval.
Definition config.hpp:51
BisectionConfig(double tolerance, int max_iterations, bool aitken, std::function< double(double)> function, double initial_point, double final_point, bool verbose)
Constructor for BisectionConfig.
Definition config.hpp:62
double initial_point
The initial point of the interval.
Definition config.hpp:50
Configuration (data) class for the Secant method.
Definition config.hpp:125
ChordsConfig(double tolerance, int max_iterations, bool aitken, std::function< double(double)> function, double initial_point, double final_point, bool verbose)
Constructor for SecantConfig.
Definition config.hpp:139
double initial_point1
The initial point of the interval.
Definition config.hpp:127
double initial_point2
The final point of the interval.
Definition config.hpp:128
Base (shared) configuration (data) class for root-finding methods.
Definition config.hpp:27
Method method
The root-finding method to be used.
Definition config.hpp:34
bool aitken
Indicates whether Aitken acceleration is enabled.
Definition config.hpp:37
std::function< double(double)> function
The function for which the root is to be found.
Definition config.hpp:39
double tolerance
The tolerance for convergence.
Definition config.hpp:35
int max_iterations
The maximum number of iterations allowed.
Definition config.hpp:36
bool verbose
Indicates whether verbose output is enabled.
Definition config.hpp:38
virtual ~ConfigBase()
Virtual destructor required for downcasting objects to derived class types (using dynamic_cast).
Definition config.hpp:33
Configuration (data) class for the Fixed Point method.
Definition config.hpp:158
std::function< double(double)> g_function
The g function for the Fixed Point method.
Definition config.hpp:161
double initial_guess
The initial guess for the root.
Definition config.hpp:160
FixedPointConfig(double tolerance, int max_iterations, bool aitken, std::function< double(double)> function, double initial_guess, std::function< double(double)> g_function, bool verbose)
Constructor for FixedPointConfig.
Definition config.hpp:172
Configuration (data) class for the Newton method.
Definition config.hpp:92
std::function< double(double)> derivative
The derivative of the function.
Definition config.hpp:95
NewtonConfig(double tolerance, int max_iterations, bool aitken, std::function< double(double)> function, std::function< double(double)> derivative, double initial_guess, bool verbose)
Constructor for NewtonConfig.
Definition config.hpp:106
double initial_guess
The initial guess for the root.
Definition config.hpp:94
Method
Enumeration of available root-finding methods.
Definition method.hpp:8
@ FIXED_POINT
Definition method.hpp:8
@ BISECTION
Definition method.hpp:8
@ CHORDS
Definition method.hpp:8
@ NEWTON
Definition method.hpp:8