Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
exampleFunction.py File Reference

Detailed Description

View in nbviewer Open in SWAN
Example of using Python functions as inputs to numerical algorithms using the ROOT Functor class.

ñ␘

Use Functor1D for wrapping one-dimensional function and compute integral of f(x) = x^2-1
integral-1D value = 5.999999999999999
Use Functor for wrapping a multi-dimensional function, the Rosenbrock Function r(x,y) and find its minimum
****************************************
Minimizer is Minuit2 / Migrad
MinFCN = 1.687e-08
NDf = 0
Edm = 1.68896e-08
NCalls = 146
Par_0 = 0.999952 +/- 1.00372
Par_1 = 0.999892 +/- 2.00986
Use GradFunctor1D for making a function object implementing f(x) and f'(x)
Found root value x0 : f(x0) = 0 : 1.0
Use GradFunctor for making a function object implementing f(x,y) and df(x,y)/dx and df(x,y)/dy
****************************************
Minimizer is Minuit2 / Migrad
MinFCN = 2.72222e-08
NDf = 0
Edm = 2.72448e-08
NCalls = 76
Par_0 = 0.999954 +/- 1.00444
Par_1 = 0.999892 +/- 2.01131
import ROOT
import array
try:
import numpy as np
except:
print("Failed to import numpy.")
exit()
## example 1D function
def f(x):
return x*x -1
#example of using the integral
print("Use Functor1D for wrapping one-dimensional function and compute integral of f(x) = x^2-1")
value = ig.Integral(0, 3)
print("integral-1D value = ", value)
expValue = 6
if (not ROOT.TMath.AreEqualRel(value, expValue, 1.E-15)) :
print("Error computing integral - computed value - different than expected, diff = ", value - expValue)
# example multi-dim function
print("\n\nUse Functor for wrapping a multi-dimensional function, the Rosenbrock Function r(x,y) and find its minimum")
x = xx[0]
y = xx[1]
tmp1 = y-x*x
tmp2 = 1-x
return 100*tmp1*tmp1+tmp2*tmp2
func2D = ROOT.Math.Functor(RosenbrockFunction,2)
### minimize multi-dim function using fitter class
fitter = ROOT.Fit.Fitter()
#use a numpy array to pass initial parameter array
initialParams = np.array([0.,0.], dtype='d')
fitter.FitFCN(func2D, initialParams)
print("Error minimizing Rosenbrock function ")
## example 1d grad function
## derivative of f(x)= x**2-1
print("\n\nUse GradFunctor1D for making a function object implementing f(x) and f'(x)")
def g(x): return 2 * x
gradFunc = ROOT.Math.GradFunctor1D(f, g)
#check if ROOT has mathmore
ret = ROOT.gSystem.Load("libMathMore")
if (ret < 0) :
print("ROOT has not Mathmore")
print("derivative value at x = 1", gradFunc.Derivative(1) )
else :
rf.SetFunction(gradFunc, 3)
value = rf.Root()
print("Found root value x0 : f(x0) = 0 : ", value)
if (value != 1):
print("Error finding a ROOT of function f(x)=x^2-1")
print("\n\nUse GradFunctor for making a function object implementing f(x,y) and df(x,y)/dx and df(x,y)/dy")
def RosenbrockDerivatives(xx, icoord):
x = xx[0]
y = xx[1]
#derivative w.r.t x
if (icoord == 0) :
return 2*(200*x*x*x-200*x*y+x-1)
else :
return 200 * (y - x * x)
gradFunc2d = ROOT.Math.GradFunctor(RosenbrockFunction, RosenbrockDerivatives, 2)
fitter = ROOT.Fit.Fitter()
#here we use a python array to pass initial parameters
initialParams = array.array('d',[0.,0.])
fitter.FitFCN(gradFunc2d, initialParams)
print("Error minimizing Rosenbrock function ")
#define f(i)
Definition RSha256.hxx:104
#define g(i)
Definition RSha256.hxx:105
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
void Print(Option_t *option="") const override
Fitter class, entry point for performing all type of fits.
Definition Fitter.h:79
Functor1D class for one-dimensional functions.
Definition Functor.h:97
Documentation for class Functor class.
Definition Functor.h:49
GradFunctor1D class for one-dimensional gradient functions.
Definition Functor.h:271
GradFunctor class for Multidimensional gradient functions.
Definition Functor.h:144
User Class for performing numerical integration of a function in one dimension.
Definition Integrator.h:98
User Class to find the Root of one dimensional functions.
Definition RootFinder.h:73
Author
Lorenzo Moneta

Definition in file exampleFunction.py.