functional¶
-
class
functional
[source]¶ Functionals handling
Introduction
A functional is a function with parameters, defined as \(f(p;x)\), where \(p\) are the parameters, and \(x\) the arguments. Methods are available to calculate the value of a function for a series of argument values for the given set of parameters, and for the automatic calculation of the derivatives with respect to the parameters.
The created functionals can be used in the fitting or in any other that needs to have generic function values or their derivatives.
A functional has a mask associated with it, to indicate if certain parameters have to be solved for. See masks for details.
Functionals are created in a variety of ways, in general by specifying the name of the functional, together with some necessary information like e.g. the order of a polynomial, or the code needed to compile your privately defined function. Parameters can be set at creation time or later.
- a = fs.gaussian1d() # creates a 1D Gaussian, default arguments - b = fs.open('gaussian1') # creates the same one - a.state() # the 'state' of the functional [type=0, order=-1, ndim=1, npar=3, params=[1 0 1] ] - a.type() # its type gaussian1d - a.ndim() # its dimension (number of arguments) 1 - a.npar() # its number of parameters 3 - b.state() [type=0, order=-1, ndim=1, npar=3, params=[1 0 1] ] - a.f(1); # the value at x=1 0.0625 - a.fdf([0,0.5]); # value and derivatives [[1:2,] 1 1 0 0 0.5 0.5 1.38629 0.693147]
In some cases an order can be specified as well (e.g. for polynomials):
- a := dfs.poly(3) # creates a 3rd order polynomial - b := dfs.functional('polyn',3) # creates the same one, but with # original defaults - a.state() [type=5, order=3, ndim=1, npar=4, params=[1 1 1 1] ] - b.state() [type=5, order=3, ndim=1, npar=4, params=[0 0 0 0] ]
An extremely valuable aspect of the Functionals module is the ability to create a functional from a compiled string specifying an arbitrary function. For example, let us make our own polynomial \(1 + 2*x + 3*x^2\) and evaluate it at a few abcissa locations
- a := dfs.compiled ('p0 + p1*x + p2*x*x', [1,2,3]) # Define - a.f([0,10,20]) # Evaluate at x=[0,10,20] [1 321 1241]
The functions created can also be used to specify the function to be fitted in a least squares fit (see the fitting ).
Methods Summary
Free resources of the functional
Calculate the value of the functional.
Construct a functional tool
Create and return a new functional tool representing a 1D Gaussian function
Create a 2D Gaussian function
Get the number of dimensions
Create and return a new functional tool representing a 1D polynomial function, y = c_0 + c_1*x + c_2*x**2 + .
Create and return a new functional tool representing a 1D power log polynomial function, y = c_0 * x**( c_1 + c_2*ln(x) + c_3*ln(x)**2 + .
-
f
(x=0)[source]¶ Calculate the value of the functional.
Parameters
x ({double, doubleArray}=0)
- real argument values
-
gaussian1d
(amplitude=1, center=0, fwhm=1)[source]¶ Create and return a new functional tool representing a 1D Gaussian function
Create a 1-dimensional Gaussian with the specified amplitude, fwhm, and center.
Parameters
amplitude (double=1)
- amplitude of Gaussiancenter (double=0)
- center of Gaussianfwhm (double=1)
- FWHM of Gaussian
-
gaussian2d
(amplitude=1, center=[- 1], fwhm=[- 1], pa='0')[source]¶ Create a 2D Gaussian function
Create a 2-dimensional Gaussian with the specified amplitude, fwhms, and center. The created functional has method {em f} to calculate the function value at a series of {em x, y} values, or the value.
Parameters
amplitude (double=1)
- Amplitude of Gaussiancenter (doubleArray=[-1])
- Center (x,y) position. Must have exactly 2 elements.fwhm (doubleArray=[-1])
- FWHM of the axes. Must have exactly 2 elements.pa ({string, doubleQuant}='0')
- The angle between the positive y axis and the major axis, measured counterclockwise.
-
polynomial
(coefficients=[0])[source]¶ Create and return a new functional tool representing a 1D polynomial function, y = c_0 + c_1*x + c_2*x**2 + … + c_n*x**n
Create a 1-dimensional polynomial function with the specified coefficents.
Parameters
coefficients (doubleArray=[0])
- Array of coefficients. Number of coefficients determines order of polynomial.
-
powerlogpoly
(coefficients=[0])[source]¶ Create and return a new functional tool representing a 1D power log polynomial function, y = c_0 * x**( c_1 + c_2*ln(x) + c_3*ln(x)**2 + … c_n*ln(x)**(n-1)
Create a 1-dimensional power log polynomial function with the specified coefficents.
Parameters
coefficients (doubleArray=[0])
- Array of coefficients.
-