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 boolean dependsOn(Variable x) {
   // Return true if this command depends on the value of x, false otherwise.
   // That is, when apply() is called, can the result depend on the value of x?
   return sumExpr.dependsOn(x);
 }