Logo Search packages:      
Sourcecode: wims version File versions  Download package

Constant.java

/*************************************************************************
*                                                                        *
*   1) This source code file, in unmodified form, and compiled classes   *
*      derived from it can be used and distributed without restriction,  *
*      including for commercial use.  (Attribution is not required       *
*      but is appreciated.)                                              *
*                                                                        *
*    2) Modified versions of this file can be made and distributed       *
*       provided:  the modified versions are put into a Java package     *
*       different from the original package, edu.hws;  modified          *
*       versions are distributed under the same terms as the original;   *
*       and the modifications are documented in comments.  (Modification *
*       here does not include simply making subclasses that belong to    *
*       a package other than edu.hws, which can be done without any      *
*       restriction.)                                                    *
*                                                                        *
*   David J. Eck                                                         *
*   Department of Mathematics and Computer Science                       *
*   Hobart and William Smith Colleges                                    *
*   Geneva, New York 14456,   USA                                        *
*   Email: eck@hws.edu          WWW: http://math.hws.edu/eck/            *
*                                                                        *
*************************************************************************/

package edu.hws.jcm.data;

/**
 * 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.
 */
00039 public class Constant implements Expression, ExpressionCommand, MathObject {
               // Also implements Value, which is a subinterface of Expression.

   private String name;     // This Constant's name, possibly null.

   /**
    * The value of this Constant.   
    */
00047    protected double value;  
   
   /**
    * Create an unnamed Constant with the given value and null name.
    */
00052    public Constant(double value) {
      this.value = value;
   }
   
   /**
    * Create a Constant with the given name and value.
    * The name can be null.
    */
00060    public Constant(String name, double value) {
      setName(name);
      this.value = value;
   }
   
   // -------------------- Methods from the MathObject interface -------------------------
   
   /**
    * Return the name of this Constant.  It can be null.
    */
00070    public String getName() {
      return name;
   }

   /**   
    * Set the name of this Constant.  (Note that this should not be done
    * if the Constant has been registered with a Parser.)
    */
00078    public void setName(String name) {
      this.name = name;
   }
   

   // -------------- Method from the Value interface (inherited through Expression) ------
   
   /**
    * Return the value of this Constant.
    */
00088    public double getVal() {
      return value;
   }
   

   // ----------------------- Methods from the Expression interface ---------------------
   
   /**
    * 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.
    */
00099    public double getValueWithCases(Cases cases) {
      return value;
   }

   /**   
    * Return the derivative of this Constant with respect to the variable wrt.  
    * The derivative is another Constant with value zero.
    */
00107    public Expression derivative(Variable wrt) {
      return new Constant(0);
   }

   /**   
    * 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.
    */
00116    public String toString() {
      if (name == null)
         return  NumUtils.realToString(value);
      else
         return name;
   }
   

   // -------------------- Methods from the ExpressionCommand interface -----------------

   /**
    * 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.
    */
00131    public void apply(StackOfDouble stack, Cases cases) {
      stack.push(getVal());  // Feb 3, 2001 -- changed this from "stack.push(value)", which caused problems with sub-classes!
   }

   /**   
    * 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.
    */
00141    public void compileDerivative(ExpressionProgram prog, int myIndex, ExpressionProgram deriv, Variable wrt) {
      deriv.addConstant(0);
   }

   /**   
    * 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.
    */
00150    public int extent(ExpressionProgram prog, int myIndex) {
      return 1;
   }
   
   /**
    * Retrun false, since the value of this Constant is independent of the value of x.
    */
00157    public boolean dependsOn(Variable x) {
       return false;
   }
   
   /**
    * Append the print string for this Constant to the buffer.  (The values of prog and
    * myIndex are irrelevant.)
    */
00165    public void appendOutputString(ExpressionProgram prog, int myIndex, StringBuffer buffer) {
      buffer.append(toString());
   }
   
} // end class Constant


Generated by  Doxygen 1.6.0   Back to index