Class ExpressionProgram
- All Implemented Interfaces:
Expression
,Value
,Serializable
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 invalid input: '<'= n invalid input: '<' 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.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function.If this is non-null, it is used as the print string for this expression in the toString() method.static final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function.static final int
Code for a unary or binary operator or a standard function. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
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.void
Adds com as the next command in the program.void
addConstant
(double d) Add the number d as the next command in the program.void
appendOutputString
(int index, StringBuffer buffer) Add a string representing part of the expression to the output buffer.protected double
applyCommandCode
(int code) Apply the stack operation represented by code (a number invalid input: '<' 0) to the stack.void
compileDerivative
(int index, ExpressionProgram deriv, Variable wrt) The command at position index in the program represents a subexpression of the whole expression.void
copyExpression
(int index, ExpressionProgram destination) The command at position index in the program represents a subexpression of the whole expression.boolean
The command at position index in the program represents a subexpression of the whole expression.boolean
Checks whether the expression as a whole has any dependence on the variable x.derivative
(Variable wrt) Compute the derivative of this expression with respect to the Variable wrt.int
extent
(int index) The command at position index in the program represents a subexpression of the whole expression.double
getVal()
Run the ExprssionProgram and return the value that it computes.double
Run the ExprssionProgram and return the value that it computes.toString()
If a source string has been saved, use it as the print string.void
trim()
To save space, cut the arrays that holds the program data down to the actual amount of data that they contain.
-
Field Details
-
PLUS
public static final int PLUSCode for a unary or binary operator or a standard function.- See Also:
-
MINUS
public static final int MINUSCode for a unary or binary operator or a standard function.- See Also:
-
TIMES
public static final int TIMESCode for a unary or binary operator or a standard function.- See Also:
-
DIVIDE
public static final int DIVIDECode for a unary or binary operator or a standard function.- See Also:
-
POWER
public static final int POWERCode for a unary or binary operator or a standard function.- See Also:
-
EQ
public static final int EQCode for a unary or binary operator or a standard function.- See Also:
-
NE
public static final int NECode for a unary or binary operator or a standard function.- See Also:
-
LT
public static final int LTCode for a unary or binary operator or a standard function.- See Also:
-
GT
public static final int GTCode for a unary or binary operator or a standard function.- See Also:
-
LE
public static final int LECode for a unary or binary operator or a standard function.- See Also:
-
GE
public static final int GECode for a unary or binary operator or a standard function.- See Also:
-
AND
public static final int ANDCode for a unary or binary operator or a standard function.- See Also:
-
OR
public static final int ORCode for a unary or binary operator or a standard function.- See Also:
-
NOT
public static final int NOTCode for a unary or binary operator or a standard function.- See Also:
-
UNARY_MINUS
public static final int UNARY_MINUSCode for a unary or binary operator or a standard function.- See Also:
-
FACTORIAL
public static final int FACTORIALCode for a unary or binary operator or a standard function.- See Also:
-
SIN
public static final int SINCode for a unary or binary operator or a standard function.- See Also:
-
COS
public static final int COSCode for a unary or binary operator or a standard function.- See Also:
-
TAN
public static final int TANCode for a unary or binary operator or a standard function.- See Also:
-
COT
public static final int COTCode for a unary or binary operator or a standard function.- See Also:
-
SEC
public static final int SECCode for a unary or binary operator or a standard function.- See Also:
-
CSC
public static final int CSCCode for a unary or binary operator or a standard function.- See Also:
-
ARCSIN
public static final int ARCSINCode for a unary or binary operator or a standard function.- See Also:
-
ARCCOS
public static final int ARCCOSCode for a unary or binary operator or a standard function.- See Also:
-
ARCTAN
public static final int ARCTANCode for a unary or binary operator or a standard function.- See Also:
-
ABS
public static final int ABSCode for a unary or binary operator or a standard function.- See Also:
-
SQRT
public static final int SQRTCode for a unary or binary operator or a standard function.- See Also:
-
EXP
public static final int EXPCode for a unary or binary operator or a standard function.- See Also:
-
LN
public static final int LNCode for a unary or binary operator or a standard function.- See Also:
-
LOG2
public static final int LOG2Code for a unary or binary operator or a standard function.- See Also:
-
LOG10
public static final int LOG10Code for a unary or binary operator or a standard function.- See Also:
-
TRUNC
public static final int TRUNCCode for a unary or binary operator or a standard function.- See Also:
-
ROUND
public static final int ROUNDCode for a unary or binary operator or a standard function.- See Also:
-
FLOOR
public static final int FLOORCode for a unary or binary operator or a standard function.- See Also:
-
CEILING
public static final int CEILINGCode for a unary or binary operator or a standard function.- See Also:
-
CUBERT
public static final int CUBERTCode for a unary or binary operator or a standard function.- See Also:
-
sourceString
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.)
-
-
Constructor Details
-
ExpressionProgram
public ExpressionProgram()Default constructor creates an initially empty program.
-
-
Method Details
-
addCommandObject
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) 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.
-
addCommand
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. 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. -
trim
public void trim()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. -
getVal
public double getVal()Run the ExprssionProgram and return the value that it computes. -
getValueWithCases
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.- Specified by:
getValueWithCases
in interfaceExpression
-
applyCommandCode
protected double applyCommandCode(int code) Apply the stack operation represented by code (a number invalid input: '<' 0) to the stack. -
toString
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.- Specified by:
toString
in interfaceExpression
- Overrides:
toString
in classObject
-
appendOutputString
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. -
derivative
Compute the derivative of this expression with respect to the Variable wrt. The value returned is actually an ExpressionProgram.- Specified by:
derivative
in interfaceExpression
-
compileDerivative
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. -
extent
public int extent(int index) 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. -
copyExpression
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
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
Checks whether the expression as a whole has any dependence on the variable x.- Specified by:
dependsOn
in interfaceExpression
-