%mathpiper,def="CrossProduct"

Function("CrossProduct",{aLeft,aRight})
[
  Local(length);
  length:=Length(aLeft);
  Check(length = 3, "Argument", "OutProduct: error, vectors not of dimension 3");
  Check(length = Length(aRight), "Argument", "OutProduct: error, vectors not of the same dimension");

  Local(perms);
  perms := PermutationsList({1,2,3});

  Local(result);
  result:=ZeroVector(3);

  Local(term);
  ForEach(term,perms)
  [
    result[ term[1] ] := result[ term[1] ] +
      LeviCivita(term) * aLeft[ term[2] ] * aRight[ term[3] ] ;
  ];
  result;
];

%/mathpiper



%mathpiper_docs,name="CrossProduct",categories="User Functions;Linear Algebra"
*CMD CrossProduct --- outer product of vectors
*STD
*CALL
	CrossProduct(a,b)
	a X b
Precedence:
*EVAL PrecedenceGet("X")

*PARMS

{a}, {b} -- three-dimensional vectors

*DESC

The cross product of the vectors "a"
and "b" is returned. The result is perpendicular to both "a" and
"b" and its length is the product of the lengths of the vectors.
Both "a" and "b" have to be three-dimensional.

*E.G.

In> {a,b,c} X {d,e,f};
Result: {b*f-c*e,c*d-a*f,a*e-b*d};

*SEE InProduct, X
%/mathpiper_docs