Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
GSLRootFinder.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 GSLRootFinder
26//
27// Created by: moneta at Sun Nov 14 11:27:11 2004
28//
29// Last update: Sun Nov 14 11:27:11 2004
30//
31
32#include "Math/IFunction.h"
33#include "Math/GSLRootFinder.h"
34#include "Math/GSLRootHelper.h"
35#include "Math/Error.h"
36
37#include "GSLRootFSolver.h"
38#include "GSLFunctionWrapper.h"
39
40#include "gsl/gsl_roots.h"
41#include "gsl/gsl_errno.h"
42#include <cmath>
43
44
45namespace ROOT {
46namespace Math {
47
48
50 fFunction(nullptr), fS(nullptr),
51 fRoot(0), fXlow(0), fXup(0),
52 fIter(0), fStatus(-1),
53 fValidInterval(false)
54{
55 // create function wrapper
57}
58
60{
61 // delete function wrapper
62 if (fFunction) delete fFunction;
63}
64
65bool GSLRootFinder::SetFunction( GSLFuncPointer f, void * p, double xlow, double xup) {
66 // set from GSL function
67 fXlow = xlow;
68 fXup = xup;
71
72 int status = gsl_root_fsolver_set( fS->Solver(), fFunction->GetFunc(), xlow, xup);
73 if (status == GSL_SUCCESS)
74 fValidInterval = true;
75 else
76 fValidInterval = false;
77
78 return fValidInterval;
79}
80
81bool GSLRootFinder::SetFunction( const IGenFunction & f, double xlow, double xup) {
82 // set from IGenFunction
83 fStatus = -1; // invalid the status
84 fXlow = xlow;
85 fXup = xup;
87 int status = gsl_root_fsolver_set( fS->Solver(), fFunction->GetFunc(), xlow, xup);
88 if (status == GSL_SUCCESS)
89 fValidInterval = true;
90 else
91 fValidInterval = false;
92
93 return fValidInterval;
94}
95
97 // set type of solver
98 fS = s;
99}
100
102 // free resources
103 if (fS) delete fS;
104}
105
107 // iterate
108 int status = 0;
109 if (!fFunction->IsValid() ) {
110 MATH_ERROR_MSG("GSLRootFinder::Iterate"," Function is not valid");
111 status = -1;
112 return status;
113 }
114 if (!fValidInterval ) {
115 MATH_ERROR_MSG("GSLRootFinder::Iterate"," Interval is not valid");
116 status = -2;
117 return status;
118 }
119
120 status = gsl_root_fsolver_iterate(fS->Solver());
121
122 // update Root
123 fRoot = gsl_root_fsolver_root(fS->Solver() );
124 // update interval
125 fXlow = gsl_root_fsolver_x_lower(fS->Solver() );
126 fXup = gsl_root_fsolver_x_upper(fS->Solver() );
127
128 //std::cout << "iterate .." << fRoot << " status " << status << " interval "
129 // << fXlow << " " << fXup << std::endl;
130
131 return status;
132}
133
134double GSLRootFinder::Root() const {
135 // return cached value
136 return fRoot;
137}
138/**
139double GSLRootFinder::XLower() const {
140 return fXlow;
141}
142
143double GSLRootFinder::XUpper() const {
144 return fXup;
145}
146*/
147const char * GSLRootFinder::Name() const {
148 // get GSL name
149 return gsl_root_fsolver_name(fS->Solver() );
150}
151
152bool GSLRootFinder::Solve (int maxIter, double absTol, double relTol)
153{
154 // find the roots by iterating
155 fStatus = -1;
156 int status = 0;
157 int iter = 0;
158 do {
159 iter++;
160 status = Iterate();
161 //std::cerr << "RF: iteration " << iter << " status = " << status << std::endl;
162 if (status != GSL_SUCCESS) {
163 MATH_ERROR_MSG("GSLRootFinder::Solve","error returned when performing an iteration");
164 fStatus = status;
165 return false;
166 }
167 status = GSLRootHelper::TestInterval(fXlow, fXup, absTol, relTol);
168 if (status == GSL_SUCCESS) {
169 fIter = iter;
170 fStatus = status;
171 return true;
172 }
173 }
174 while (status == GSL_CONTINUE && iter < maxIter);
175 if (status == GSL_CONTINUE) {
176 double tol = std::abs(fXup-fXlow);
177 MATH_INFO_MSGVAL("GSLRootFinder::Solve","exceeded max iterations, reached tolerance is not sufficient",tol);
178 }
179 fStatus = status;
180 return false;
181}
182
183
184
185
186} // namespace Math
187} // 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
Wrapper class to the gsl_function C structure.
void SetFunction(const FuncType &f)
fill the GSL C struct from a generic C++ callable object implementing operator()
void SetFuncPointer(GSLFuncPointer f)
set in the GSL C struct the pointer to the function evaluation
void SetParams(void *p)
set in the GSL C struct the extra-object pointer
bool IsValid()
check if function is valid (has been set)
Root-Finder implementation class using GSL.
gsl_root_fsolver * Solver() const
const char * Name() const override
double GSLRootFinder::XLower() const { return fXlow; }
GSLFunctionWrapper * fFunction
int Iterate() override
This method is implemented only by the GSLRootFinder and GSLRootFinderDeriv classes and will return a...
void SetSolver(GSLRootFSolver *s)
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.
bool SetFunction(const IGenFunction &f, double xlow, double xup) override
Sets the function for the rest of the algorithms.
Interface (abstract class) for generic functions objects of one-dimension Provides a method to evalua...
Definition IFunction.h:112
Namespace for new Math classes and functions.
int TestInterval(double xlow, double xup, 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...