ROOT
ROOT project
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
Solver< T > Class Template Reference

Class Solver managing the solving process and creating the Stepper object. More...

#include <solver_def.hpp>

Collaboration diagram for Solver< T >:
Collaboration graph

Public Member Functions

 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.
 
 Solver (std::function< double(double)> fun, T initial_guess, const Method method, int max_iterations, double tolerance, bool aitken_mode, bool verbose, std::function< double(double)> derivative_or_function_g)
 Constructor for Solver object.
 
Eigen::MatrixX2d solve ()
 Calls everything required to Solve with a method.
 

Private Member Functions

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.
 
void save_results (int iter, Eigen::Vector2d result_to_save)
 Saves the result of a step in a defined row of the results' matrix.
 
Eigen::Vector2d get_previous_result (int step_length)
 Returns a row of the results' matrix.
 
double calculate_error (double x_prev, double x_next)
 Computes the error of the latest iteration.
 
void save_starting_point ()
 Saves the actual initial guess in the top row of the results' matrix, no matter what type will be the Class argument initial_guess.
 
void convert_stepper (std::unique_ptr< StepperBase< T > > &stepper)
 Converts the generic Abstract stepper into a typed one.
 
void convert_stepper (std::unique_ptr< StepperBase< double > > &stepper)
 
void convert_stepper (std::unique_ptr< StepperBase< Eigen::Vector2d > > &stepper)
 
void save_starting_point ()
 
void save_starting_point ()
 

Private Attributes

Method method
 Method which will be used - defined thanks to.
 
int max_iterations
 Stores the maximum iterations for the method.
 
double tolerance
 Stores the tolerance below which the process ends.
 
bool aitken_requirement
 True if Aitken acceleration is required, 0 otherwise.
 
bool verbose
 Verbose mode flag.
 
std::function< double(double)> derivative_or_function_g
 Stores the derivative or g_function of the function if needed.
 
Eigen::MatrixX2d results
 
std::function< double(double)> function
 Stores the function to find the root of and the starting guess for the process.
 
initial_guess
 Templated initial_guess for the method, whose type changes depending on the method itself.
 

Friends

class SolverTester
 Friend class for unit testing purposes.
 

Detailed Description

template<typename T>
class Solver< T >

Class Solver managing the solving process and creating the Stepper object.

Constructor & Destructor Documentation

◆ Solver() [1/2]

template<typename T >
Solver< T >::Solver ( std::function< double(double)>  fun,
initial_guess,
const Method  method,
int  max_iterations,
double  tolerance,
bool  aitken_mode,
bool  verbose 
)

Constructor for Solver object.

Parameters
funThe function to find the root of
initial_guessThe initial guess(es) or interval
methodThe method which will be used for the Solution
max_iterationsMaximum iterations in which the method has to converge
toleranceThe tolerance below which the error/function will make the method converge
aitken_modeOption to apply Aitken's acceleration
verboseOption to give verbose output

◆ Solver() [2/2]

template<typename T >
Solver< T >::Solver ( std::function< double(double)>  fun,
initial_guess,
const Method  method,
int  max_iterations,
double  tolerance,
bool  aitken_mode,
bool  verbose,
std::function< double(double)>  derivative_or_function_g 
)

Constructor for Solver object.

Parameters
funThe function to find the root of
initial_guessThe initial guess(es) or interval
methodThe method which will be used for the Solution
max_iterationsMaximum iterations in which the method has to converge
toleranceThe tolerance below which the error/function will make the method converge
aitken_modeOption to apply Aitken's acceleration
verboseOption to give verbose output
derivative_or_function_gThe derivative of the function (for Newton) or g_function (for Fixed Point)

Member Function Documentation

◆ calculate_error()

template<typename T >
double Solver< T >::calculate_error ( double  x_prev,
double  x_next 
)
private

Computes the error of the latest iteration.

Parameters
x_nextThe new guess x(i)
x_prevThe old guess x(i-1)
Returns
|x_next - x_prev|

