Logo ROOT   6.10/09
Reference Guide
GSLRootFinderDeriv.cxx
Go to the documentation of this file.
1 // @(#)root/mathmore:$Id$
2 // Authors: L. Moneta, A. Zsenei 08/2005
3 
4  /**********************************************************************
5  * *
6  * Copyright (c) 2004 ROOT Foundation, CERN/PH-SFT *
7  * *
8  * This library is free software; you can redistribute it and/or *
9  * modify it under the terms of the GNU General Public License *
10  * as published by the Free Software Foundation; either version 2 *
11  * of the License, or (at your option) any later version. *
12  * *
13  * This library is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16  * General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this library (see file COPYING); if not, write *
20  * to the Free Software Foundation, Inc., 59 Temple Place, Suite *
21  * 330, Boston, MA 02111-1307 USA, or contact the author. *
22  * *
23  **********************************************************************/
24 
25 // Implementation file for class GSLRootFinderDeriv
26 //
27 // Created by: moneta at Sun Nov 21 16:26:03 2004
28 //
29 // Last update: Sun Nov 21 16:26:03 2004
30 //
31 
32 #include "Math/IFunction.h"
33 #include "Math/Error.h"
35 #include "Math/GSLRootHelper.h"
36 #include "GSLRootFdFSolver.h"
37 #include "GSLFunctionWrapper.h"
38 
39 #include "gsl/gsl_roots.h"
40 #include "gsl/gsl_errno.h"
41 
42 #include <cmath>
43 
44 namespace ROOT {
45 namespace Math {
46 
47 
49  fFunction(0), fS(0),
50  fRoot(0), fPrevRoot(0),
51  fIter(0), fStatus(-1),
52  fValidPoint(false)
53 {
54  // create function wrapper
56 }
57 
59 {
60  // delete function wrapper
61  if (fFunction) delete fFunction;
62 }
63 
65 {
66 }
67 
69 {
70  // private operator=
71  if (this == &rhs) return *this; // time saving self-test
72 
73  return *this;
74 }
75 
76 
77 
78 
80  fStatus = -1;
81  // set Function with signature as GSL
82  fRoot = xstart;
85  fFunction->SetFdfPointer( Fdf );
86  fFunction->SetParams( p );
87  int status = gsl_root_fdfsolver_set( fS->Solver(), fFunction->GetFunc(), fRoot);
88  if (status == GSL_SUCCESS)
89  fValidPoint = true;
90  else
91  fValidPoint = false;
92 
93  return fValidPoint;
94 
95 }
96 
98  // set solver
99  fS = s;
100 }
101 
103  // free the gsl solver
104  if (fS) delete fS;
105 }
106 
108  // iterate........
109 
110  if (!fFunction->IsValid() ) {
111  MATH_ERROR_MSG("GSLRootFinderDeriv::Iterate"," Function is not valid");
112  return -1;
113  }
114  if (!fValidPoint ) {
115  MATH_ERROR_MSG("GSLRootFinderDeriv::Iterate"," Estimated point is not valid");
116  return -2;
117  }
118 
119 
120  int status = gsl_root_fdfsolver_iterate(fS->Solver());
121  // update Root
122  fPrevRoot = fRoot;
123  fRoot = gsl_root_fdfsolver_root(fS->Solver() );
124  return status;
125 }
126 
127 double GSLRootFinderDeriv::Root() const {
128  // return cached value
129  return fRoot;
130 }
131 
132 const char * GSLRootFinderDeriv::Name() const {
133  // get name from GSL
134  return gsl_root_fdfsolver_name(fS->Solver() );
135 }
136 
137 bool GSLRootFinderDeriv::Solve (int maxIter, double absTol, double relTol)
138 {
139  // solve for roots
140  fStatus = -1;
141  int iter = 0;
142  int status = 0;
143  do {
144  iter++;
145 
146  status = Iterate();
147  if (status != GSL_SUCCESS) {
148  MATH_ERROR_MSG("GSLRootFinderDeriv::Solve","error returned when performing an iteration");
149  fStatus = status;
150  return false;
151  }
152  status = GSLRootHelper::TestDelta(fRoot, fPrevRoot, absTol, relTol);
153  if (status == GSL_SUCCESS) {
154  fIter = iter;
155  fStatus = status;
156  return true;
157  }
158 
159  // std::cout << "iteration " << iter << " Root " << fRoot << " prev Root " <<
160  // fPrevRoot << std::endl;
161  }
162  while (status == GSL_CONTINUE && iter < maxIter);
163 
164  if (status == GSL_CONTINUE) {
165  double tol = std::abs(fRoot-fPrevRoot);
166  MATH_INFO_MSGVAL("GSLRootFinderDeriv::Solve","exceeded max iterations, reached tolerance is not sufficient",tol);
167  }
168 
169  fStatus = status;
170  return false;
171 }
172 
173 
174 } // namespace Math
175 } // namespace ROOT
int Iterate()
iterate (return GSL_SUCCESS in case of successful iteration)
const double absTol
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
bool IsValid()
check if function is valid (has been set)
bool Solve(int maxIter=100, double absTol=1E-8, double relTol=1E-10)
Find the root (return false if failed)
double Root() const
Returns the previously calculated root.
class to wrap a gsl_function_fdf (with derivatives)
GSLFunctionDerivWrapper * fFunction
void SetSolver(GSLRootFdFSolver *s)
Interface for finding function roots of one-dimensional functions.
double(* GSLFuncPointer)(double, void *)
#define MATH_ERROR_MSG(loc, str)
Definition: Error.h:50
bool SetFunction(const IGradFunction &f, double xstart)
Sets the function for algorithms using derivatives.
gsl_root_fdfsolver * Solver() const
const char * Name() const
Return name of root finder algorithm.
const double tol
GSLRootFinderDeriv & operator=(const GSLRootFinderDeriv &)
Root-Finder with derivatives implementation class using GSL.
double f(double x)
int TestDelta(double x1, double x0, double epsAbs, double epsRel)
Base class for GSL Root-Finding algorithms for one dimensional functions which use function derivativ...
Namespace for new Math classes and functions.
void(* GSLFdFPointer)(double, void *, double *, double *)
#define MATH_INFO_MSGVAL(loc, str, x)
Definition: Error.h:65