Logo ROOT  
Reference Guide
 
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 array
14
15import ROOT
16
17try:
18 import numpy as np
19except Exception as e:
20 print("Failed to import numpy:", e)
21 exit()
22
23## example 1D function
24
25def f(x):
26 return x*x -1
27
28func = ROOT.Math.Functor1D(f)
29
30#example of using the integral
31
32print("Use Functor1D for wrapping one-dimensional function and compute integral of f(x) = x^2-1")
33
36
37value = ig.Integral(0, 3)
38print("integral-1D value = ", value)
39expValue = 6
40if (not ROOT.TMath.AreEqualRel(value, expValue, 1.E-15)) :
41 print("Error computing integral - computed value - different than expected, diff = ", value - expValue)
42
43# example multi-dim function
44
45print("\n\nUse Functor for wrapping a multi-dimensional function, the Rosenbrock Function r(x,y) and find its minimum")
46
47def RosenbrockFunction(xx):
48 x = xx[0]
49 y = xx[1]
50 tmp1 = y-x*x
51 tmp2 = 1-x
52 return 100*tmp1*tmp1+tmp2*tmp2
53
54
55func2D = ROOT.Math.Functor(RosenbrockFunction,2)
56
57### minimize multi-dim function using fitter class
58
59fitter = ROOT.Fit.Fitter()
60#use a numpy array to pass initial parameter array
61initialParams = np.array([0.,0.], dtype='d')
62fitter.FitFCN(func2D, initialParams)
65 print("Error minimizing Rosenbrock function ")
66
67## example 1d grad function
68## derivative of f(x)= x**2-1
69
70print("\n\nUse GradFunctor1D for making a function object implementing f(x) and f'(x)")
71
72def g(x): return 2 * x
73
74# GSL_Newton is part of ROOT's MathMore library, which is not always active.
75# Let's therefore run this in a try block:
76try:
77 gradFunc = ROOT.Math.GradFunctor1D(f, g)
79 rf.SetFunction(gradFunc, 3)
80 rf.Solve()
81 value = rf.Root()
82 print("Found root value x0 : f(x0) = 0 : ", value)
83 if value != 1:
84 print("Error finding a ROOT of function f(x)=x^2-1")
85except Exception as e:
86 print(e)
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)
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
#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(GNN_Data &d, std::string txt="")
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:94
User Class to find the Root of one dimensional functions.
Definition RootFinder.h:73