◆ convert_stepper() [1/3]

void Solver< double >::convert_stepper ( std::unique_ptr< StepperBase< double > > &  stepper)
private

◆ convert_stepper() [2/3]

void Solver< Eigen::Vector2d >::convert_stepper ( std::unique_ptr< StepperBase< Eigen::Vector2d > > &  stepper)
private

◆ convert_stepper() [3/3]

template<typename T >
void Solver< T >::convert_stepper ( std::unique_ptr< StepperBase< T > > &  stepper)
private

Converts the generic Abstract stepper into a typed one.

Parameters
stepperThe original abstract stepper to be converted
Here is the caller graph for this function:

◆ get_previous_result()

template<typename T >
Eigen::Vector2d Solver< T >::get_previous_result ( int  step_length)
private

Returns a row of the results' matrix.

Parameters
step_lengthtells how far to go up from the bottom row
Returns
2-dimensional vector storing x(end - step_length) and f(x(end - step_length))

◆ save_results()

template<typename T >
void Solver< T >::save_results ( int  iter,
Eigen::Vector2d  result_to_save 
)
private

Saves the result of a step in a defined row of the results' matrix.

Parameters
iterThe row index in which to store the result
result_to_save2-dimensional vector storing the new guess x(i) and the evaluation at it f(x(i))
Here is the caller graph for this function:

◆ save_starting_point() [1/3]

void Solver< double >::save_starting_point ( )
private

◆ save_starting_point() [2/3]

void Solver< Eigen::Vector2d >::save_starting_point ( )
private

◆ save_starting_point() [3/3]

template<typename T >
void Solver< T >::save_starting_point ( )
private

Saves the actual initial guess in the top row of the results' matrix, no matter what type will be the Class argument initial_guess.

Here is the caller graph for this function:

◆ solve()

template<typename T >
Eigen::MatrixX2d Solver< T >::solve ( )

Calls everything required to Solve with a method.

Returns
Matrix storing in the first column x(i) for each iteration i, in the second column f(x(i))
Here is the caller graph for this function:

◆ solver_step()

template<typename T >
void Solver< T >::solver_step ( int &  iter,
std::unique_ptr< StepperBase< T > > &  stepper,
double &  err 
)
private

Creates the stepper, calls the step computation, the error calculation and the results' saver.

Parameters
iterReference to the current iteration, which will be increased once the step is computed
stepperThe stepper object previously created and specialized depending on the method
errReference to the error, which will be computed and updated to check convergence
Here is the caller graph for this function:

Friends And Related Symbol Documentation

◆ SolverTester

template<typename T >
friend class SolverTester
friend

Friend class for unit testing purposes.

Member Data Documentation

◆ aitken_requirement

template<typename T >
bool Solver< T >::aitken_requirement
private

True if Aitken acceleration is required, 0 otherwise.

◆ derivative_or_function_g

template<typename T >
std::function<double(double)> Solver< T >::derivative_or_function_g
private

Stores the derivative or g_function of the function if needed.

◆ function

template<typename T >
std::function<double(double)> Solver< T >::function
private

Stores the function to find the root of and the starting guess for the process.

◆ initial_guess

template<typename T >
T Solver< T >::initial_guess
private

Templated initial_guess for the method, whose type changes depending on the method itself.

◆ max_iterations

template<typename T >
int Solver< T >::max_iterations
private

Stores the maximum iterations for the method.

◆ method

template<typename T >
Method Solver< T >::method
private

Method which will be used - defined thanks to.

Author
Saransh-ccp's Reader

◆ results

template<typename T >
Eigen::MatrixX2d Solver< T >::results
private

Stores in the first column the points computed at each step, in the second the value of the function at those points.

◆ tolerance

template<typename T >
double Solver< T >::tolerance
private

Stores the tolerance below which the process ends.

◆ verbose

template<typename T >
bool Solver< T >::verbose
private

Verbose mode flag.


The documentation for this class was generated from the following files: