edu.hws.jcm.data
Class ExpressionProgram

java.lang.Object
  |
  +--edu.hws.jcm.data.ExpressionProgram
All Implemented Interfaces:
Expression, java.io.Serializable, Value

public class ExpressionProgram
extends java.lang.Object
implements Expression

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.

See Also:
Serialized Form

Field Summary
static int ABS
          Code for a unary or binary operator or a standard function.
static int AND
          Code for a unary or binary operator or a standard function.
static int ARCCOS
          Code for a unary or binary operator or a standard function.
static int ARCSIN
          Code for a unary or binary operator or a standard function.
static int ARCTAN
          Code for a unary or binary operator or a standard function.
static int CEILING
          Code for a unary or binary operator or a standard function.
static int COS
          Code for a unary or binary operator or a standard function.
static int COT
          Code for a unary or binary operator or a standard function.
static int CSC
          Code for a unary or binary operator or a standard function.
static int CUBERT
          Code for a unary or binary operator or a standard function.
static int DIVIDE
          Code for a unary or binary operator or a standard function.
static int EQ
          Code for a unary or binary operator or a standard function.
static int EXP
          Code for a unary or binary operator or a standard function.
static int FACTORIAL
          Code for a unary or binary operator or a standard function.
static int FLOOR
          Code for a unary or binary operator or a standard function.
static int GE
          Code for a unary or binary operator or a standard function.
static int GT
          Code for a unary or binary operator or a standard function.
static int LE
          Code for a unary or binary operator or a standard function.
static int LN
          Code for a unary or binary operator or a standard function.
static int LOG10
          Code for a unary or binary operator or a standard function.
static int LOG2
          Code for a unary or binary operator or a standard function.
static int LT
          Code for a unary or binary operator or a standard function.
static int MINUS
          Code for a unary or binary operator or a standard function.
static int NE
          Code for a unary or binary operator or a standard function.
static int NOT
          Code for a unary or binary operator or a standard function.
static int OR
          Code for a unary or binary operator or a standard function.
static int PLUS
          Code for a unary or binary operator or a standard function.
static int POWER
          Code for a unary or binary operator or a standard function.
static int ROUND
          Code for a unary or binary operator or a standard function.
static int SEC
          Code for a unary or binary operator or a standard function.
static int SIN
          Code for a unary or binary operator or a standard function.
 java.lang.String sourceString
          If this is non-null, it is used as the print string for this expression in the toString() method.
static int SQRT
          Code for a unary or binary operator or a standard function.
static int TAN
          Code for a unary or binary operator or a standard function.
static int TIMES
          Code for a unary or binary operator or a standard function.
static int TRUNC
          Code for a unary or binary operator or a standard function.
static int UNARY_MINUS
          Code for a unary or binary operator or a standard function.
 
Constructor Summary
ExpressionProgram()
          Default constructor creates an initially empty program.
 
Method Summary
 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.
 void addCommandObject(ExpressionCommand com)
          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, java.lang.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 < 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 dependsOn(int index, Variable x)
          The command at position index in the program represents a subexpression of the whole expression.
 boolean dependsOn(Variable x)
          Checks whether the expression as a whole has any dependence on the variable x.
 Expression 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 getValueWithCases(Cases c)
          Run the ExprssionProgram and return the value that it computes.
 java.lang.String 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

PLUS

public static final int PLUS
Code for a unary or binary operator or a standard function.

MINUS

public static final int MINUS
Code for a unary or binary operator or a standard function.

TIMES

public static final int TIMES
Code for a unary or binary operator or a standard function.

DIVIDE

public static final int DIVIDE
Code for a unary or binary operator or a standard function.

POWER

public static final int POWER
Code for a unary or binary operator or a standard function.

EQ

public static final int EQ
Code for a unary or binary operator or a standard function.

NE

public static final int NE
Code for a unary or binary operator or a standard function.

LT

public static final int LT
Code for a unary or binary operator or a standard function.

GT

public static final int GT
Code for a unary or binary operator or a standard function.

LE

public static final int LE
Code for a unary or binary operator or a standard function.

GE

public static final int GE
Code for a unary or binary operator or a standard function.

AND

public static final int AND
Code for a unary or binary operator or a standard function.

OR

public static final int OR
Code for a unary or binary operator or a standard function.

NOT

public static final int NOT
Code for a unary or binary operator or a standard function.

UNARY_MINUS

public static final int UNARY_MINUS
Code for a unary or binary operator or a standard function.

FACTORIAL

public static final int FACTORIAL
Code for a unary or binary operator or a standard function.

SIN

public static final int SIN
Code for a unary or binary operator or a standard function.

COS

public static final int COS
Code for a unary or binary operator or a standard function.

TAN

public static final int TAN
Code for a unary or binary operator or a standard function.

COT

public static final int COT
Code for a unary or binary operator or a standard function.

SEC

public static final int SEC
Code for a unary or binary operator or a standard function.

CSC

public static final int CSC
Code for a unary or binary operator or a standard function.

ARCSIN

public static final int ARCSIN
Code for a unary or binary operator or a standard function.

ARCCOS

public static final int ARCCOS
Code for a unary or binary operator or a standard function.

ARCTAN

public static final int ARCTAN
Code for a unary or binary operator or a standard function.

ABS

public static final int ABS
Code for a unary or binary operator or a standard function.

SQRT

public static final int SQRT
Code for a unary or binary operator or a standard function.

EXP

public static final int EXP
Code for a unary or binary operator or a standard function.

LN

public static final int LN
Code for a unary or binary operator or a standard function.

LOG2

public static final int LOG2
Code for a unary or binary operator or a standard function.

LOG10

public static final int LOG10
Code for a unary or binary operator or a standard function.

TRUNC

public static final int TRUNC
Code for a unary or binary operator or a standard function.

ROUND

public static final int ROUND
Code for a unary or binary operator or a standard function.

FLOOR

public static final int FLOOR
Code for a unary or binary operator or a standard function.

CEILING

public static final int CEILING
Code for a unary or binary operator or a standard function.

CUBERT

public static final int CUBERT
Code for a unary or binary operator or a standard function.

sourceString

public java.lang.String 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 Detail

ExpressionProgram

public ExpressionProgram()
Default constructor creates an initially empty program.
Method Detail

addCommandObject

public void addCommandObject(ExpressionCommand com)
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.
Specified by:
getVal in interface Value

getValueWithCases

public 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. See the Cases class for more information.
Specified by:
getValueWithCases in interface Expression

applyCommandCode

protected double applyCommandCode(int code)
Apply the stack operation represented by code (a number < 0) to the stack.

toString

public java.lang.String 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 interface Expression
Overrides:
toString in class java.lang.Object

appendOutputString

public void appendOutputString(int index,
                               java.lang.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. 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

public Expression derivative(Variable wrt)
Compute the derivative of this expression with respect to the Variable wrt. The value returned is actually an ExpressionProgram.
Specified by:
derivative in interface Expression

compileDerivative

public void compileDerivative(int index,
                              ExpressionProgram deriv,
                              Variable wrt)
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

public void copyExpression(int index,
                           ExpressionProgram destination)
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)
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)
Checks whether the expression as a whole has any dependence on the variable x.
Specified by:
dependsOn in interface Expression