public static MatrixNodeI multiply(MatrixNodeI m1, MatrixNodeI m2) { assert (m1.getDim().is2D()); assert (m2.getDim().is2D()); assert (m1.getDim().getLastDim() == m2.getDim().getFirstDim()); int m = m1.getDim().getFirstDim(); int n = m1.getDim().getLastDim(); int p = m2.getDim().getLastDim(); Node[] entry = new Node[m * p]; int index = 0; try { for (int i = 0; i < m; ++i) { for (int j = 0; j < p; ++j) { Node node = zero; for (int k = 0; k < n; ++k) { Node item = factory.buildOperatorNode(op.getMultiply(), get(m1, i, k), get(m2, k, j)); node = factory.buildOperatorNode(op.getAdd(), node, item); } entry[index] = simplifier.simplify(node); ++index; } } } catch (ParseException e) { } return (MatrixNodeI) factory.buildOperatorNode(op.getMList(), entry, Dimensions.valueOf(m, p)); }
/** 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(); }
public MatrixValueI evaluate(MatrixNodeI node, MatrixJep mj) throws ParseException { this.mjep = mj; return (MatrixValueI) node.jjtAccept(this, null); }
public static Node get(MatrixNodeI m, int row, int col) { return m.jjtGetChild(row * m.getDim().getLastDim() + col); }