[back] [home] [content] [continue]
You can also define new variables and even functions by calling the appropriate method:
All you need is
String
for the variable name,Object
as the value andType
of this value.Example:
ExpressionConfiguration config=new ExpressionConfiguration(); //example 1: config.defineVariable("x", new Real(2.5), RealType.TYPE); //example 2: Complex[] complexArray=new Complex[5]; for(int i=0; i<complexArray.length; i++) complexArray[i]=new Complex(i, i); config.defineVariable("v", new ComplexVector(complexArray), ComplexVectorType.TYPE); //now you can use these variables within any expression config.setExpression("x*v"); ComplexVector cv=(ComplexVector)config.evaluateExpression();
Note: You can only define variables of the supported Types
:
Note: It's very important to give the variable its correct
Type
(it doesn't work automatically). If not, a wrong
Evaluator
will be created and a ClassCastException
will result. For example:
ExpressionConfiguration config=new ExpressionConfiguration(); //big mistake: variable type has to be ComplexType.TYPE config.defineVariable("x", new Complex(2,2), RealType.TYPE); config.setExpression("x+2"); try { Object o=config.evaluateExpression(); } catch(ClassCastException ex) { ex.printStackTrace(); } //-> RealPlusComplexEvaluator will be created instead of the needed ComplexPlusComplexEvaluator //-> ClassCastException
Note: A variable can be defined independently from the chosen default type. For example: If the type is real, a variable can also be complex.
ExpressionConfiguration config=new ExpressionConfiguration(RealType.TYPE); config.defineVariable("a", new Complex(1,1), ComplexType.TYPE); config.setExpression("2a"); //UML-Objektdiagram Complex c=(Complex)config.evaluateExpression(); //result: 2.0+2.0i
All you need is
String
for the function name,String
array which contains all parameter names andString
representing the function definition as an
expression.Example:
ExpressionConfiguration config=new ExpressionConfiguration(); //example 1: define f(x,y)=x*y config.defineFunction("f", new String[] {"x","y"}, "x*y"); //example 2: define g(z)=z+f(z,z) config.defineFunction("g", new String[] {"z"}, "z+f(z,z)"); //now you can use these functions within any expression config.setExpression("f(3,4)+g(2i)"); Complex cv=(Complex)config.evaluateExpression();
Note: A function must be defined before you can call it
in an expression whereas a variable can also be defined after
the expression was setted (see: "Definition-Handling in mathExpr").
But in any case definitions must be closed before evaluation, otherwise it
will result an UnknownDefinitionException
.
Example:
ExpressionConfiguration config=new ExpressionConfiguration(); //possible: config.defineVariable("a", new Real(1), RealType.TYPE); config.setExpression("2a"); //possible, too: config.setExpression("4+b"); config.defineVariable("b", new Real(2), RealType.TYPE); //not possible: config.setExpression("f(5)"); config.defineFunction("f", new String[] {"x"}, "x^2"); //of course not possible: config.setExpression("newVar-newVar"); Object result=config.evaluateExpression(); config.defineVariable("newVar", new Real(3), RealType.TYPE);