Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
44namespace ROOT {
45namespace Math {
46
47
49 fFunction(nullptr), fS(nullptr),
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
79bool GSLRootFinderDeriv::SetFunction( GSLFuncPointer f, GSLFuncPointer df, GSLFdFPointer Fdf, void * p, double xstart) {
80 fStatus = -1;
81 // set Function with signature as GSL
82 fRoot = xstart;
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
123 fRoot = gsl_root_fdfsolver_root(fS->Solver() );
124 return status;
125}
126
128 // return cached value
129 return fRoot;
130}
131
132const char * GSLRootFinderDeriv::Name() const {
133 // get name from GSL
134 return gsl_root_fdfsolver_name(fS->Solver() );
135}
136
137bool 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
#define MATH_INFO_MSGVAL(loc, txt, x)
Definition Error.h:101
#define MATH_ERROR_MSG(loc, str)
Definition Error.h:83
#define f(i)
Definition RSha256.hxx:104
winID h TVirtualViewer3D TVirtualGLPainter p
class to wrap a gsl_function_fdf (with derivatives)
bool IsValid()
check if function is valid (has been set)
Root-Finder with derivatives implementation class using GSL.
gsl_root_fdfsolver * Solver() const
Base class for GSL Root-Finding algorithms for one dimensional functions which use function derivativ...
GSLFunctionDerivWrapper * fFunction
const char * Name() const override
Return name of root finder algorithm.
int Iterate() override
iterate (return GSL_SUCCESS in case of successful iteration)
double Root() const override
Returns the previously calculated root.
bool Solve(int maxIter=100, double absTol=1E-8, double relTol=1E-10) override
Find the root (return false if failed)
bool SetFunction(const IGradFunction &f, double xstart) override
Sets the function for algorithms using derivatives.
GSLRootFinderDeriv & operator=(const GSLRootFinderDeriv &)
void SetSolver(GSLRootFdFSolver *s)
Interface for finding function roots of one-dimensional functions.
Namespace for new Math classes and functions.
int TestDelta(double x1, double x0, double epsAbs, double epsRel)
double(* GSLFuncPointer)(double, void *)
Function pointer corresponding to gsl_function signature.
This file contains a specialised ROOT message handler to test for diagnostic in unit tests.