public int extent(ExpressionProgram prog, int myIndex) {
   // The ExpressionCommand occurs in the program prog at the index indicated by myIndex.
   // Return the total number of indices in prog occupied by this command and the commands
   // that generate data used by this command.  In this case, that means the commands that
   // compute the upper and lower limits of the summatio, plus this Cmd object.
   int upper = prog.extent(myIndex - 1); // Extent of upper limit expression in prog.
   int lower = prog.extent(myIndex - 1 - upper); // Extent of lower limit expression in prog.
   return upper + lower + 1; // Upper + lower limits + this object.
 }
 public void compileDerivative(
     ExpressionProgram prog, int myIndex, ExpressionProgram deriv, Variable wrt) {
   // The ExpressionCommand occurs in the program prog at the index indicated by myIndex.
   // Add commands to deriv that will evaluate the derivative of this command with respect to
   // the variable wrt.  Note that the "Cmd" object is preceded in the program by commands that
   // compute the lower and upper limits of the summation.
   if (!sumExpr.dependsOn(wrt)) deriv.addConstant(0);
   else {
     int upper = prog.extent(myIndex - 1); // Size of expression giving the upper limit.
     prog.copyExpression(myIndex - 1 - upper, deriv); // Copy lower limit exression to deriv.
     prog.copyExpression(myIndex - 1, deriv); // Copy upper limit expression to deriv.
     deriv.addCommandObject(new Cmd(sumVar, (ExpressionProgram) sumExpr.derivative(wrt)));
   }
 }
 public void appendOutputString(ExpressionProgram prog, int myIndex, StringBuffer buffer) {
   // The ExpressionCommand occurs in the program prog at the index indicated by myIndex.
   // Add a print string representation of the sub-expression represented by this command
   // (including any previous commands in the program that generate data used by this
   // command).
   int upper = prog.extent(myIndex - 1);
   buffer.append("sum(");
   buffer.append(sumVar.getName());
   buffer.append(", ");
   prog.appendOutputString(myIndex - 1 - upper, buffer);
   buffer.append(", ");
   prog.appendOutputString(myIndex - 1, buffer);
   buffer.append(", ");
   buffer.append(sumExpr.toString());
   buffer.append(")");
 }