Skip to content

Defining functions

amomra edited this page Apr 21, 2015 · 2 revisions

Defining functions

The parser allows the definition of a wide variety of different callback functions. Functions with a fixed number of up to ten numeric arguments, functions with a variable number of numeric arguments and functions taking a single string argument plus up to two numeric values. In order to define a parser function you need to specify its name, a callback function and a flag indicating if the function is volatile. Volatile functions are functions that do not always return the same result for the same arguments. They can not be optimized.

The callback functions must have of either one of the following types:

// For fixed number of arguments
double FunType0();
double FunType1(double);
double FunType2(double, double);
double FunType3(double, double, double);
double FunType4(double, double, double, double);
double FunType5(double, double, double, double, double);
// ... and so on to up to 10 parameters

// for a variable number of arguments
//   first arg:   list of arguments
//   second arg:  number of arguments
double MultFunType(double[], int);

// for functions taking a single string plus up to two numeric values
double StrFunType1(string);
double StrFunType2(string, double);
double StrFunType3(string, double, double);

Defining parser functions

Defining callback functions can be done by calling the DefineFun method which are overloaded for any type of callback supported by the library (As the ones listed above).

parser.DefineFun("FunName", Callback, false);

Lambda expressions can be used to define callbacks:

parser.DefineFun("testFun", (double arg0, double arg1) => arg0 + arg1, false);

Bulk mode functions

Although the bulk mode does work with standard callback functions it may sometimes be necessary to have additional informations inside a callback function. Especially Informations like the index of the current variable set and the index of the thread performing the calculation may be crucial to the evaluation process. To facilitate this need a special set of callback functions was added.

// for bulk callbacks
//   first int:   bulk index
//   second int:  thread index
//   doubles: arguments
double BulkFunType0(int, int);
double BulkFunType1(int, int, double);
double BulkFunType2(int, int, double, double);
// ... and so on to up to 10 arguments

Bulk function can be also defined with DefineFun function:

try
{
    Parser p = new Parser();
    p.DefineVar("a", new double[]{ 1, 2, 3, 4, 5});
    p.DefineVar("b", new double[]{ 6, 7, 8, 9, 10});

    parser.DefineFun("testBulkFun", (int bulkIndex, int threadIndex, double arg0, double arg1) =>
    {
        // do stuff with the indexes
        return arg0 + arg1;
    } , false);

    p.Expr = "testBulkFun(a, b)";

    double[] results = p.EvalBulk(5);

    for (int i = 0; i < results.Length; i++)
    {
        Console.WriteLine(results[i]);
    }
}
catch (ParserError e)
{
    Console.WriteLine(e.Message);
}
Clone this wiki locally