/** other functions * */ public Object visit(ASTFunNode node, Object data) throws ParseException { MatrixNodeI mnode = (MatrixNodeI) node; PostfixMathCommandI pfmc = node.getPFMC(); if (pfmc instanceof MatrixSpecialEvaluationI) { MatrixSpecialEvaluationI se = (MatrixSpecialEvaluationI) pfmc; return se.evaluate(mnode, this, mjep); } else if (pfmc instanceof CallbackEvaluationI) { Object val = ((CallbackEvaluationI) pfmc).evaluate(node, this); if (val instanceof MatrixValueI) mnode.getMValue().setEles((MatrixValueI) val); else mnode.getMValue().setEle(0, val); return mnode.getMValue(); } else if (pfmc instanceof SpecialEvaluationI) { throw new ParseException("Encountered an instance of SpecialEvaluationI"); } else if (pfmc instanceof BinaryOperatorI) { BinaryOperatorI bin = (BinaryOperatorI) pfmc; MatrixValueI lhsval = (MatrixValueI) node.jjtGetChild(0).jjtAccept(this, data); MatrixValueI rhsval = (MatrixValueI) node.jjtGetChild(1).jjtAccept(this, data); return bin.calcValue(mnode.getMValue(), lhsval, rhsval); } else if (pfmc instanceof UnaryOperatorI) { UnaryOperatorI uni = (UnaryOperatorI) pfmc; MatrixValueI val = (MatrixValueI) node.jjtGetChild(0).jjtAccept(this, data); return uni.calcValue(mnode.getMValue(), val); } else if (pfmc instanceof NaryOperatorI) { NaryOperatorI uni = (NaryOperatorI) pfmc; MatrixValueI results[] = new MatrixValueI[node.jjtGetNumChildren()]; for (int i = 0; i < results.length; ++i) results[i] = (MatrixValueI) node.jjtGetChild(i).jjtAccept(this, data); return uni.calcValue(mnode.getMValue(), results); } else if (pfmc instanceof Comparative) { Object lhsval = (MatrixValueI) node.jjtGetChild(0).jjtAccept(this, data); Object rhsval = (MatrixValueI) node.jjtGetChild(1).jjtAccept(this, data); stack.push(lhsval); stack.push(rhsval); pfmc.setCurNumberOfParameters(2); pfmc.run(stack); mnode.getMValue().setEle(0, stack.pop()); return mnode.getMValue(); } // not a clever op use old style call // assumes int num = node.jjtGetNumChildren(); for (int i = 0; i < num; ++i) { MatrixValueI vec = (MatrixValueI) node.jjtGetChild(i).jjtAccept(this, data); if (!vec.getDim().equals(Dimensions.ONE)) throw new ParseException("Arguments of " + node.getName() + " must be scalers"); stack.push(vec.getEle(0)); } pfmc.setCurNumberOfParameters(num); pfmc.run(stack); mnode.getMValue().setEle(0, stack.pop()); return mnode.getMValue(); }
/** Multiply the inputs element by element putting the results in res. */ public MatrixValueI calcValue(MatrixValueI res, MatrixValueI[] inputs) throws ParseException { int numArgs = inputs.length; int len = res.getNumEles(); for (int i = 0; i < len; ++i) { Object ele = inputs[0].getEle(i); for (int j = 1; j < numArgs; ++j) ele = super.mul(ele, inputs[j].getEle(i)); res.setEle(i, ele); } return res; }
public Object eval(Node node) throws ParseException { MatrixValueI val = (MatrixValueI) node.jjtAccept(this, null); return val.copy(); }
public Object mul(MatrixValueI param1, MatrixValueI param2) throws ParseException { Dimensions dims = this.calcDim(new Dimensions[] {param1.getDim(), param2.getDim()}); MatrixValueI res = Tensor.getInstance(dims); return this.calcValue(res, new MatrixValueI[] {param1, param2}); }
/** Multiply arguments element by element. Returns result. */ public Object mul(Object param1, Object param2) throws ParseException { if (param1 instanceof MatrixValueI && param2 instanceof MatrixValueI) { return mul((MatrixValueI) param1, (MatrixValueI) param2); } else if (param1 instanceof MatrixValueI) { MatrixValueI l = (MatrixValueI) param1; MatrixValueI res = Tensor.getInstance(l.getDim()); for (int i = 0; i < res.getNumEles(); ++i) res.setEle(i, super.mul(l.getEle(i), param2)); return res; } else if (param2 instanceof MatrixValueI) { MatrixValueI r = (MatrixValueI) param2; MatrixValueI res = Tensor.getInstance(r.getDim()); for (int i = 0; i < res.getNumEles(); ++i) res.setEle(i, super.mul(param1, r.getEle(i))); return res; } return super.mul(param1, param2); }