/**
   * Differentiates a variable. May want to alter behaviour when using multi equation as diff(f,x)
   * might not be zero.
   *
   * @return 1 if the variable has the same name as data, 0 if the variable has a different name.
   */
  public Object visit(ASTVarNode node, Object data) throws ParseException {
    String varName = (String) data;
    XVariable var = (XVariable) node.getVar();
    PartialDerivative deriv = null;
    if (var instanceof DVariable) {
      DVariable difvar = (DVariable) var;
      if (varName.equals(var.getName())) return nf.buildConstantNode(tu.getONE());
      else if (isConstantVar(var)) return nf.buildConstantNode(tu.getZERO());

      deriv = difvar.findDerivative(varName, localDJep);
    } else if (var instanceof PartialDerivative) {
      if (isConstantVar(var)) return nf.buildConstantNode(tu.getZERO());

      PartialDerivative pvar = (PartialDerivative) var;
      DVariable dvar = pvar.getRoot();
      deriv = dvar.findDerivative(pvar, varName, localDJep);

    } else throw new ParseException("Encountered non differentiable variable");

    Node eqn = deriv.getEquation();
    if (eqn instanceof ASTVarNode) return nf.buildVariableNode(((ASTVarNode) eqn).getVar());
    if (eqn instanceof ASTConstant) return nf.buildConstantNode(((ASTConstant) eqn).getValue());

    return nf.buildVariableNode(deriv);
  }
Exemplo n.º 2
0
 /** multi dimensional differentiable variables */
 public Object visit(ASTVarNode node, Object data) throws ParseException {
   MatrixVariableI var = (MatrixVariableI) node.getVar();
   if (var.hasValidValue()) return var.getMValue();
   if (!var.hasEquation())
     throw new ParseException(
         "Tried to evaluate a variable with an invalid value but no equation");
   MatrixValueI res = (MatrixValueI) var.getEquation().jjtAccept(this, data);
   var.setMValue(res);
   return res;
 }
Exemplo n.º 3
0
  /**
   * Visit a variable node. The value of the variable is obtained from the symbol table (symTab) and
   * pushed onto the stack.
   */
  public Object visit(ASTVarNode node, Object data) throws ParseException {

    // try to get the variable object
    Variable var = symTab.getVar(node.getVarName());
    if (var == null) {
      String message = "Could not evaluate " + node.getVarName() + ": ";
      throw new ParseException(message + "the variable was not found in the symbol table");
    }

    // get the variable value
    // Object temp = var.getValue();
    Object temp = var.getValue();

    if (trapNullValues && temp == null) {
      String message = "Could not evaluate " + node.getVarName() + ": ";
      throw new ParseException(message + "variable not set");
    }
    // all is fine
    // push the value on the stack
    stack.push(temp);
    return data;
  }
Exemplo n.º 4
0
 /**
  * creates a new ASTVarNode with a given variable. This method should be sub-classed
  *
  * @throws ParseException
  */
 public ASTVarNode buildVariableNode(Variable var) throws ParseException {
   ASTVarNode node = new ASTVarNode(ParserTreeConstants.JJTVARNODE);
   node.setVar(var);
   return node;
 }
Exemplo n.º 5
0
 /**
  * creates a new ASTVarNode with the same name as argument.
  *
  * @throws ParseException
  */
 public ASTVarNode buildVariableNode(ASTVarNode node) throws ParseException {
   return buildVariableNode(node.getVar());
 }