Package edu.hws.jcm.data
Class Constant
java.lang.Object
edu.hws.jcm.data.Constant
- All Implemented Interfaces:
Expression
,ExpressionCommand
,MathObject
,Value
,Serializable
- Direct Known Subclasses:
Variable
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:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
appendOutputString
(ExpressionProgram prog, int myIndex, 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
Retrun false, since the value of this Constant is independent of the value of x.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.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
Set the name of this Constant.toString()
Return the print string representing this Constant.
-
Field Details
-
value
protected double valueThe value of this Constant.
-
-
Constructor Details
-
Constant
public Constant(double value) Create an unnamed Constant with the given value and null name. -
Constant
Create a Constant with the given name and value. The name can be null.
-
-
Method Details
-
getName
Return the name of this Constant. It can be null.- Specified by:
getName
in interfaceMathObject
-
setName
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
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
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
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 classObject
-
apply
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
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
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
Append the print string for this Constant to the buffer. (The values of prog and myIndex are irrelevant.)- Specified by:
appendOutputString
in interfaceExpressionCommand
-