Package edu.hws.jcm.data

# Class ExpressionProgram

java.lang.Object
edu.hws.jcm.data.ExpressionProgram

All Implemented Interfaces:
  `Expression`, `Value`, `Serializable`

public class ExpressionProgram extends 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 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:
  - Serialized Form

## Field Summary

Fields
Modifier and Type
Field
Description
`static final int`
`ABS`
Code for a unary or binary operator or a standard function.
`static final int`
`AND`
Code for a unary or binary operator or a standard function.
`static final int`
`ARCCOS`
Code for a unary or binary operator or a standard function.
`static final int`
`ARCSIN`
Code for a unary or binary operator or a standard function.
`static final int`
`ARCTAN`
Code for a unary or binary operator or a standard function.
`static final int`
`CEILING`
Code for a unary or binary operator or a standard function.
`static final int`
`COS`
Code for a unary or binary operator or a standard function.
`static final int`
`COT`
Code for a unary or binary operator or a standard function.
`static final int`
`CSC`
Code for a unary or binary operator or a standard function.
`static final int`
`CUBERT`
Code for a unary or binary operator or a standard function.
`static final int`
`DIVIDE`
Code for a unary or binary operator or a standard function.
`static final int`
`EQ`
Code for a unary or binary operator or a standard function.
`static final int`
`EXP`
Code for a unary or binary operator or a standard function.
`static final int`
`FACTORIAL`
Code for a unary or binary operator or a standard function.
`static final int`
`FLOOR`
Code for a unary or binary operator or a standard function.
`static final int`
`GE`
Code for a unary or binary operator or a standard function.
`static final int`
`GT`
Code for a unary or binary operator or a standard function.
`static final int`
`LE`
Code for a unary or binary operator or a standard function.
`static final int`
`LN`
Code for a unary or binary operator or a standard function.
`static final int`
`LOG10`
Code for a unary or binary operator or a standard function.
`static final int`
`LOG2`
Code for a unary or binary operator or a standard function.
`static final int`
`LT`
Code for a unary or binary operator or a standard function.
`static final int`
`MINUS`
Code for a unary or binary operator or a standard function.
`static final int`
`NE`
Code for a unary or binary operator or a standard function.
`static final int`
`NOT`
Code for a unary or binary operator or a standard function.
`static final int`
`OR`
Code for a unary or binary operator or a standard function.
`static final int`
`PLUS`
Code for a unary or binary operator or a standard function.
`static final int`
`POWER`
Code for a unary or binary operator or a standard function.
`static final int`
`ROUND`
Code for a unary or binary operator or a standard function.
`static final int`
`SEC`
Code for a unary or binary operator or a standard function.
`static final int`
`SIN`
Code for a unary or binary operator or a standard function.
`String`
`sourceString`
If this is non-null, it is used as the print string for this expression in the toString() method.
`static final int`
`SQRT`
Code for a unary or binary operator or a standard function.
`static final int`
`TAN`
Code for a unary or binary operator or a standard function.
`static final int`
`TIMES`
Code for a unary or binary operator or a standard function.
`static final int`
`TRUNC`
Code for a unary or binary operator or a standard function.
`static final int`
`UNARY_MINUS`
Code for a unary or binary operator or a standard function.

## Constructor Summary

Constructors
Constructor
Description
`ExpressionProgram()`
Default constructor creates an initially empty program.

## Method Summary

Modifier and Type
Method
Description
`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, 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`
`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.
`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 Details

### PLUS

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

See Also:
  - Constant Field Values

### MINUS

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

See Also:
  - Constant Field Values

### TIMES

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

See Also:
  - Constant Field Values

### DIVIDE

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

See Also:
  - Constant Field Values

### POWER

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

See Also:
  - Constant Field Values

### EQ

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

See Also:
  - Constant Field Values

### NE

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

See Also:
  - Constant Field Values

### LT

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

See Also:
  - Constant Field Values

### GT

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

See Also:
  - Constant Field Values

### LE

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

See Also:
  - Constant Field Values

### GE

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

See Also:
  - Constant Field Values

### AND

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

See Also:
  - Constant Field Values

### OR

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

See Also:
  - Constant Field Values

### NOT

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

See Also:
  - Constant Field Values

### UNARY_MINUS

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

See Also:
  - Constant Field Values

### FACTORIAL

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

See Also:
  - Constant Field Values

### SIN

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

See Also:
  - Constant Field Values

### COS

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

See Also:
  - Constant Field Values

### TAN

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

See Also:
  - Constant Field Values

### COT

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

See Also:
  - Constant Field Values

### SEC

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

See Also:
  - Constant Field Values

### CSC

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

See Also:
  - Constant Field Values

### ARCSIN

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

See Also:
  - Constant Field Values

### ARCCOS

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

See Also:
  - Constant Field Values

### ARCTAN

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

See Also:
  - Constant Field Values

### ABS

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

See Also:
  - Constant Field Values

### SQRT

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

See Also:
  - Constant Field Values

### EXP

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

See Also:
  - Constant Field Values

### LN

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

See Also:
  - Constant Field Values

### LOG2

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

See Also:
  - Constant Field Values

### LOG10

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

See Also:
  - Constant Field Values

### TRUNC

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

See Also:
  - Constant Field Values

### ROUND

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

See Also:
  - Constant Field Values

### FLOOR

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

See Also:
  - Constant Field Values

### CEILING

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

See Also:
  - Constant Field Values

### CUBERT

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

See Also:
  - Constant Field Values

### sourceString

public 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 Details

### ExpressionProgram

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

## Method Details

### 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 invalid input: '<' 0) to the stack.

### toString

public 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 `Object`

### appendOutputString

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. 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`