001: /*************************************************************************
002: * *
003: * 1) This source code file, in unmodified form, and compiled classes *
004: * derived from it can be used and distributed without restriction, *
005: * including for commercial use. (Attribution is not required *
006: * but is appreciated.) *
007: * *
008: * 2) Modified versions of this file can be made and distributed *
009: * provided: the modified versions are put into a Java package *
010: * different from the original package, edu.hws; modified *
011: * versions are distributed under the same terms as the original; *
012: * and the modifications are documented in comments. (Modification *
013: * here does not include simply making subclasses that belong to *
014: * a package other than edu.hws, which can be done without any *
015: * restriction.) *
016: * *
017: * David J. Eck *
018: * Department of Mathematics and Computer Science *
019: * Hobart and William Smith Colleges *
020: * Geneva, New York 14456, USA *
021: * Email: eck@hws.edu WWW: http://math.hws.edu/eck/ *
022: * *
023: *************************************************************************/package edu.hws.jcm.data;
024:
025: /**
026: * A Constant is a Value that represents a constant real number. (The value doesn't have to
027: * be constant in sub-classes, since the member that stores the value is protected, not private.)
028: * A Constant doesn't necessarily need a name. If the name is null, then the print string for the
029: * Constant is the value of the constant. If it has a non-null name, then the print string
030: * is the name. (Note that, as for any MathObject, if the name is null, than the Constant can't
031: * be added to a Parser.) Constant objects are used to represent the mathematical constants
032: * pi and e.
033: * A Constant is both an Expression and an ExpressionCommand. Since it is an ExpressionCommand,
034: * it can occur as a command in an ExpressionProgram. In that case, it simply represens a named constant
035: * occurs in an expression.
036: */
037: public class Constant implements Expression, ExpressionCommand,
038: MathObject {
039: // Also implements Value, which is a subinterface of Expression.
040:
041: private String name; // This Constant's name, possibly null.
042:
043: /**
044: * The value of this Constant.
045: */
046: protected double value;
047:
048: /**
049: * Create an unnamed Constant with the given value and null name.
050: */
051: public Constant(double value) {
052: this .value = value;
053: }
054:
055: /**
056: * Create a Constant with the given name and value.
057: * The name can be null.
058: */
059: public Constant(String name, double value) {
060: setName(name);
061: this .value = value;
062: }
063:
064: // -------------------- Methods from the MathObject interface -------------------------
065:
066: /**
067: * Return the name of this Constant. It can be null.
068: */
069: public String getName() {
070: return name;
071: }
072:
073: /**
074: * Set the name of this Constant. (Note that this should not be done
075: * if the Constant has been registered with a Parser.)
076: */
077: public void setName(String name) {
078: this .name = name;
079: }
080:
081: // -------------- Method from the Value interface (inherited through Expression) ------
082:
083: /**
084: * Return the value of this Constant.
085: */
086: public double getVal() {
087: return value;
088: }
089:
090: // ----------------------- Methods from the Expression interface ---------------------
091:
092: /**
093: * Return the value of the Constant. Since a constant is continuous function,
094: * there is only one "case", so no case information needs to be recorded in cases.
095: */
096: public double getValueWithCases(Cases cases) {
097: return value;
098: }
099:
100: /**
101: * Return the derivative of this Constant with respect to the variable wrt.
102: * The derivative is another Constant with value zero.
103: */
104: public Expression derivative(Variable wrt) {
105: return new Constant(0);
106: }
107:
108: /**
109: * Return the print string representing this Constant. The string is the
110: * name of the constant, if that is non-null. Otherwise, it is the value
111: * of the constant.
112: */
113: public String toString() {
114: if (name == null)
115: return NumUtils.realToString(value);
116: else
117: return name;
118: }
119:
120: // -------------------- Methods from the ExpressionCommand interface -----------------
121:
122: /**
123: * Apply the Constant to the stack. This is done by pushing the value of
124: * the constant onto the stack. The evaluation of a constant doesn't have any
125: * "cases", so there is no need to record any information in cases.
126: */
127: public void apply(StackOfDouble stack, Cases cases) {
128: stack.push(getVal()); // Feb 3, 2001 -- changed this from "stack.push(value)", which caused problems with sub-classes!
129: }
130:
131: /**
132: * Add a commands to deriv to evaluate the derivative of this Constant with respect to the
133: * variable. The derivative is 0, so the only command is the constant 0 (which really
134: * represents the stack operation "push 0"). The program and the position of the Constant
135: * in that program are irrelevant.
136: */
137: public void compileDerivative(ExpressionProgram prog, int myIndex,
138: ExpressionProgram deriv, Variable wrt) {
139: deriv.addConstant(0);
140: }
141:
142: /**
143: * Return the number of locations that this Constant uses in the program.
144: * The value is always 1, since the constant is a complete sub-expression
145: * in itself.
146: */
147: public int extent(ExpressionProgram prog, int myIndex) {
148: return 1;
149: }
150:
151: /**
152: * Retrun false, since the value of this Constant is independent of the value of x.
153: */
154: public boolean dependsOn(Variable x) {
155: return false;
156: }
157:
158: /**
159: * Append the print string for this Constant to the buffer. (The values of prog and
160: * myIndex are irrelevant.)
161: */
162: public void appendOutputString(ExpressionProgram prog, int myIndex,
163: StringBuffer buffer) {
164: buffer.append(toString());
165: }
166:
167: } // end class Constant
|