/** * 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); }
/** 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; }
/** * creates a new ASTVarNode with the same name as argument. * * @throws ParseException */ public ASTVarNode buildVariableNode(ASTVarNode node) throws ParseException { return buildVariableNode(node.getVar()); }