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
64
65bool GSLRootFinderDeriv::SetFunction( GSLFuncPointer f, GSLFuncPointer df, GSLFdFPointer Fdf, void * p, double xstart) {
66 fStatus = -1;
67 // set Function with signature as GSL
68 fRoot = xstart;
73 int status = gsl_root_fdfsolver_set( fS->Solver(), fFunction->GetFunc(), fRoot);
74 if (status == GSL_SUCCESS)
75 fValidPoint = true;
76 else
77 fValidPoint = false;
78
79 return fValidPoint;
80
81}
82
84 // set solver
85 fS = s;
86}
87
89 // free the gsl solver
90 if (fS) delete fS;
91}
92
94 // iterate........
95
96 if (!fFunction->IsValid() ) {
97 MATH_ERROR_MSG("GSLRootFinderDeriv::Iterate"," Function is not valid");
98 return -1;
99 }
100 if (!fValidPoint ) {
101 MATH_ERROR_MSG("GSLRootFinderDeriv::Iterate"," Estimated point is not valid");
102 return -2;
103 }
104
105
106 int status = gsl_root_fdfsolver_iterate(fS->Solver());
107 // update Root
109 fRoot = gsl_root_fdfsolver_root(fS->Solver() );
110 return status;
111}
112
114 // return cached value
115 return fRoot;
116}
117
118const char * GSLRootFinderDeriv::Name() const {
119 // get name from GSL
120 return gsl_root_fdfsolver_name(fS->Solver() );
121}
122
123bool GSLRootFinderDeriv::Solve (int maxIter, double absTol, double relTol)
124{
125 // solve for roots
126 fStatus = -1;
127 int iter = 0;
128 int status = 0;
129 do {
130 iter++;
131
132 status = Iterate();
133 if (status != GSL_SUCCESS) {
134 MATH_ERROR_MSG("GSLRootFinderDeriv::Solve","error returned when performing an iteration");
135 fStatus = status;
136 return false;
137 }
138 status = GSLRootHelper::TestDelta(fRoot, fPrevRoot, absTol, relTol);
139 if (status == GSL_SUCCESS) {
140 fIter = iter;
141 fStatus = status;
142 return true;
143 }
144
145 // std::cout << "iteration " << iter << " Root " << fRoot << " prev Root " <<
146 // fPrevRoot << std::endl;
147 }
148 while (status == GSL_CONTINUE && iter < maxIter);
149
150 if (status == GSL_CONTINUE) {
151 double tol = std::abs(fRoot-fPrevRoot);
152 MATH_INFO_MSGVAL("GSLRootFinderDeriv::Solve","exceeded max iterations, reached tolerance is not sufficient",tol);
153 }
154
155 fStatus = status;
156 return false;
157}
158
159
160} // namespace Math
161} // 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
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.
void SetSolver(GSLRootFdFSolver *s)
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.
tbb::task_arena is an alias of tbb::interface7::task_arena, which doesn't allow to forward declare tb...