The ExprEvaluator class
|
The above image shows the InfixEvalDemo with the "Use variables t, x, y without '='" option selected. values for t, x, and y were provided and the infix expression was evaluated. Actually the "values" for t, x, and y can be infix expressions as well. They are evaluated top to bottom. Note: The ap's output actually includes a visual of the expression tree but it hidden in the image to reduce the space needed for the image.
With Use variables a, b, ..., z with "=" selected, a user function is defined and values provided for
e and f .
Then the function is evaluated using e , f , and 100
as the parameter values.
|
|
The Create a table option creates a table with x automatically incremented from 0 to 5. A function
tree is created for each of the 4 functions which are evaluated in order. As shown in the example,
b in
f3(x) uses the value of a calculated by f2(x) and f4(x)
uses that value in its calculation. The columns could
be labeled "x", "f1(x)", "f2(x)", "f3(x)", and "f4(x)".
|
In the "Use variables t, x, y without '='" mode, the demo uses 4 instances of ExprEvaluator - one for each of t, x, y, and infix. That means that it is possible to use infix expressions for t, x, and y. This allows several interesting uses. Here are some examples:
t | x | y | Infix | Comment |
---|---|---|---|---|
t + 2 | 2 * t | 2 * x | 2 * y | Adds 2 to the old value of t, sets x to twice the value of t, sets y to twice the value of x and then calculates twice the value of y. For example, if the old value of t was 0, the new t would be 2, x becomes 4, y becomes 8 and the value of the infix expression is 16. |
sqrt(3^2 + 4^2) | Evaluates the hypotenuse of a right triangle with sides 3 and 4. | 3 | 4 | sqrt(x^2 + y^2) | Evaluates the hypotenuse of a right triangle with sides x and y. |
3 | (4/3)*x | sqrt(x^2 + y^2) | Evaluates the hypotenuse of a right triangle with sides x and (4/3)x. | |
0 | (x-4)/(x-5) | Evaluates (x-4)/(x-5) when x = 0. | ||
x+.1 | (x-4)/(x-5) | Evaluates (x-4)/(x-5) with x increasing by .1 each time the expressions are evaluated. | ||
30 | cos(toRadians(t)) | sin(toRadians(t)) | Calculates the x and y coordinates of the point on the unit circle at the angle of 30 degrees. | |
t + 10 | cos(toRadians(t)) | sin(toRadians(t)) | sin(2*toRadians(t))^2 | Calculates the x, y, and z coordinates of the point above the unit circle with angle t increasing by 10 degrees each time the expressions are evaluated. The z value is [sin(2t)]2 |
In the "Use variables a, b, ... z with '='" mode, the demo uses only 1 instances of ExprEvaluator . Here are a few examples:
1st | 2nd | 3rd | Infix | Comment |
---|---|---|---|---|
a = 3 | b = 4 | sqrt(a^2 + b^2) | Evaluates the hypotenuse of a right triangle with sides a and b. | |
a = 3 | b = 4 | c = 2 | Evaluate a polynomial with 3 constant parameters. This is a two part example. First assign values to a, b, and c. Then follow up with assigning values to x and y. Can be repeated with new values of x and y. | |
x = 3 | y = 4 | a*x^2 + b*x*y + c*y^2 |
In the case of illegal calculations such as 1/0, log(-1), or sqrt(-1), the result will typically be infinity, -infinity, or NaN (Not a Number).
ExprEvaluator originally was developed for a Data Structures class that demonstrated stacks and binary trees. It only operated with numbers and the binary operators +, -, *, /. It could build binary expression trees from both infix and postfix expressions. This version expands it by allowing the ^ operator, functions, unary operators + and -, variables a, b, c, ..., z, as well as constants PI and E.
ExprEvaluator can be used in nearly any program where it is desirable to allow the user to input an prefix, infix or postfix expression that needs to be evaluated. The class was designed to be very easy to use as illustrated by TrivialExample1.java. Suppose that the infix expression is contained in the String expr and the values of t, x, and y are stored in tVal, xVal, and yVal. All that is needed to evaluate the expression into a variable exprVal are these lines of code:
ExprEvaluator evaluator = new ExprEvaluator(); ... try { evaluator.buildFromInfix(infix); exprVal = evaluator.evaluate(tVal, xVal, yVal); }catch (Exception e) { System.out.println ("Error: namely: " + e); }
When it is desired to evaluate several different functions the following code style could be used:
ExprEvaluator evaluator = new ExprEvaluator(); ExprTree [ ] tree = new ExprTree[3]; String [ ] expr = new String[3]; double x; double [ ] val = new double[3]; expr[0] = ... expr[1] = ... expr[2] = ... ... try { for (int i = 0; i < 3; i++) tree[i] = evaluator.buildFromInfix(expr[i]); for (x = ...; x < ...; ...) setX(x); for (i = 0; i < 3; i++) val[i] = evaluator.evaluate(tree[i]); ... } }catch (Exception e) { System.out.println ("Error: namely: " + e); }
If the expression is prefix or postfix instead of infix, the only change would be to be to replace "buildFromInfix" by "buildFromPrefix" or "buildFromPostfix".
For additional information, please look at the Javadoc files for ExprEvaluator.
Down load the beta copy as a .zip file. Included files:
javac -classpath .;ExprEvaluator.jar Example.java java -classpath .;ExprEvaluator.jar Exampleto compile and execute the application Example using the ExprEvaluator package even though Example.class is not in the ExprEvaluator.jar file. The jar file is speeds loading of applets in web pages. For example, this page contains the tags:
<applet code = "InfixEvalDemo.class" archive = "ExprEvaluator.jar" width= "530" height= "630"> </applet>
The class requires Java 1.5 or higher
Please send any comments, corrections, questions, or suggestions to James Brink brinkje@plu.edu.
Copyright James Brink, 2008, 2013. Permission to use the package for non-commercial use is hereby granted.
Revised 5/17/13