Using the JEP package of classes in your project is simple! The following steps will get you started quickly.
jep-x.x.x.jar
file (located in the main directory)
to a directory of your choiceimport com.singularsys.jep.Jep; ... 1: Jep jep = new Jep(); 2: jep.addVariable("x", 10); 3: try { 4: jep.parse("x+1"); 5: Object result = jep.evaluate(); 6: 7: System.out.println("x + 1 = " + result); 8: 9: } catch (Exception e) { 10: System.out.println("An error occured: " + e.getMessage()); 11: }Line 1 creates a new Jep parser object. Line 2 adds the variable "x" to the parser and initializes it's value to 10. In lines 4 and 5, the expression "x+1" is parsed and evaluated. The result is stored in
result
.
Finally, the result is printed, resulting in "x + 1 = 11" being
printed.What's next? Browse through the documentation to see what JEP is all capable of. Also have a look at the code of the sample applets. And don't forget to use the JavaDocs as handy reference!
Three methods for evaluating an expression are available:
Object evaluate()
: Evaluates the last parsed expression and
returns the result as an object.Object evaluate(Node root)
: Evaluates an expression passed
in by it's root node and returns the result as an Object.double evaluateD()
: Evaluates the last parsed expression
and returns the result as a double value. If the result can not be converted
into a double, an EvaluationException is thrown.You may not always know what type the result of the expression is. For example
it could be Double, Vector, Boolean, or String depending on the expression
parsed. You can use the instanceof
operator to identify the type
of the result, then cast the result into the appropriate class.
Three methods for evaluating an expression are available:
Object evaluate()
: Evaluates the last parsed expression and
returns the result as an object.Object evaluate(Node root)
: Evaluates an expression passed
in by it's root node and returns the result as an Object.double evaluateD()
: Evaluates the last parsed expression
and returns the result as a double value. If the result can not be converted
into a double, an EvaluationException is thrown.You may not always know what type the result of the expression is. For example
it could be Double, Vector, Boolean, or String depending on the expression
parsed. You can use the instanceof
operator to identify the type
of the result, then cast the result into the appropriate class.
If you want to speed up your calculations, and you are not using strings,
vectors or complex numbers, you can use the RealEvaluator
instead
of the StandardEvaluator
. Evaluation is sped up by using only
the primitive double
type rather than creating Double
objects. Load the RealEvaluator
by calling setComponent(Component)
as shown below.
import com.singularsys.reals.RealEvaluator; ... jep.setComponent(new RealEvaluator());
The performance gain depends on factors such as the expression, the JVM and operating system. Typically, a peformance increase of over 50% should be expected. Performance tests on a MacBook Pro with a 1.8 GHz Core Duo processor running JVM 1.5 ranged from 2x to 6x performance gain. The same tests on a Windows machine with a 1.6GHz Pentium processor resulted in up to 4x performance. The results are summarized in the following table:
Processor (OS) | JRE Version | Performance Gain |
1.6 GHz Pentium (Windows XP) |
1.6
|
2x - 3x
|
1.6 GHz Pentium (Windows XP) |
1.5
|
2x - 4x
|
1.8 GHz Core Duo (Mac OS X) |
1.5
|
2x - 6x
|
It is possible to perform repeated evaluation of expressions while changing variable values without repeatedly parsing the expression. This, of course, is much faster than re-parsing the expression each time it needs to be evaluated.
The following code shows how the addVariable(String, Object)
and evaluate()
functions can be called repeatedly to change variable
values and re-evaluate an expression.
// add variable to allow parsing jep.addVariable("x", 0); jep.parse("2*x-3"); // evaluate expression for x = 0 to x = 99 for (int i=0; i<100; i++) { // update the value of x jep.addVariable("x", i); // print the result System.out.println("Value at x = " + i + ": " + jep.evaluate()); }
You can use fast repeated evaluation with any evaluator including the RealEvaluator
.
With JEP 3, it is now possible to evaluate expression using decimal arithmetic.
The datatype used to represent numbers in this mode is BigDecimal
rather than double
. Simply use the following code for constructing
a new Jep instance:
import com.singularsys.jep.bigDecimals.BigDecComponents; ... jep = new Jep(new BigDecComponents());
The difference in the accuracy is best shown through an example. When performing
multiplication of two numbers of the double
type, 10*0.09 evaluates
as 0.8999999999999999. But when performing the same calculation using
decimal arithmetic with the BigDecimal
type, 10*0.09 evaluates
as 0.9.
If you are interested in performing high precision arithmetic, refer to the BigDecimal section of this documentation.
You can enable the implicit multiplication option with setImplicitMul(true)
.
The default setting is false
(no implicit multiplication).
Implicit multiplication allows expressions such as "2 x" to be interpreted as "2*x". Note that a space is required between two variables for them to be interpreted as being multiplied. The same holds for a variable followed by a number. For example "y 3" is interpreted as "y*3", but "y3" is interpreted as a single variable with the name y3. If a variable is preceded by a number, no space is required between them for implicit multiplication to come in effect.