GSLRootFinderDeriv.cxx

Go to the documentation of this file.
00001 // @(#)root/mathmore:$Id: GSLRootFinderDeriv.cxx 32583 2010-03-12 09:57:42Z moneta $
00002 // Authors: L. Moneta, A. Zsenei   08/2005 
00003 
00004  /**********************************************************************
00005   *                                                                    *
00006   * Copyright (c) 2004 ROOT Foundation,  CERN/PH-SFT                   *
00007   *                                                                    *
00008   * This library is free software; you can redistribute it and/or      *
00009   * modify it under the terms of the GNU General Public License        *
00010   * as published by the Free Software Foundation; either version 2     *
00011   * of the License, or (at your option) any later version.             *
00012   *                                                                    *
00013   * This library is distributed in the hope that it will be useful,    *
00014   * but WITHOUT ANY WARRANTY; without even the implied warranty of     *
00015   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   *
00016   * General Public License for more details.                           *
00017   *                                                                    *
00018   * You should have received a copy of the GNU General Public License  *
00019   * along with this library (see file COPYING); if not, write          *
00020   * to the Free Software Foundation, Inc., 59 Temple Place, Suite      *
00021   * 330, Boston, MA 02111-1307 USA, or contact the author.             *
00022   *                                                                    *
00023   **********************************************************************/
00024 
00025 // Implementation file for class GSLRootFinderDeriv
00026 // 
00027 // Created by: moneta  at Sun Nov 21 16:26:03 2004
00028 // 
00029 // Last update: Sun Nov 21 16:26:03 2004
00030 // 
00031 
00032 #include "Math/IFunction.h"
00033 #include "Math/Error.h"
00034 #include "Math/GSLRootFinderDeriv.h"
00035 #include "Math/GSLRootHelper.h"
00036 #include "GSLRootFdFSolver.h"
00037 #include "GSLFunctionWrapper.h"
00038 
00039 #include "gsl/gsl_roots.h"
00040 #include "gsl/gsl_errno.h"
00041 
00042 #include <iostream>
00043 #include <cmath>
00044 
00045 namespace ROOT {
00046 namespace Math {
00047 
00048 
00049 GSLRootFinderDeriv::GSLRootFinderDeriv() : 
00050    fFunction(0), fS(0), 
00051    fRoot(0), fPrevRoot(0), 
00052    fIter(0), fStatus(-1), 
00053    fValidPoint(false)
00054 { 
00055    // create function wrapper
00056    fFunction = new GSLFunctionDerivWrapper(); 
00057 }
00058 
00059 GSLRootFinderDeriv::~GSLRootFinderDeriv() 
00060 {
00061    // delete function wrapper
00062    if (fFunction) delete fFunction;
00063 }
00064 
00065 GSLRootFinderDeriv::GSLRootFinderDeriv(const GSLRootFinderDeriv &) : IRootFinderMethod()
00066 {
00067 }
00068 
00069 GSLRootFinderDeriv & GSLRootFinderDeriv::operator = (const GSLRootFinderDeriv &rhs) 
00070 {
00071    // private operator=
00072    if (this == &rhs) return *this;  // time saving self-test
00073    
00074    return *this;
00075 }
00076 
00077 
00078 
00079 
00080 bool GSLRootFinderDeriv::SetFunction(  GSLFuncPointer f, GSLFuncPointer df, GSLFdFPointer Fdf, void * p, double xstart) {
00081    fStatus = -1; 
00082    // set Function with signature as GSL
00083    fRoot = xstart;
00084    fFunction->SetFuncPointer( f ); 
00085    fFunction->SetDerivPointer( df ); 
00086    fFunction->SetFdfPointer( Fdf ); 
00087    fFunction->SetParams( p ); 
00088    int status = gsl_root_fdfsolver_set( fS->Solver(), fFunction->GetFunc(), fRoot); 
00089    if (status == GSL_SUCCESS)  
00090       fValidPoint = true; 
00091    else 
00092       fValidPoint = false; 
00093 
00094    return fValidPoint;
00095 
00096 }
00097 
00098 void GSLRootFinderDeriv::SetSolver(GSLRootFdFSolver * s ) {
00099    // set solver
00100    fS = s; 
00101 }
00102 
00103 void GSLRootFinderDeriv::FreeSolver( ) { 
00104    // free the gsl solver
00105    if (fS) delete fS; 
00106 }
00107 
00108 int GSLRootFinderDeriv::Iterate() { 
00109    // iterate........
00110    
00111    if (!fFunction->IsValid() ) {
00112       MATH_ERROR_MSG("GSLRootFinderDeriv::Iterate"," Function is not valid");
00113       return -1; 
00114    }
00115    if (!fValidPoint ) {
00116       MATH_ERROR_MSG("GSLRootFinderDeriv::Iterate"," Estimated point is not valid");
00117       return -2; 
00118    }
00119 
00120    
00121    int status = gsl_root_fdfsolver_iterate(fS->Solver()); 
00122    // update Root
00123    fPrevRoot = fRoot;
00124    fRoot =  gsl_root_fdfsolver_root(fS->Solver() ); 
00125    return status; 
00126 }
00127 
00128 double GSLRootFinderDeriv::Root() const {
00129    // return cached value
00130    return fRoot; 
00131 }
00132 
00133 const char * GSLRootFinderDeriv::Name() const {
00134    // get name from GSL
00135    return gsl_root_fdfsolver_name(fS->Solver() ); 
00136 }
00137 
00138 bool GSLRootFinderDeriv::Solve (int maxIter, double absTol, double relTol) 
00139 { 
00140    // solve for roots 
00141    fStatus = -1;
00142    int iter = 0; 
00143    int status = 0; 
00144    do { 
00145       iter++; 
00146       
00147       status = Iterate();
00148       if (status != GSL_SUCCESS) {
00149          MATH_ERROR_MSG("GSLRootFinderDeriv::Solve","error returned when performing an iteration");
00150          fStatus = status;
00151          return false; 
00152       } 
00153       status = GSLRootHelper::TestDelta(fRoot, fPrevRoot, absTol, relTol);
00154       if (status == GSL_SUCCESS) { 
00155          fIter = iter;
00156          fStatus = status;
00157          return true; 
00158       }
00159       
00160       //     std::cout << "iteration " << iter << " Root " << fRoot << " prev Root " << 
00161       //       fPrevRoot << std::endl;
00162    }
00163    while (status == GSL_CONTINUE && iter < maxIter); 
00164 
00165    if (status == GSL_CONTINUE) { 
00166       double tol = std::abs(fRoot-fPrevRoot);
00167       MATH_INFO_MSGVAL("GSLRootFinderDeriv::Solve","exceeded max iterations, reached tolerance is not sufficient",tol);
00168    }
00169 
00170    fStatus = status; 
00171    return false;
00172 }
00173 
00174 
00175 } // namespace Math
00176 } // namespace ROOT

Generated on Tue Jul 5 14:34:56 2011 for ROOT_528-00b_version by  doxygen 1.5.1