5.9.1 FormulaParser

This utility class allows to parse string formula and evaluate it either using variables, or just to get numerical value. The variables are numbered as follows: “x0”, “x1”, “x2”, “x3”, “x4” and etc. “KEY” column corresponds to x0, “VALUE” column to “x1” and etc.

You can also use mathematical functions in formula: “sin”, “cos”, “tan”, “asin”, “acos”, “atan”, “abs”, “exp”, “log”, “lg”, “sqrt”, “rad”, “deg”, “round”, “floor”, “ceil, “rnd”, e.g. “sin(rad(90))”.

You can also use statistical operations: “mean”, “stdev”, “variance”, e.g. “mean(x0)*stdev(x0)”.

Available constants are: “PI”, “E”. Numbers are double values.

Possible operators are: ”+”, ”-”, “*”, ”/”, ”%”, “^”.

Note: When using statistical operations, be sure to set the data model to the FormulaParser as shown in the examples.

Using FormulaParser:

1
2
3
4
5
6
7
8
try{
  //Sets FormulaParser object, named fp
  FormulaParser fp = new FormulaParser("5*x0");
  fp.eval(5); //the output would be 25
}catch(Exception e)
{
  e.printStackTrace();
}

Here is an example of using statistics:

1
2
3
4
5
6
7
8
9
try{
  FormulaParser fp = new FormulaParser("mean(x0)");
  fp.setDataModel(dataModel);
  fp.eval(); //the output would be mean value of the data model
}catch(Exception e)
{
  e.printStackTrace();
 
}

Here is an example of trigonometrical functions:

1
2
3
4
5
6
7
8
9
try{
  FormulaParser fp = new FormulaParser("((cos(x0))^2)");
  fp.eval(0); //the output would be 1
  fp.eval(1); //the output would be 0
}catch(Exception e)
{
  e.printStackTrace();
 
}