Package edu.hws.jcm.data
Class Variable
- java.lang.Object
-
- edu.hws.jcm.data.Constant
-
- edu.hws.jcm.data.Variable
-
- All Implemented Interfaces:
Expression
,ExpressionCommand
,MathObject
,Value
,java.io.Serializable
public class Variable extends Constant
A Variable is a Value object whose value can be changed. Usually, a Variable will have a name, although that is not required unless tha Variable is going to be registered with a Parser. A Variable can be used as a Value, an Expression, or an ExpressionCommand. Since it is an ExpressionCommand, it can occur as a command in an ExpressionProgram. In that case, it simply represents a variable that occurs as part of an expression.This class implements the Expression, ExpressionCommand, MathObject, and Value interfaces (since Constant implements them).
Most methods in interfaces Value, Exprssion, ExpressionCommand, and MathObject are inherited from class Constant. The following four methods override methods inherited from that class: public Expression derivative(Variable wrt); public void compileDerivative(ExpressionProgram prog, int myIndex, ExpressionProgram deriv, Variable wrt); public boolean dependsOn(Variable x); and public String toString().
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description Variable()
Create an unnamed Variable with initial value 0.Variable(java.lang.String name)
Create a Variable with the given name and with initial value zero.Variable(java.lang.String name, double value)
Create a Variable with the given name and given initial value.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
compileDerivative(ExpressionProgram prog, int myIndex, ExpressionProgram deriv, Variable wrt)
Add a command to deriv to evaluate the derivative of this Variable with respect to the Variable wrt.boolean
dependsOn(Variable x)
Check whether the value of this variable depends on the value of x.Expression
derivative(Variable wrt)
Return the derivative of this Variable with respect to the Variable wrt.void
setVal(double value)
Set the value of this Variable to the specified value.java.lang.String
toString()
Return a print string representing this variable.-
Methods inherited from class edu.hws.jcm.data.Constant
appendOutputString, apply, extent, getName, getVal, getValueWithCases, setName
-
-
-
-
Constructor Detail
-
Variable
public Variable()
Create an unnamed Variable with initial value 0.
-
Variable
public Variable(java.lang.String name)
Create a Variable with the given name and with initial value zero. (The name can be null.)
-
Variable
public Variable(java.lang.String name, double value)
Create a Variable with the given name and given initial value. (The name can be null.)
-
-
Method Detail
-
setVal
public void setVal(double value)
Set the value of this Variable to the specified value.
-
derivative
public Expression derivative(Variable wrt)
Return the derivative of this Variable with respect to the Variable wrt. The answer is 1 if wrt is this Variable. Otherwise, the answer is 0.- Specified by:
derivative
in interfaceExpression
- Overrides:
derivative
in classConstant
- Parameters:
wrt
- "with respect to", i.e., the variable with respect to which to take the derivative.- Returns:
- a constant: 1 if wrt is this Variable, 0 otherwise.
-
compileDerivative
public void compileDerivative(ExpressionProgram prog, int myIndex, ExpressionProgram deriv, Variable wrt)
Add a command to deriv to evaluate the derivative of this Variable with respect to the Variable wrt. The derivative is a command for pushing either 1 or 0, depending on whether wrt is this Variable or some other Variable. This is not meant to be called directly.- Specified by:
compileDerivative
in interfaceExpressionCommand
- Overrides:
compileDerivative
in classConstant
- 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.
-
dependsOn
public boolean dependsOn(Variable x)
Check whether the value of this variable depends on the value of x. This is true if x is this Variable, false otherwise.- Specified by:
dependsOn
in interfaceExpression
- Specified by:
dependsOn
in interfaceExpressionCommand
- Overrides:
dependsOn
in classConstant
-
toString
public java.lang.String toString()
Return a print string representing this variable. The string is the name of the variable, if it has one. If not, the string "(unnamed variable)"- Specified by:
toString
in interfaceExpression
- Overrides:
toString
in classConstant
-
-