public Object cross(Vector lhs, Vector rhs) throws ParseException { int len = lhs.size(); if ((len != 2 && len != 3) || len != rhs.size()) throw new ParseException("Cross: both sides must be of length 3"); if (len == 3) { Vector res = new Vector(3); res.setSize(3); res.setElementAt( sub.sub( mul.mul(lhs.elementAt(1), rhs.elementAt(2)), mul.mul(lhs.elementAt(2), rhs.elementAt(1))), 0); res.setElementAt( sub.sub( mul.mul(lhs.elementAt(2), rhs.elementAt(0)), mul.mul(lhs.elementAt(0), rhs.elementAt(2))), 1); res.setElementAt( sub.sub( mul.mul(lhs.elementAt(0), rhs.elementAt(1)), mul.mul(lhs.elementAt(1), rhs.elementAt(0))), 2); return res; } else { return sub.sub( mul.mul(lhs.elementAt(0), rhs.elementAt(1)), mul.mul(lhs.elementAt(1), rhs.elementAt(0))); } }
public Object dot(Vector v1, Vector v2) throws ParseException { if (v1.size() != v2.size()) throw new ParseException("Dot: both sides of dot must be same length"); int len = v1.size(); if (len < 1) throw new ParseException("Dot: empty vectors parsed"); Object res = mul.mul(v1.elementAt(0), v2.elementAt(0)); for (int i = 1; i < len; ++i) { res = add.add(res, mul.mul(v1.elementAt(i), v2.elementAt(i))); } return res; }