Logo ROOT  
Reference Guide
exampleFunction.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_math
3## \notebook
4## Example of using Python functions and input to numerical algorithm
5## using the ROOT Functor class
6##
7## \author Lorenzo Moneta
8
9import ROOT
10import array
11try:
12 import numpy as np
13except:
14 print("Failed to import numpy.")
15 exit()
16
17## example 1D function
18
19def f(x):
20 return x*x -1
21
22func = ROOT.Math.Functor1D(f)
23
24#example of using the integral
25
26print("Use Functor1D for wrapping one-dimensional function and compute integral of f(x) = x^2-1")
27
29ig.SetFunction(func)
30
31value = ig.Integral(0, 3)
32print("integral-1D value = ", value)
33expValue = 6
34if (not ROOT.TMath.AreEqualRel(value, expValue, 1.E-15)) :
35 print("Error computing integral - computed value - different than expected, diff = ", value - expValue)
36
37# example multi-dim function
38
39print("\n\nUse Functor for wrapping a multi-dimensional function, the Rosenbrock Function r(x,y) and find its minimum")
40
41def RosenbrockFunction(xx):
42 x = xx[0]
43 y = xx[1]
44 tmp1 = y-x*x
45 tmp2 = 1-x
46 return 100*tmp1*tmp1+tmp2*tmp2
47
48
49func2D = ROOT.Math.Functor(RosenbrockFunction,2)
50
51### minimize multi-dim function using fitter class
52
53fitter = ROOT.Fit.Fitter()
54#use a numpy array to pass initial parameter array
55initialParams = np.array([0.,0.], dtype='d')
56fitter.FitFCN(func2D, initialParams)
57fitter.Result().Print(ROOT.std.cout)
58if (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)) :
59 print("Error minimizing Rosenbrock function ")
60
61## example 1d grad function
62## derivative of f(x)= x**2-1
63
64print("\n\nUse GradFunctor1D for making a function object implementing f(x) and f'(x)")
65
66def g(x): return 2 * x
67
68gradFunc = ROOT.Math.GradFunctor1D(f, g)
69
70#check if ROOT has mathmore
71prevLevel = ROOT.gErrorIgnoreLevel
72ROOT.gErrorIgnoreLevel=ROOT.kFatal
73ret = ROOT.gSystem.Load("libMathMore")
74ROOT.gErrorIgnoreLevel=prevLevel
75if (ret < 0) :
76 print("ROOT has not Mathmore")
77 print("derivative value at x = 1", gradFunc.Derivative(1) )
78
79else :
80 rf = ROOT.Math.RootFinder(ROOT.Math.RootFinder.kGSL_NEWTON)
81 rf.SetFunction(gradFunc, 3)
82 rf.Solve()
83 value = rf.Root()
84 print("Found root value x0 : f(x0) = 0 : ", value)
85 if (value != 1):
86 print("Error finding a ROOT of function f(x)=x^2-1")
87
88
89print("\n\nUse GradFunctor for making a function object implementing f(x,y) and df(x,y)/dx and df(x,y)/dy")
90
91def RosenbrockDerivatives(xx, icoord):
92 x = xx[0]
93 y = xx[1]
94 #derivative w.r.t x
95 if (icoord == 0) :
96 return 2*(200*x*x*x-200*x*y+x-1)
97 else :
98 return 200 * (y - x * x)
99
100gradFunc2d = ROOT.Math.GradFunctor(RosenbrockFunction, RosenbrockDerivatives, 2)
101
102fitter = ROOT.Fit.Fitter()
103#here we use a python array to pass initial parameters
104initialParams = array.array('d',[0.,0.])
105fitter.FitFCN(gradFunc2d, initialParams)
106fitter.Result().Print(ROOT.std.cout)
107if (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)) :
108 print("Error minimizing Rosenbrock function ")
#define f(i)
Definition: RSha256.hxx:104
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t g
Fitter class, entry point for performing all type of fits.
Definition: Fitter.h:77
Functor1D class for one-dimensional functions.
Definition: Functor.h:506
Documentation for class Functor class.
Definition: Functor.h:401
GradFunctor1D class for one-dimensional gradient functions.
Definition: Functor.h:730
GradFunctor class for Multidimensional gradient functions.
Definition: Functor.h:604
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:84
void Print(std::ostream &os, const OptionType &opt)