Custom Functions

<< Click to Display Table of Contents >>

Navigation:  Using SMILE Wrappers > Equations reference >

Custom Functions

It is possible to extend the available function set by calling Network.setExtFunctions. The functions are defined at the network level and stored within XDSL file. setExtFunctions requires an array of strings with the textual definition of functions. Each function definition has the following form:

function-name([parameter1[,parameter2...]]) = expression

For example, a function adding two numbers can be defined by the following string:

add(a,b)=a+b

Recursion is not allowed, but it is possible to use the preceding custom functions in the function expression, for example:

dbl(x)=x+x
quad(x)=2*dbl(x)

Reverse ordering of dbl and quad in the vector passed to setExtFunctions will result in a parser error for quad, because the dbl function is not defined when quad is parsed.

Each parameter specified on the LHS of the equal sign must be used in the function body on the RHS. The following is an incorrect custom function definition, because parameter c is not used:

myFunc(a,b,c)=sin(a)+cos(b)

Custom function can be defined as a constant. In such case the function has no parameters, but its definition and function calls in node equations still require parenthesis:

gEarth()=9.80665

Random number generators can be used in function definitions:

StdNormal()=Normal(0,1)

TwoStep(lo,hi)=Steps(lo,(lo+hi)/2,hi,1,2)

To retrieve the custom functions defined for the network, call Network.getExtFunctions. By default, there are no custom functions. Each call to SetExtFunctions sets up a complete set of custom functions. To add new functions to the existing set, call getExtFunctions first, then add new function definition to the output vector before calling setExtFunctions.