Expression4j Expression4J

Sample > Agregated expression

About
Documentation
Sample
    Simple expression
    Agregated expression
    Predefined function
    User define function
    Custom element
    Custom operator
    External configuration
Download

Create an agregated expression

An agregated expression is constitued by :
  •  function name (like f or foo or function)
  • parameters (like x or y or foo)
  • equal sign (=)
  • the body of the expression who use a previously defined expression 
Expression does not support space. If spaces are present in the expression, the parsin will generate an error.

My first agregated expression with expression4j

try {
	Expression expression1 = 
ExpressionFactory.createExpression("f(x,y)=2.4e-2*x-3.2*y");
Expression expression2 =
ExpressionFactory.createExpression("g(x,y)=4*f(x,y)+3");

System.out.println("Expression name: " + expression2.getName());
System.out.println("Expression : " + expression2);
System.out.println("Expression parameters: " +
expression2.getParameters());

MathematicalElement me1 = NumberFactory.createReal(2);
MathematicalElement me2 = NumberFactory.createReal(4);
Parameters parameters = ExpressionFactory.createParameters();
parameters.addParameter("x",me1);
parameters.addParameter("y",me2);

System.out.println("Value of expression g for x=2 and y = 4:" +
expression2.evaluate(parameters).getRealValue());

me1 = NumberFactory.createReal(3);
me2 = NumberFactory.createReal(2);
parameters.addParameter("x",me1);
parameters.addParameter("y",me2);
System.out.println("Value of expression g for x=3 and y = 2:" +
expression2.evaluate(parameters).getRealValue());
}
catch (Exception e) {
System.out.println("Error: " + e);
}


another sample:

public static void main(String[] args) {
try {
Expression expression1 = ExpressionFactory
.createExpression("f(x,y)=2.4e-2*x-3.2*y");
Expression expression2 = ExpressionFactory
.createExpression("g(x,y)=4*f(x+3,f(y*2,x/2))+3");
System.out.println("Expression name: " + expression2.getName());
System.out.println("Expression : " + expression2);
System.out.println("Expression parameters: " +
expression2.getParameters());

MathematicalElement me1 = NumberFactory.createReal(2);
MathematicalElement me2 = NumberFactory.createReal(4);
Parameters parameters = ExpressionFactory.createParameters();
parameters.addParameter("x",me1);
parameters.addParameter("y",me2);
System.out.println("Value of expression g for x=2 and y = 4:" +
expression2.evaluate(parameters).getRealValue());

me1 = NumberFactory.createReal(3);
me2 = NumberFactory.createReal(2);
parameters.addParameter("x",me1);
parameters.addParameter("y",me2);
System.out.println("Value of expression g for x=3 and y = 2:" +
expression2.evaluate(parameters).getRealValue());
}
catch (Exception e) {
System.out.println("Error: " + e);
}
}


 
SourceForge.net Logo Support This Project