## Thursday, 12 April 2012

### Multiplying two vectors

In mathematics there are a number of ways to multiply two vectors. The most common is the dot product, which works for any two vectors of the same size. In three dimensions there's the cross product, while in two dimensions there's the perp dot product, sort of equivalent to the cross product.

But they all have the weakness that they are not invertible. Given the cross product

and knowing c and another vector it isn't possible to calculate the third vector (c × c = 0, so given a solution all points on a line through this solution parallel to c are also solutions).

This is annoying because many other products are invertible. Not just scalar multiplication but products of general complex numbers, quaternions and matrices can all be inverted. But there is a product of two vectors that is invertible, the geometric product of geometric algebra.

This is simply the sum of the inner product and outer products, which are the dot product and cross products generalised to higher dimensions. Except the outer product is not a scalar or vector but a bivector, a quantity associated with area, or thought of as two-dimensional (distinct from one-dimensional vectors).

The details of this are too technical for a blog post (I would recommend the above links for details, especially the one to bivector which I had a part in writing), but there is one further property worth highlighting. The geometric product of a vector with itself is a scalar, equivalent to the dot product with itself, numerically equal to the square of its length.

This means vector division is possible. As if a vector times itself is a scalar then to divide by a vector simply multiply by the vector then divide by the scalar. Or multiply by the vector scaled by it's length squared.

I have avoided algebra so far as it would make the post far too long and technical, but it is worth doing so for the following practical example: given two vectors, x any y, the question 'what must x be multiplied by to give y?' is answered using division. In particular given the following product:

R can be calculated by multiplying by x-1, the multiplicative inverse of x:

Which gives the value for R:

R is a spinor or rotor, which acts on vectors and rotates them through multiplication. It rotates not only x to y but any vector in the plane containing x and y.

In two dimensions there is only one plane so R rotates any vector in the same way so by the same amount. The quantity R is a complex number with real and imaginary parts, and can be calculated as follows:

R.real = (x.fX * y.fX + x.fY * y.fY) / fLengthSquared;
R.imag = (x.fY * y.fY - x.fX * y.fY) / fLengthSquared;

To multiply this by a vector, e.g. to rotate a vector u into vector v, the following is used:

v.fX = u.fX * R.real + u.fY * R.imag;
v.fY = u.fY * R.real - u.fX * R.imag;

This can be done with complex numbers but the above is in some ways simpler: no need to deal with the conjugate when dividing. It also treats vectors (which square to positive values) and complex numbers (with positive and negative squares) as separate, distinct things which makes more mathematical sense.

This generalises to higher dimensions, with one difference. Because in dimensions higher than two rotations are non-commutative the above will not work. Instead the product used to rotate a vector is two sided, that is:

Where R-1 is the reversion or inverse of R. In three dimensions this is quaternion rotation, but it is not limited to two and three dimensions.