Package edu.hws.jcm.data
Class Constant
- java.lang.Object
-
- edu.hws.jcm.data.Constant
-
- All Implemented Interfaces:
Expression
,ExpressionCommand
,MathObject
,Value
,java.io.Serializable
- Direct Known Subclasses:
Variable
public class Constant extends java.lang.Object implements Expression, ExpressionCommand, MathObject
A Constant is a Value that represents a constant real number. (The value doesn't have to be constant in sub-classes, since the member that stores the value is protected, not private.) A Constant doesn't necessarily need a name. If the name is null, then the print string for the Constant is the value of the constant. If it has a non-null name, then the print string is the name. (Note that, as for any MathObject, if the name is null, than the Constant can't be added to a Parser.) Constant objects are used to represent the mathematical constants pi and e. A Constant is both an Expression and an ExpressionCommand. Since it is an ExpressionCommand, it can occur as a command in an ExpressionProgram. In that case, it simply represens a named constant occurs in an expression.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected double
value
The value of this Constant.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
appendOutputString(ExpressionProgram prog, int myIndex, java.lang.StringBuffer buffer)
Append the print string for this Constant to the buffer.void
apply(StackOfDouble stack, Cases cases)
Apply the Constant to the stack.void
compileDerivative(ExpressionProgram prog, int myIndex, ExpressionProgram deriv, Variable wrt)
Add a commands to deriv to evaluate the derivative of this Constant with respect to the variable.boolean
dependsOn(Variable x)
Retrun false, since the value of this Constant is independent of the value of x.Expression
derivative(Variable wrt)
Return the derivative of this Constant with respect to the variable wrt.int
extent(ExpressionProgram prog, int myIndex)
Return the number of locations that this Constant uses in the program.java.lang.String
getName()
Return the name of this Constant.double
getVal()
Return the value of this Constant.double
getValueWithCases(Cases cases)
Return the value of the Constant.void
setName(java.lang.String name)
Set the name of this Constant.java.lang.String
toString()
Return the print string representing this Constant.
-
-
-
Method Detail
-
getName
public java.lang.String getName()
Return the name of this Constant. It can be null.- Specified by:
getName
in interfaceMathObject
-
setName
public void setName(java.lang.String name)
Set the name of this Constant. (Note that this should not be done if the Constant has been registered with a Parser.)- Specified by:
setName
in interfaceMathObject
-
getVal
public double getVal()
Return the value of this Constant.
-
getValueWithCases
public double getValueWithCases(Cases cases)
Return the value of the Constant. Since a constant is continuous function, there is only one "case", so no case information needs to be recorded in cases.- Specified by:
getValueWithCases
in interfaceExpression
-
derivative
public Expression derivative(Variable wrt)
Return the derivative of this Constant with respect to the variable wrt. The derivative is another Constant with value zero.- Specified by:
derivative
in interfaceExpression
-
toString
public java.lang.String toString()
Return the print string representing this Constant. The string is the name of the constant, if that is non-null. Otherwise, it is the value of the constant.- Specified by:
toString
in interfaceExpression
- Overrides:
toString
in classjava.lang.Object
-
apply
public void apply(StackOfDouble stack, Cases cases)
Apply the Constant to the stack. This is done by pushing the value of the constant onto the stack. The evaluation of a constant doesn't have any "cases", so there is no need to record any information in cases.- Specified by:
apply
in interfaceExpressionCommand
- Parameters:
stack
- contains results of previous commands in the program.cases
- if non-null, any case information generated during evaluation should be recorded here.
-
compileDerivative
public void compileDerivative(ExpressionProgram prog, int myIndex, ExpressionProgram deriv, Variable wrt)
Add a commands to deriv to evaluate the derivative of this Constant with respect to the variable. The derivative is 0, so the only command is the constant 0 (which really represents the stack operation "push 0"). The program and the position of the Constant in that program are irrelevant.- Specified by:
compileDerivative
in interfaceExpressionCommand
- Parameters:
prog
- program in which ExpressionCommand occurs.myIndex
- point at which ExpressionCommand occurs in the ExpressionProgram.deriv
- the derivative of the ExpressionPorgram prog, which is in the process of being computed. Commands should added to deriv that will compute the derivative of this ExpressionCommand.wrt
- commands are added to deriv with respect to this Variable.
-
extent
public int extent(ExpressionProgram prog, int myIndex)
Return the number of locations that this Constant uses in the program. The value is always 1, since the constant is a complete sub-expression in itself.- Specified by:
extent
in interfaceExpressionCommand
- Parameters:
prog
- ExpressionProgram in which this ExpressionCommand occurs.myIndex
- index at which ExpressionCommand occurs in prog.- Returns:
- total number of indices in prog occupied by this command and commands that generate data used by this command.
-
dependsOn
public boolean dependsOn(Variable x)
Retrun false, since the value of this Constant is independent of the value of x.- Specified by:
dependsOn
in interfaceExpression
- Specified by:
dependsOn
in interfaceExpressionCommand
-
appendOutputString
public void appendOutputString(ExpressionProgram prog, int myIndex, java.lang.StringBuffer buffer)
Append the print string for this Constant to the buffer. (The values of prog and myIndex are irrelevant.)- Specified by:
appendOutputString
in interfaceExpressionCommand
-
-