Saturday, August 15, 2009

Vector: Cross Product

Keeping with my theme of naming operators using Unicode math symbols not in ASCII, my next attempt was the cross product. Of all the operators, this is the one that is not trivial in Perl 6, through no fault of Perl's -- it's a twisty operation. Initially I only had an implementation for 3D vectors, too, with no idea how to do the equivalent in other dimensions. A google search turned up the fact that cross product is only defined for 3D and 7D, with a huge formula for the later. Luckily the Wikipedia page source for the latter had the formula in TeX. It was a small matter of Perl 6 to convert this formula to code I could use.

I ended up using where to make dispatch to the correct version of cross product (or non at all for most dimensions).

Having the 7D cross product is a bit silly -- but it feels like a very nice use of where. I think I will soon go back through most of the other operators and add where clauses to the second parameter, to make sure it has the same dimension as the first.

Of course, I don't have any idea what the 7D solutions should look like. Luckily, I have now have a test script, so I can write tests to make sure that the results of the 7D cross product obey the proper identities:


General: I forgot to mention last time that pmichaud is looking at fixing the bug that stops you from building a more complex version of an operator out of simpler versions of the same operator. When that gets fixed, the vector addition and subtraction and vector/scalar multiplication and division will look a lot nicer.

No comments:

Post a Comment