You are here

Function Derivation

Classes for Function Derivation

There are in ROOT only two classes to perform numerical derivation. One of them is in the MathCore library while the other is in the MathMore wrapping an integration function from the GSL library:

  • RichardsonDerivator: Implements the Richardson method for numerical integration. It can calculate up to the third derivative of a function. Example:
    #include "Math/Functor.h"
    #include "Math/RichardsonDerivator.h"
     
    double myfunc(double x ) { 
       return 2 + 3*x + 4*x*x; 
    }
     
    int NumericalDerivation(){
      double x0 = 2;
      ROOT::Math::Functor1D f1D(&myfunc);
     
      ROOT::Math::RichardsonDerivator rd;
      rd.SetFunction(f1D);
     
      std::cout << "Derivative of function inheriting from "
                << "IGenFunction f(x) = 2 + 3x + 4x^2 at x = 2" << std::endl;
      std::cout << "First Derivative:   " << rd.Derivative1(x0) << std::endl;
      std::cout << "Second Derivative:  " << rd.Derivative2(x0) << std::endl;
      std::cout << "Third Derivative:   " << rd.Derivative3(x0) << std::endl;
     
      return 0;
    }
  • Derivator: This class uses the GSLIntegrator class to call the GSL library indirectly. Example:
    #include "Math/Functor.h"
    #include "Math/Derivator.h"
     
    double myfunc(double x ) { 
       return 2 + 3*x + 4*x*x; 
    }
     
    int NumericalDerivation(){
     
      ROOT::Math::Functor1D f1D(&myfunc);
      ROOT::Math::Derivator der(f1D);
     
      double step = 1E-8;
      double x0 = 2;
     
      der.SetFunction(f1D);
      double result = der.Eval(x0);
      std::cout << "Derivative of function inheriting from IGenFunction "
                << "f(x) = 2 + 3x + 4x^2 at x = 2" << std::endl;
      std::cout << "Return code:  " << der.Status() << std::endl;
      std::cout << "Result:       " << result << " +/- " << der.Error() 
                << std::endl;
      std::cout << "Exact result: " << 19 << std::endl;
      std::cout << "EvalForward:  " << der.EvalForward(f1D, x0) 
                << std::endl;
      std::cout << "EvalBackward: " << der.EvalBackward(x0, step) 
                << std::endl;
     
      return 0;
    }