Ejemplo n.º 1
0
 /**
  * Create a differention rule for function with 2 arguments. The rules must be in terms of "x" and
  * "y"
  *
  * @param inName name of function
  * @param inPfmc PostfixMathCommandI for function
  * @param rule1 a string represention differation of function wrt "x"
  * @param rule2 a string represention differation of function wrt "y"
  * @throws ParseException
  */
 private MacroDiffRules(
     DJep djep, String inName, PostfixMathCommandI inPfmc, String rule1, String rule2)
     throws ParseException {
   name = inName;
   pfmc = inPfmc;
   if (pfmc != null) {
     int nParam = pfmc.getNumberOfParameters();
     if (nParam != 2) {
       throw new ParseException(
           "Number of rules must match number of parameters for "
               + inName
               + " which is "
               + nParam);
     }
   }
   XSymbolTable localSymTab =
       (XSymbolTable) ((XSymbolTable) djep.getSymbolTable()).newInstance(); // new
   // SymbolTable();
   localSymTab.copyConstants(djep.getSymbolTable());
   XJep localJep = djep.newInstance(localSymTab);
   Node node1 = localJep.parse(rule1);
   Node node2 = localJep.parse(rule2);
   rules = new Node[2];
   rules[0] = node1;
   rules[1] = node2;
 }
Ejemplo n.º 2
0
  public Node differentiate(
      ASTFunNode node, String var, Node[] children, Node[] dchildren, DJep djep)
      throws ParseException {
    OperatorSet opset = djep.getOperatorSet();
    NodeFactory nf = djep.getNodeFactory();
    Operator op = opset.getMultiply();
    if (mulOp != null) op = mulOp;

    int nchild = node.jjtGetNumChildren();
    if (nchild == 2)
      return nf.buildOperatorNode(
          opset.getAdd(),
          nf.buildOperatorNode(op, dchildren[0], djep.deepCopy(children[1])),
          nf.buildOperatorNode(op, djep.deepCopy(children[0]), dchildren[1]));

    Node sums[] = new Node[nchild];
    for (int i = 0; i < nchild; ++i) {
      Node terms[] = new Node[nchild];
      for (int j = 0; j < nchild; ++j) terms[j] = children[j];
      terms[i] = dchildren[i];
      sums[i] = nf.buildOperatorNode(op, terms);
    }
    Node res = nf.buildOperatorNode(opset.getAdd(), sums);
    return res;

    // throw new ParseException("Too many children "+nchild+" for "+node+"\n");
  }
Ejemplo n.º 3
0
 protected void setUp() {
   j = new DJep();
   j.addStandardConstants();
   j.addStandardFunctions();
   j.addComplex();
   // j.setTraverse(true);
   j.setAllowAssignment(true);
   j.setAllowUndeclared(true);
   j.setImplicitMul(true);
   ((DJep) j).addStandardDiffRules();
 }
Ejemplo n.º 4
0
  /**
   * Create a differentation rule for function with n arguments. The rules must be in terms of "x1",
   * "x2", ... "xn"
   *
   * @param inName name of function
   * @param inPfmc PostfixMathCommandI for function
   * @throws ParseException
   */
  private MacroDiffRules(DJep djep, String inName, PostfixMathCommandI inPfmc, String[] inRules)
      throws ParseException {
    name = inName;
    pfmc = inPfmc;
    if (pfmc != null) {
      int nParam = pfmc.getNumberOfParameters();
      if (nParam != inRules.length) {
        throw new ParseException(
            "Number of rules must match number of parameters for "
                + inName
                + " which is "
                + nParam);
      }
    }

    XSymbolTable localSymTab = (XSymbolTable) ((XSymbolTable) djep.getSymbolTable()).newInstance();
    localSymTab.copyConstants(djep.getSymbolTable());
    XJep localJep = djep.newInstance(localSymTab);

    rules = new Node[inRules.length];
    for (int i = 0; i < inRules.length; ++i) {
      rules[i] = localJep.parse(inRules[i]);
    }
  }
Ejemplo n.º 5
0
 /**
  * Create a differention rule for function with 1 argument
  *
  * @param inName name of function
  * @param node a tree represention differation of function wrt "x"
  * @throws ParseException
  */
 public MacroDiffRules(DJep djep, String inName, Node node) throws ParseException {
   name = inName;
   pfmc = djep.getFunctionTable().get(inName);
   if (pfmc != null) {
     int nParam = pfmc.getNumberOfParameters();
     if (nParam != 1) {
       throw new ParseException(
           "Number of rules must match number of parameters for "
               + inName
               + " which is "
               + nParam);
     }
   }
   rules = new Node[1];
   rules[0] = node;
 }
Ejemplo n.º 6
0
  /**
   * differentiate the expression and simplify
   *
   * @param var variable used for differentiation
   */
  public void diff(String var) {
    String name = proxy.getName();

    DJep j = new DJep();
    j.addStandardConstants();
    j.addStandardFunctions();
    j.addComplex();
    j.setAllowUndeclared(true);
    j.setAllowAssignment(true);
    j.setImplicitMul(true);
    // Sets up standard rules for differentiating sin(x) etc.
    j.addStandardDiffRules();

    try {
      // parse the string
      node = j.parse(name);
      diff = j.differentiate(node, var);
      simp = j.simplify(diff);
    } catch (ParseException e) {
      ErrorMessage("Error in parsing " + name);
    }
  }
Ejemplo n.º 7
0
 /**
  * Create a differention rule for function with 1 argument
  *
  * @param inName name of function
  * @param rule a string represention differation of a function wrt "x"
  * @throws ParseException
  */
 public MacroDiffRules(DJep djep, String inName, String rule) throws ParseException {
   this(djep, inName, djep.getFunctionTable().get(inName), rule);
 }