40 #include "gsl/gsl_roots.h"
41 #include "gsl/gsl_errno.h"
49 GSLRootFinder::GSLRootFinder() :
51 fRoot(0), fXlow(0), fXup(0),
52 fIter(0), fStatus(-1),
56 fFunction =
new GSLFunctionWrapper();
59 GSLRootFinder::~GSLRootFinder()
62 if (fFunction)
delete fFunction;
65 GSLRootFinder::GSLRootFinder(
const GSLRootFinder &): IRootFinderMethod()
69 GSLRootFinder & GSLRootFinder::operator = (
const GSLRootFinder &rhs)
72 if (
this == &rhs)
return *
this;
77 bool GSLRootFinder::SetFunction( GSLFuncPointer f,
void * p,
double xlow,
double xup) {
81 fFunction->SetFuncPointer( f );
82 fFunction->SetParams( p );
84 int status = gsl_root_fsolver_set( fS->Solver(), fFunction->GetFunc(), xlow, xup);
85 if (status == GSL_SUCCESS)
86 fValidInterval =
true;
88 fValidInterval =
false;
90 return fValidInterval;
93 bool GSLRootFinder::SetFunction(
const IGenFunction & f,
double xlow,
double xup) {
98 fFunction->SetFunction( f );
99 int status = gsl_root_fsolver_set( fS->Solver(), fFunction->GetFunc(), xlow, xup);
100 if (status == GSL_SUCCESS)
101 fValidInterval =
true;
103 fValidInterval =
false;
105 return fValidInterval;
108 void GSLRootFinder::SetSolver(GSLRootFSolver * s ) {
113 void GSLRootFinder::FreeSolver( ) {
118 int GSLRootFinder::Iterate() {
121 if (!fFunction->IsValid() ) {
122 MATH_ERROR_MSG(
"GSLRootFinder::Iterate",
" Function is not valid");
126 if (!fValidInterval ) {
127 MATH_ERROR_MSG(
"GSLRootFinder::Iterate",
" Interval is not valid");
132 status = gsl_root_fsolver_iterate(fS->Solver());
135 fRoot = gsl_root_fsolver_root(fS->Solver() );
137 fXlow = gsl_root_fsolver_x_lower(fS->Solver() );
138 fXup = gsl_root_fsolver_x_upper(fS->Solver() );
146 double GSLRootFinder::Root()
const {
159 const char * GSLRootFinder::Name()
const {
161 return gsl_root_fsolver_name(fS->Solver() );
164 bool GSLRootFinder::Solve (
int maxIter,
double absTol,
double relTol)
174 if (status != GSL_SUCCESS) {
175 MATH_ERROR_MSG(
"GSLRootFinder::Solve",
"error returned when performing an iteration");
179 status = GSLRootHelper::TestInterval(fXlow, fXup, absTol, relTol);
180 if (status == GSL_SUCCESS) {
186 while (status == GSL_CONTINUE && iter < maxIter);
187 if (status == GSL_CONTINUE) {
188 double tol = std::abs(fXup-fXlow);
189 MATH_INFO_MSGVAL(
"GSLRootFinder::Solve",
"exceeded max iterations, reached tolerance is not sufficient",tol);