11constexpr double tol = 1e-6;
16 double tolerance,
bool aitken_mode,
bool verbose) {
18 this->initial_guess = initial_guess;
19 this->method = method;
20 this->max_iterations = max_iterations;
21 this->tolerance = tolerance;
22 this->results = Eigen::MatrixX2d(0, 2);
23 this->aitken_requirement = aitken_mode;
24 this->verbose = verbose;
28 double tolerance,
bool aitken_mode,
bool verbose,
29 std::function<
double(
double)> derivative_or_function_g) {
31 this->initial_guess = initial_guess;
32 this->method = method;
33 this->max_iterations = max_iterations;
34 this->tolerance = tolerance;
35 this->results = Eigen::MatrixX2d(0, 2);
36 this->aitken_requirement = aitken_mode;
37 this->verbose = verbose;
38 this->derivative_or_function_g = derivative_or_function_g;
43 switch (this->method) {
45 stepper = std::make_unique<NewtonRaphsonStepper<double>>(this->function, this->aitken_requirement,
46 this->derivative_or_function_g);
49 stepper = std::make_unique<FixedPointStepper<double>>(this->function, this->aitken_requirement,
50 this->derivative_or_function_g);
53 std::cerr <<
"\033[31mCaught error: Selected method is not compatible with scalar initial guess\033[0m"
61 switch (this->method) {
63 stepper = std::make_unique<BisectionStepper<Eigen::Vector2d>>(this->function, this->aitken_requirement,
67 stepper = std::make_unique<ChordsStepper<Eigen::Vector2d>>(this->function, this->aitken_requirement,
71 std::cerr <<
"\033[31mCaught error: Selected method is not compatible with vector initial guess\033[0m"
79 results.conservativeResize(iter + 1, 2);
80 this->results.row(iter) = result_to_save.transpose();
85 int target_row = results.rows() - step_length - 1;
86 return this->results.row(target_row);
91 if (results.rows() == 0) results.conservativeResize(1, 2);
92 this->save_results(0, {this->initial_guess, this->function(this->initial_guess)});
97 if (results.rows() == 0) results.resize(1, 2);
98 double to_save = initial_guess(1);
99 this->save_results(0, {to_save, this->function(to_save)});
104 return abs(x_prev - x_next);
111 std::unique_ptr<StepperBase<T>> stepper;
113 convert_stepper(stepper);
115 save_starting_point();
118 std::cout <<
"x(0): " << this->get_previous_result(0)(0) <<
"; f(x0): " << this->get_previous_result(0)(1)
124 while (err > this->tolerance && abs(this->get_previous_result(0)(1)) > this->tolerance &&
125 iter < this->max_iterations) {
127 std::cout <<
"x(0): " << this->get_previous_result(0)(0) <<
"; f(x0): " << this->get_previous_result(0)(1)
130 this->solver_step(iter, stepper, err);
133 if (iter == this->max_iterations && err > this->tolerance) {
134 std::cerr <<
"033[31mThe solution did not converge in" << this->max_iterations <<
" iterations\033[0m"
138 if (err <= this->tolerance || abs(this->get_previous_result(0)(1)) <= this->tolerance) {
139 std::cout <<
"Converged in " << iter - 1 <<
" iterations." << std::endl;
142 std::cout <<
"Final estimate: x = " << this->get_previous_result(0)(0)
143 <<
"; f(x) = " << this->get_previous_result(0)(1) <<
"; error = " << err << std::endl;
151 std::cout <<
"Iteration " << iter <<
": ";
153 auto new_results = stepper->step(this->get_previous_result(0));
154 this->save_results(iter, new_results);
155 err = this->calculate_error(new_results(0), this->get_previous_result(1)(0));
Class Solver managing the solving process and creating the Stepper object.
Definition solver_def.hpp:31
void save_results(int iter, Eigen::Vector2d result_to_save)
Saves the result of a step in a defined row of the results' matrix.
Definition solver.hpp:78
Eigen::MatrixX2d solve()
Calls everything required to Solve with a method.
Definition solver.hpp:108
void save_starting_point()
Saves the actual initial guess in the top row of the results' matrix, no matter what type will be the...
void convert_stepper(std::unique_ptr< StepperBase< T > > &stepper)
Converts the generic Abstract stepper into a typed one.
void solver_step(int &iter, std::unique_ptr< StepperBase< T > > &stepper, double &err)
Creates the stepper, calls the step computation, the error calculation and the results' saver.
Definition solver.hpp:149
Eigen::Vector2d get_previous_result(int step_length)
Returns a row of the results' matrix.
Definition solver.hpp:84
double calculate_error(double x_prev, double x_next)
Computes the error of the latest iteration.
Definition solver.hpp:103
Solver(std::function< double(double)> fun, T initial_guess, const Method method, int max_iterations, double tolerance, bool aitken_mode, bool verbose)
Constructor for Solver object.
Definition solver.hpp:15
The virtual mother stepper class which defines constructor and method in common for all the methods.
Definition stepper_def.hpp:24
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
constexpr double tol
Definition solver.hpp:11
constexpr int max_iters
Definition solver.hpp:12
Contains definition of class Solver to find the root of a non-linear equation with some numerical met...