| java.lang.Object edu.hws.jcm.data.ExpressionProgram
ExpressionProgram | public class ExpressionProgram implements Expression(Code) | | An ExprssionProgram represents a mathematical expression such as "3" or "sin(x^2)", stored
in the form of a program for a stack machine. The program consists of a sequence of
commands that, when executed, will compute the value of the expression.
Each command is encoded as an integer. There are three types of commands that can
occur: (1) A negative integer must be one of the 36 constant values PLUS, MINUS,..., CUBERT.
These constants represent unary and binary operators and standard functions. (2) An integer in the range
0 <= n < 0x3FFFFFFF encodes an operation of the form "push a constant onto the stack".
The constant that is being pushed is encoded as an index in the array "constant",
which is a private member of this class that holds all the constants that
occur in this ExpressionProgram. (3) An integer >= 0x3FFFFFFF represents
an ExpressionCommand object. When 0x3FFFFFFF is subtracted from the integer, the
result is an index into the array "command", which is a private member of this
class that holds all the ExpressionCommands that occur in this
ExpressionProgram.
|
Constructor Summary | |
public | ExpressionProgram() Default constructor creates an initially empty program. |
Method Summary | |
public void | addCommand(int code) Add a command code to the program, where code is one of the opCode constants
that are public final members of this class, from CUBERT to PLUS. | public void | addCommandObject(ExpressionCommand com) Adds com as the next command in the program. | public void | addConstant(double d) Add the number d as the next command in the program. | public void | appendOutputString(int index, StringBuffer buffer) Add a string representing part of the expression to the output buffer.
You probably are only interested in this if you write a ParserExtension
or ExpressionCommand.
(The command at position index in the program represents a
subexpression. | protected double | applyCommandCode(int code) Apply the stack operation represented by code (a number < 0) to the stack. | public void | compileDerivative(int index, ExpressionProgram deriv, Variable wrt) The command at position index in the program represents a subexpression of
the whole expression. | public void | copyExpression(int index, ExpressionProgram destination) The command at position index in the program represents a subexpression of
the whole expression. | public boolean | dependsOn(int index, Variable x) The command at position index in the program represents a subexpression of
the whole expression. | public boolean | dependsOn(Variable x) Checks whether the expression as a whole has any dependence on the variable x. | public Expression | derivative(Variable wrt) Compute the derivative of this expression with respect to the Variable wrt. | public int | extent(int index) The command at position index in the program represents a subexpression of
the whole expression. | public synchronized double | getVal() Run the ExprssionProgram and return the value that it computes. | public synchronized double | getValueWithCases(Cases c) Run the ExprssionProgram and return the value that it computes.
If the Cases object, c, is non-null, then information about "cases" is recorded in c.
This information can be used to help detect possible "discontinuities"
between two evaluations. | public String | toString() If a source string has been saved, use it as the print string. | public void | trim() To save space, cut the arrays that holds the program data down to the actual
amount of data that they contain. |
PLUSMINUSTIMESDIVIDEPOWEREQNELTGTLEGEANDORNOTUNARY_MINUSFACTORIALSINCOSTANCOTSECCSCARCSINARCCOSARCTANABSSQRTEXPLNLOG2LOG10TRUNCROUNDFLOORCEILINGCUBERT | final public static int PLUSMINUSTIMESDIVIDEPOWEREQNELTGTLEGEANDORNOTUNARY_MINUSFACTORIALSINCOSTANCOTSECCSCARCSINARCCOSARCTANABSSQRTEXPLNLOG2LOG10TRUNCROUNDFLOORCEILINGCUBERT(Code) | | Code for a unary or binary operator or a standard function.
|
sourceString | public String sourceString(Code) | | If this is non-null, it is used as the print string
for this expression in the toString() method. (When an
expression is created by a Parser by parsing a string,
the parse stores that string in this variable.)
|
ExpressionProgram | public ExpressionProgram()(Code) | | Default constructor creates an initially empty program.
|
addCommand | public void addCommand(int code)(Code) | | Add a command code to the program, where code is one of the opCode constants
that are public final members of this class, from CUBERT to PLUS. Each code
represents either a binary or unary operator or a standard function that operates on the stack by
poping its argument(s) from the stack, perfroming the operation, and pushing
the result back onto the stack.
|
addCommandObject | public void addCommandObject(ExpressionCommand com)(Code) | | Adds com as the next command in the program. Among other things, for example, com can
be a Variable or Constant. In that case, the meaning of the command is the stack
operation "push (value of com)".
Parameters: com - added as next command in the program. |
addConstant | public void addConstant(double d)(Code) | | Add the number d as the next command in the program. The meaning of this command is
actually the stack operation "push d".
Parameters: d - added as next command in program. |
appendOutputString | public void appendOutputString(int index, StringBuffer buffer)(Code) | | Add a string representing part of the expression to the output buffer.
You probably are only interested in this if you write a ParserExtension
or ExpressionCommand.
(The command at position index in the program represents a
subexpression. It could be a constant or a variable, for example,
which is complete subexpression in itself. Or it could be the
final operator in a larger subexpression. In that case, the operands,
which are located in lower positions in the program, are considered to
be part of the expression. This routine appends a print string
for the entire subexpression to the buffer. When this is called
with index = progCt-1 (the last command in the program), it processes
the entire program.
Note that the hard part here is deciding when to put in parentheses.
This is done based on the precedence of the operators. The result is not always pretty.
|
applyCommandCode | protected double applyCommandCode(int code)(Code) | | Apply the stack operation represented by code (a number < 0) to the stack.
|
compileDerivative | public void compileDerivative(int index, ExpressionProgram deriv, Variable wrt)(Code) | | The command at position index in the program represents a subexpression of
the whole expression. This routine adds commands to deriv for computing the
derivative of that subexpression with respect to the variable wrt.
You probably are not interested in this unless you write a ParserExtension
or an ExpressionCommand.
|
copyExpression | public void copyExpression(int index, ExpressionProgram destination)(Code) | | The command at position index in the program represents a subexpression of
the whole expression. This routine copies the commands for the entire
subexpression to the destination program.
You probably are not interested in this unless you write a ParserExtension
or an ExpressionCommand.
|
dependsOn | public boolean dependsOn(int index, Variable x)(Code) | | The command at position index in the program represents a subexpression of
the whole expression. If that subexpression includes some dependence on
the variable x, then true is returned. If the subexpression is constant
with respect to the variable x, then false is returned.
You probably are not interested in this unless you write a ParserExtension
or an ExpressionCommand.
|
dependsOn | public boolean dependsOn(Variable x)(Code) | | Checks whether the expression as a whole has any dependence on the variable x.
|
derivative | public Expression derivative(Variable wrt)(Code) | | Compute the derivative of this expression with respect to the Variable wrt.
The value returned is actually an ExpressionProgram.
|
extent | public int extent(int index)(Code) | | The command at position index in the program represents a subexpression of
the whole expression. This routine finds and returns the number of commands
in the program that are part of that subexpression. That is, the subexpresssion
occupies the part of the program between index - extent + 1 and index.
You probably are not interested in this unless you write a ParserExtension
or an ExpressionCommand.
|
getVal | public synchronized double getVal()(Code) | | Run the ExprssionProgram and return the value that it computes.
|
getValueWithCases | public synchronized double getValueWithCases(Cases c)(Code) | | Run the ExprssionProgram and return the value that it computes.
If the Cases object, c, is non-null, then information about "cases" is recorded in c.
This information can be used to help detect possible "discontinuities"
between two evaluations. See the Cases class for more information.
|
toString | public String toString()(Code) | | If a source string has been saved, use it as the print string. (When a Parser creates
an expression by parsing a string, it saves the source string in the ExpressionProgram.)
Otherwise, construct the print string based on the commands in the program.
|
trim | public void trim()(Code) | | To save space, cut the arrays that holds the program data down to the actual
amount of data that they contain. This should be called after the complete
program has been generated.
|
|
|