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 for fitiing as provided by pyrap.fitting.
A functional has a mask associated with it, to indicate if certain parameters have to be solved for. See masks for details.
To access the functionals module import pyrap.functionals.
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 d efined function. Parameters can be set at creation time or later:
>>> from pyrap.functionals import functional, gaussian1d
>>> a = gaussian1d() # creates a 1D Gaussian, default arguments
>>> b = functional('gaussian1') # creates the same one
>>> print a.f(1) # the value at x=1
[0.062500000000000028]
>>> print a(1)
[0.062500000000000028]
>>> print a.fdf([0,0.5]); # value and derivatives
>>> print a([0, 0.5], derivative=True)
In some cases an order can be specified as well (e.g. for polynomials):
>>> from pyrap.functionals import functional, poly
>>> a = poly(3) # creates a 3rd order polynomial
>>> print a
{'ndim': 1, 'masks': array([ True, True, True, True], dtype=bool),
'params': array([ 1., 1., 1., 1.]), 'npar': 4, 'type': 5, 'order': 3}
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*x2 and evaluate it at a few abcissa locations:
>>> from pyrap.functionals import compiled
>>> a = compiled('p0 + p1*x + p2*x*x', [1,2,3]) # Define
>>> a([0,10,20]) # Evaluate at x=[0,10,20]
[1.0, 321.0, 1241.0]
The functions created can also be used to specify the function to be fitted in a least squares fit.
Calculate the value of the functional for the specified arguments (taking any specified mask into account).
| Parameter: | x – the value(s) to evaluate at |
|---|
Example:
a = gaussian1d()
a.f(0.0)
# equivalent
a(0.0)
| Parameter: | x – the value(s) to evaluate at Example: a = gaussian1d()
a.fdf(0.0)
# equivalent
a(0.0, derivative=True)
|
|---|
Return the dimensionality of the functional
| Retval: | int |
|---|
Return the numper of parameters of the functional :retval: int
Equivalent:
>>> p = poly(2)
>>> print p.npar()
3
>>> print len(p)
3
Create a 1-dimensional Gaussian with the specified height, width and center. :param params: the [height, center, width] as a list
Calculate the value of the functional for the specified arguments (taking any specified mask into account).
| Parameter: | x – the value(s) to evaluate at |
|---|
Example:
a = gaussian1d()
a.f(0.0)
# equivalent
a(0.0)
| Parameter: | x – the value(s) to evaluate at Example: a = gaussian1d()
a.fdf(0.0)
# equivalent
a(0.0, derivative=True)
|
|---|
Return the dimensionality of the functional
| Retval: | int |
|---|
Return the numper of parameters of the functional :retval: int
Equivalent:
>>> p = poly(2)
>>> print p.npar()
3
>>> print len(p)
3
Create a two-dimensional gaussian.
| Parameters: |
|
|---|
Calculate the value of the functional for the specified arguments (taking any specified mask into account).
| Parameter: | x – the value(s) to evaluate at |
|---|
Example:
a = gaussian1d()
a.f(0.0)
# equivalent
a(0.0)
| Parameter: | x – the value(s) to evaluate at Example: a = gaussian1d()
a.fdf(0.0)
# equivalent
a(0.0, derivative=True)
|
|---|
Return the dimensionality of the functional
| Retval: | int |
|---|
Return the numper of parameters of the functional :retval: int
Equivalent:
>>> p = poly(2)
>>> print p.npar()
3
>>> print len(p)
3
Create a polynomial of specified degree. The default parameters are all 1. (Note that using the generic functional function the parameters are all set to 0).
| Parameters: |
|
|---|
Calculate the value of the functional for the specified arguments (taking any specified mask into account).
| Parameter: | x – the value(s) to evaluate at |
|---|
Example:
a = gaussian1d()
a.f(0.0)
# equivalent
a(0.0)
| Parameter: | x – the value(s) to evaluate at Example: a = gaussian1d()
a.fdf(0.0)
# equivalent
a(0.0, derivative=True)
|
|---|
Return the dimensionality of the functional
| Retval: | int |
|---|
Return the numper of parameters of the functional :retval: int
Equivalent:
>>> p = poly(2)
>>> print p.npar()
3
>>> print len(p)
3
Create an odd polynomial of specified degree.
| Parameters: |
|
|---|
Calculate the value of the functional for the specified arguments (taking any specified mask into account).
| Parameter: | x – the value(s) to evaluate at |
|---|
Example:
a = gaussian1d()
a.f(0.0)
# equivalent
a(0.0)
| Parameter: | x – the value(s) to evaluate at Example: a = gaussian1d()
a.fdf(0.0)
# equivalent
a(0.0, derivative=True)
|
|---|
Return the dimensionality of the functional
| Retval: | int |
|---|
Return the numper of parameters of the functional :retval: int
Equivalent:
>>> p = poly(2)
>>> print p.npar()
3
>>> print len(p)
3
Create an even polynomial of specified degree.
| Parameters: |
|
|---|
Calculate the value of the functional for the specified arguments (taking any specified mask into account).
| Parameter: | x – the value(s) to evaluate at |
|---|
Example:
a = gaussian1d()
a.f(0.0)
# equivalent
a(0.0)
| Parameter: | x – the value(s) to evaluate at Example: a = gaussian1d()
a.fdf(0.0)
# equivalent
a(0.0, derivative=True)
|
|---|
Return the dimensionality of the functional
| Retval: | int |
|---|
Return the numper of parameters of the functional :retval: int
Equivalent:
>>> p = poly(2)
>>> print p.npar()
3
>>> print len(p)
3
Calculate the value of the functional for the specified arguments (taking any specified mask into account).
| Parameter: | x – the value(s) to evaluate at |
|---|
Example:
a = gaussian1d()
a.f(0.0)
# equivalent
a(0.0)
| Parameter: | x – the value(s) to evaluate at Example: a = gaussian1d()
a.fdf(0.0)
# equivalent
a(0.0, derivative=True)
|
|---|
Return the dimensionality of the functional
| Retval: | int |
|---|
Return the numper of parameters of the functional :retval: int
Equivalent:
>>> p = poly(2)
>>> print p.npar()
3
>>> print len(p)
3
Calculate the value of the functional for the specified arguments (taking any specified mask into account).
| Parameter: | x – the value(s) to evaluate at |
|---|
Example:
a = gaussian1d()
a.f(0.0)
# equivalent
a(0.0)
| Parameter: | x – the value(s) to evaluate at Example: a = gaussian1d()
a.fdf(0.0)
# equivalent
a(0.0, derivative=True)
|
|---|
Return the dimensionality of the functional
| Retval: | int |
|---|
Return the numper of parameters of the functional :retval: int
Equivalent:
>>> p = poly(2)
>>> print p.npar()
3
>>> print len(p)
3
Calculate the value of the functional for the specified arguments (taking any specified mask into account).
| Parameter: | x – the value(s) to evaluate at |
|---|
Example:
a = gaussian1d()
a.f(0.0)
# equivalent
a(0.0)
| Parameter: | x – the value(s) to evaluate at Example: a = gaussian1d()
a.fdf(0.0)
# equivalent
a(0.0, derivative=True)
|
|---|
Return the dimensionality of the functional
| Retval: | int |
|---|
Return the numper of parameters of the functional :retval: int
Equivalent:
>>> p = poly(2)
>>> print p.npar()
3
>>> print len(p)
3
Create a function based on the programable string. The string should be a single expression, which can use the standard operators and functions and parentheses, having a single value as a result. The parameters of the function can be addressed with the p variable. This variable can be indexed in two ways. The first way is using the standard a lgebraic way, where the parameters are: p (or p0), p1, p2, ... . The second way is by indexing, where the parameters are addressed as: p[0], p[1], ... . The arguments are accessed in the same way, but using the variable name x. The compilation determines the number of dimensions and parameters of the produced function.
Operators are the standard operators (including comparisons, which produce a zero or one result; and conditional expression).
In addition to the standard expected functions, there is an atan with either one or two arguments (although atan2 exists as well), and pi and ee with no or one argument. The functional created behaves as all other functionals, and hence can be used in combinations.
Examples:
>>> from pyrap.functionals import compiled
>>> import math
>>> a = compiled('sin(pi(0.5) ) +pi'); # an example
>>> print a(0)
array([ 4.1415926535897931])
>>> b = compiled('p*exp(-(x/p[2])^2)')
>>> print b.get_parameters()
[0.0, 0.0]
>>> b.set_parameters([10, 1]) # change to height 10 and halfwidth 1
>>> print b([-1,-0.5,0,.5,1])
array([ 3.6787944117144233,
7.788007830714049,
10.0,
7.788007830714049,
3.6787944117144233])
# the next one is sync(x), catering for x=0
# using the fact that comparisons deliver values. Note
# the extensive calculation to make sure no divison by 0
>>> synca = compiled('( (x==0) * 1)+( (x!=0) * sin(x+(x==0)*1)/(x+(x==0)*1) )')
>>> print synca([-1,0,1])
[0.841471, 10., 0.841471]
>>> print math.sin(1)/1
0.841471
# using conditional expressions:
print compiled('x==0 ? 1 : sin(x)/x')([-1,0,1])
[0.841471, 1.0, 0.841471]
Calculate the value of the functional for the specified arguments (taking any specified mask into account).
| Parameter: | x – the value(s) to evaluate at |
|---|
Example:
a = gaussian1d()
a.f(0.0)
# equivalent
a(0.0)
| Parameter: | x – the value(s) to evaluate at Example: a = gaussian1d()
a.fdf(0.0)
# equivalent
a(0.0, derivative=True)
|
|---|
Return the dimensionality of the functional
| Retval: | int |
|---|
Return the numper of parameters of the functional :retval: int
Equivalent:
>>> p = poly(2)
>>> print p.npar()
3
>>> print len(p)
3