/** * 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); }
public boolean isConstantVar(XVariable var) { if (!var.hasEquation()) return true; Node eqn = var.getEquation(); if (eqn instanceof ASTConstant) return true; /* So why would we want a=x to be treated as a constant? */ // if(eqn instanceof ASTVarNode) { // return isConstantVar((XVariable)((ASTVarNode) eqn).getVar()); // } return false; }