Functors

Theoretical description of scattering experiments is based on Scattering Theory, which is a general framework for studying scattering processes of waves and particles. The final equation of intensity of scattered beam depends on a sample type, thin film or bulky sample, isotropic or anisotropic; setup components like, detector type - stripe, point or 2d image detector, slits configuration, beam profile, etc. One should not forget about diffuse scattering and background which sometimes cannot be defined precisely and are included in the optimized intensity equation as a polynom. To include all that into one framework and to give a user a freedom to describe special customized solutions for their laboratory setups or samples is a challenging task. After some trial and error we cam to conclusion that ESCAPE framework requires a flexible functor object.

Functor is an object which plays the role of a function of one or several variables. The maximum number of variables which is currenly supported is 5. Functors support mathematical operations and arithmetic operators.

When ESCAPE was developed we tried to implement functors so, that their notations would be as close as possible to functions in algebra. For that several entites have been introduced. The first one is a function variable.

Variables

One creates a variable in the follwoing way:

Variables support algebraic expressions and operators. If one writes $x+y$, the result of this expression is a two-dimensional functor, i.e. function with two variables $f(x, y)$

Const functor

Const functor is a functor which regardless of its variables values, always returns a given constant value. Constant can be also a parameter. As an input constant functor requires also a list of variables.

Expressions

As it has already been mentioned, functors support arithmetic operators and mathematical functions: sin(), cos(), tan(), sinh(), cosh(), tanh(), exp(), sqrt(), log(), log10(), pow(), min(), max(), erf().

Below we demonstrate a simple oscillating functor with damping amplitude.

Variables order

ESCAPE supports expressions with functors of different dimensions and preserves the order of variables in the resulted functor. For example, $f_1(z, x)+f_2(x)+f_3(y)$ will result in the following functor $f_r(z, x, y)$, not $f_r(x, y, z)$ as maybe desired. One has to keep this in mind when creating large complex expressions. One can always check the order of variables using variables property of functors.

Boolean functors and conditional expressions

If two functors are compared using one of the standard comparison operators >, <, >=, <=, ==, !=, a product of this comparison is a boolean functor. For example, we have two functors f1(x)=x^2 and f2(x,y)=x+y

Fb is a boolean functor of two variables $f(x, y)$. When beeing called with with values of variables $x$ and $y$, it calls f1(x) and f2(x, y) and returns a result of f1(x) >= f2(x). Boolean functors support logical operators: or - $|$, and - $&$ and not - '~'. Python doesn't allow to override standard operators like and, not, or, thus, we use bitwise operators for this purpose.

Boolean functors are typically used with conditional expressions. The first argument of the conditional method is a condition itself, i.e. a boolean functor, the second one is a functor which has to be executed if the condition returns true, and the last one is executed if the condition returns false.

Functors with complex return type

In scattering the connection between experiment geometry and equations describing measured intensity is done in terms of reciprocal space. In the first approximation, called Born approximation, the conversion between real- and reciprocal- space is performed using Fourier Transform. The result of Fourier Transform can be a complex function. Thus, it make sense to introduce a functor with complex return type.

Complex functors support the same arithmetic operators and mathematical functors as functors with double return type. Additionally there are several other methods like, norm, real, imag, conjugate which require cplx_functor_obj input parameter. Few examples are given below.