Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Loading...
Searching...
No Matches
exampleFunction.py
Go to the documentation of this file.
1## \file
2## \ingroup tutorial_math
3## \notebook
4## Example of using Python functions as inputs to numerical algorithms
5## using the ROOT Functor class.
6##
7## \macro_image
8## \macro_output
9## \macro_code
10##
11## \author Lorenzo Moneta
12
13import ROOT
14import array
15try:
16 import numpy as np
17except:
18 print("Failed to import numpy.")
19 exit()
20
21## example 1D function
22
23def f(x):
24 return x*x -1
25
26func = ROOT.Math.Functor1D(f)
27
28#example of using the integral
29
30print("Use Functor1D for wrapping one-dimensional function and compute integral of f(x) = x^2-1")
31
34
35value = ig.Integral(0, 3)
36print("integral-1D value = ", value)
37expValue = 6
38if (not ROOT.TMath.AreEqualRel(value, expValue, 1.E-15)) :
39 print("Error computing integral - computed value - different than expected, diff = ", value - expValue)
40
41# example multi-dim function
42
43print("\n\nUse Functor for wrapping a multi-dimensional function, the Rosenbrock Function r(x,y) and find its minimum")
44
45def RosenbrockFunction(xx):
46 x = xx[0]
47 y = xx[1]
48 tmp1 = y-x*x
49 tmp2 = 1-x
50 return 100*tmp1*tmp1+tmp2*tmp2
51
52
53func2D = ROOT.Math.Functor(RosenbrockFunction,2)
54
55### minimize multi-dim function using fitter class
56
57fitter = ROOT.Fit.Fitter()
58#use a numpy array to pass initial parameter array
59initialParams = np.array([0.,0.], dtype='d')
60fitter.FitFCN(func2D, initialParams)
63 print("Error minimizing Rosenbrock function ")
64
65## example 1d grad function
66## derivative of f(x)= x**2-1
67
68print("\n\nUse GradFunctor1D for making a function object implementing f(x) and f'(x)")
69
70def g(x): return 2 * x
71
72gradFunc = ROOT.Math.GradFunctor1D(f, g)
73
74#check if ROOT has mathmore
75prevLevel = ROOT.gErrorIgnoreLevel
77ret = ROOT.gSystem.Load("libMathMore")
79if (ret < 0) :
80 print("ROOT has not Mathmore")
81 print("derivative value at x = 1", gradFunc.Derivative(1) )
82
83else :
85 rf.SetFunction(gradFunc, 3)
86 rf.Solve()
87 value = rf.Root()
88 print("Found root value x0 : f(x0) = 0 : ", value)
89 if (value != 1):
90 print("Error finding a ROOT of function f(x)=x^2-1")
91
92
93print("\n\nUse GradFunctor for making a function object implementing f(x,y) and df(x,y)/dx and df(x,y)/dy")
94
95def RosenbrockDerivatives(xx, icoord):
96 x = xx[0]
97 y = xx[1]
98 #derivative w.r.t x
99 if (icoord == 0) :
100 return 2*(200*x*x*x-200*x*y+x-1)
101 else :
102 return 200 * (y - x * x)
103
104gradFunc2d = ROOT.Math.GradFunctor(RosenbrockFunction, RosenbrockDerivatives, 2)
105
106fitter = ROOT.Fit.Fitter()
107#here we use a python array to pass initial parameters
108initialParams = array.array('d',[0.,0.])
109fitter.FitFCN(gradFunc2d, initialParams)
111if (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)) :
112 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