Logo ROOT   6.08/07
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 <iostream>
43 #include <cmath>
44 
45 namespace ROOT {
46 namespace Math {
47 
48 
50  fFunction(0), fS(0),
51  fRoot(0), fPrevRoot(0),
52  fIter(0), fStatus(-1),
53  fValidPoint(false)
54 {
55  // create function wrapper
57 }
58 
60 {
61  // delete function wrapper
62  if (fFunction) delete fFunction;
63 }
64 
66 {
67 }
68 
70 {
71  // private operator=
72  if (this == &rhs) return *this; // time saving self-test
73 
74  return *this;
75 }
76 
77 
78 
79 
81  fStatus = -1;
82  // set Function with signature as GSL
83  fRoot = xstart;
86  fFunction->SetFdfPointer( Fdf );
87  fFunction->SetParams( p );
88  int status = gsl_root_fdfsolver_set( fS->Solver(), fFunction->GetFunc(), fRoot);
89  if (status == GSL_SUCCESS)
90  fValidPoint = true;
91  else
92  fValidPoint = false;
93 
94  return fValidPoint;
95 
96 }
97 
99  // set solver
100  fS = s;
101 }
102 
104  // free the gsl solver
105  if (fS) delete fS;
106 }
107 
109  // iterate........
110 
111  if (!fFunction->IsValid() ) {
112  MATH_ERROR_MSG("GSLRootFinderDeriv::Iterate"," Function is not valid");
113  return -1;
114  }
115  if (!fValidPoint ) {
116  MATH_ERROR_MSG("GSLRootFinderDeriv::Iterate"," Estimated point is not valid");
117  return -2;
118  }
119 
120 
121  int status = gsl_root_fdfsolver_iterate(fS->Solver());
122  // update Root
123  fPrevRoot = fRoot;
124  fRoot = gsl_root_fdfsolver_root(fS->Solver() );
125  return status;
126 }
127 
128 double GSLRootFinderDeriv::Root() const {
129  // return cached value
130  return fRoot;
131 }
132 
133 const char * GSLRootFinderDeriv::Name() const {
134  // get name from GSL
135  return gsl_root_fdfsolver_name(fS->Solver() );
136 }
137 
138 bool GSLRootFinderDeriv::Solve (int maxIter, double absTol, double relTol)
139 {
140  // solve for roots
141  fStatus = -1;
142  int iter = 0;
143  int status = 0;
144  do {
145  iter++;
146 
147  status = Iterate();
148  if (status != GSL_SUCCESS) {
149  MATH_ERROR_MSG("GSLRootFinderDeriv::Solve","error returned when performing an iteration");
150  fStatus = status;
151  return false;
152  }
153  status = GSLRootHelper::TestDelta(fRoot, fPrevRoot, absTol, relTol);
154  if (status == GSL_SUCCESS) {
155  fIter = iter;
156  fStatus = status;
157  return true;
158  }
159 
160  // std::cout << "iteration " << iter << " Root " << fRoot << " prev Root " <<
161  // fPrevRoot << std::endl;
162  }
163  while (status == GSL_CONTINUE && iter < maxIter);
164 
165  if (status == GSL_CONTINUE) {
166  double tol = std::abs(fRoot-fPrevRoot);
167  MATH_INFO_MSGVAL("GSLRootFinderDeriv::Solve","exceeded max iterations, reached tolerance is not sufficient",tol);
168  }
169 
170  fStatus = status;
171  return false;
172 }
173 
174 
175 } // namespace Math
176 } // namespace ROOT
int Iterate()
iterate (return GSL_SUCCESS in case of successful iteration)
const double absTol
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
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