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 Variable is a Value object whose value can be changed. Usually, a Variable will have
027: * a name, although that is not required unless tha Variable is going to be
028: * registered with a Parser. A Variable can be used as a Value, an Expression,
029: * or an ExpressionCommand. Since it is an ExpressionCommand, it can occur
030: * as a command in an ExpressionProgram. In that case, it simply represents a variable
031: * that occurs as part of an expression.
032: * <p>
033: * This class implements the Expression, ExpressionCommand, MathObject,
034: * and Value interfaces (since Constant implements them).
035: * <p>
036: * Most methods in interfaces Value, Exprssion, ExpressionCommand, and MathObject
037: * are inherited from class Constant. The following four methods override
038: * methods inherited from that class:
039: * public Expression derivative(Variable wrt);
040: * public void compileDerivative(ExpressionProgram prog, int myIndex, ExpressionProgram deriv, Variable wrt);
041: * public boolean dependsOn(Variable x); and
042: * public String toString().
043: */
044: public class Variable extends Constant {
045:
046: /**
047: * Create an unnamed Variable with initial value 0.
048: */
049: public Variable() {
050: super (0);
051: }
052:
053: /**
054: * Create a Variable with the given name and with initial value zero.
055: * (The name can be null.)
056: */
057: public Variable(String name) {
058: super (name, 0);
059: }
060:
061: /**
062: * Create a Variable with the given name and given initial value.
063: * (The name can be null.)
064: */
065: public Variable(String name, double value) {
066: super (name, value);
067: }
068:
069: /**
070: * Set the value of this Variable to the specified value.
071: */
072: public void setVal(double value) {
073: this .value = value;
074: }
075:
076: /**
077: * Return the derivative of this Variable with respect to the
078: * Variable wrt. The answer is 1 if wrt is this Variable.
079: * Otherwise, the answer is 0.
080: * @param wrt "with respect to", i.e., the variable with respect to which to
081: * take the derivative.
082: * @return a constant: 1 if wrt is this Variable, 0 otherwise.
083: */
084: public Expression derivative(Variable wrt) {
085: return new Constant((wrt == this ) ? 1 : 0);
086: }
087:
088: /**
089: * Add a command to deriv to evaluate the derivative of this Variable with respect to the
090: * Variable wrt. The derivative is a command for pushing either 1 or 0, depending on whether
091: * wrt is this Variable or some other Variable. This is not meant to be called directly.
092: */
093: public void compileDerivative(ExpressionProgram prog, int myIndex,
094: ExpressionProgram deriv, Variable wrt) {
095: deriv.addConstant((wrt == this ) ? 1 : 0);
096: }
097:
098: /**
099: * Check whether the value of this variable depends on the value of x. This
100: * is true if x is this Variable, false otherwise.
101: */
102: public boolean dependsOn(Variable x) {
103: return this == x;
104: }
105:
106: /**
107: * Return a print string representing this variable. The string is the
108: * name of the variable, if it has one. If not, the string "(unnamed variable)"
109: */
110: public String toString() {
111: String s = getName();
112: return (s == null) ? "(unnamed variable)" : s;
113: }
114:
115: } // end class Variable
|