# exampleFunction¶

Example of using Python functions and input to numerical algorithm using the ROOT Functor class

Author: Lorenzo Moneta
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Wednesday, August 17, 2022 at 09:34 AM.

In :
import ROOT
import array
try:
import numpy as np
except:
print("Failed to import numpy.")
exit()

Welcome to JupyROOT 6.27/01


example 1D function

In :
def f(x):
return x*x -1

func = ROOT.Math.Functor1D(f)


xample of using the integral

In :
print("Use Functor1D for wrapping one-dimensional function and compute integral of f(x) = x^2-1")

ig = ROOT.Math.Integrator()
ig.SetFunction(func)

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)


Use Functor1D for wrapping one-dimensional function and compute integral of f(x) = x^2-1
integral-1D value =  5.999999999999999


example multi-dim function

In :
print("\n\nUse Functor for wrapping a multi-dimensional function, the Rosenbrock Function r(x,y) and find its minimum")

def RosenbrockFunction(xx):
x = xx
y = xx
tmp1 = y-x*x
tmp2 = 1-x
return 100*tmp1*tmp1+tmp2*tmp2

func2D = ROOT.Math.Functor(RosenbrockFunction,2)

Use Functor for wrapping a multi-dimensional function, the Rosenbrock Function r(x,y) and find its minimum


# minimize multi-dim function using fitter class¶

In :
fitter = ROOT.Fit.Fitter()


se a numpy array to pass initial parameter array

In :
initialParams = np.array([0.,0.], dtype='d')
fitter.FitFCN(func2D, initialParams)
fitter.Result().Print(ROOT.std.cout)
if (not ROOT.TMath.AreEqualRel(fitter.Result().Parameter(0), 1, 1.E-3) or not ROOT.TMath.AreEqualRel(fitter.Result().Parameter(1), 1, 1.E-3)) :
print("Error minimizing Rosenbrock function ")

****************************************
MinFCN                    =    1.687e-08
NDf                       =            0
Edm                       =  3.37793e-08
NCalls                    =          146
Par_0                     =     0.999952   +/-   1.00372
Par_1                     =     0.999892   +/-   2.00986


example 1d grad function derivative of f(x)= x**2-1

In :
print("\n\nUse GradFunctor1D for making a function object implementing f(x) and f'(x)")

def g(x): return 2 * x


Use GradFunctor1D for making a function object implementing f(x) and f'(x)


heck if ROOT has mathmore

In :
prevLevel = ROOT.gErrorIgnoreLevel
ROOT.gErrorIgnoreLevel=ROOT.kFatal
ROOT.gErrorIgnoreLevel=prevLevel
if (ret < 0) :
print("ROOT has not Mathmore")
print("derivative value at x = 1", gradFunc.Derivative(1) )

else :
rf = ROOT.Math.RootFinder(ROOT.Math.RootFinder.kGSL_NEWTON)
rf.Solve()
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
y = xx
#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)

fitter = ROOT.Fit.Fitter()

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


ere we use a python array to pass initial parameters

In :
initialParams = array.array('d',[0.,0.])
fitter.Result().Print(ROOT.std.cout)
if (not ROOT.TMath.AreEqualRel(fitter.Result().Parameter(0), 1, 1.E-3) or not ROOT.TMath.AreEqualRel(fitter.Result().Parameter(1), 1, 1.E-3)) :
print("Error minimizing Rosenbrock function ")

****************************************
MinFCN                    =  1.46662e-07
NDf                       =            0
Edm                       =  2.93174e-07
NCalls                    =           89
Par_0                     =     0.999885   +/-   0.997051
Par_1                     =     0.999733   +/-   1.99638


Draw all canvases

In :
from ROOT import gROOT
gROOT.GetListOfCanvases().Draw()