___ _ _ / _ \ _ _ __ _| |_ ___ _ __ _ __ (_) ___ _ __ ___ | | | | | | |/ _` | __/ _ \ '__| '_ \| |/ _ \| '_ \/ __| | |_| | |_| | (_| | || __/ | | | | | | (_) | | | \__ \ \__\_\\__,_|\__,_|\__\___|_| |_| |_|_|\___/|_| |_|___/

I have written a python class for manipulating quaternions. You can download it here. For quick viewing in your browser, click here. This class implements quaternion multiplication and allows quick access to the column vector representation of a quaternion (i.e. (a,b,c,d)^T) as well as the 2x2 complex matrix representation suitable for unit quaternions.

We can represent a quaternion: a+b**i**+c**j**+d**k** in the column vector form described
above. We call *a* the scalar part of the quaternion and *b,c,d*
the vector part of the quaternion.

Scalar multiplication and quaternion addition are defined in the same way as for typical vectors in R^4.

Quaternions have an additional operation of quaternion multiplication, which is non-commutative, and is defined as follows:

a1*a2-b1*b2-c1*c2-d1*d2 |

a1*b2+b1*a2+c1*d2-d1*c2 |

a1*c2-b1*d2+c1*a2+d1*b2 |

a1*d2+b1*c2-c1*b2+d1*a2 |

A vector **x** is often transformed to **x'** using a rotation matrix
**R** using the equation **x'**=**Rx**. When we work with quaternions
we could convert them to a rotation matrix in order to perform transformations,
but we can also perform the transformation directly. A quaternion transforms a
vector as follows:

Let **q** be a unit quaternion (i.e. a quaternion with norm of 1),
**x** a vector to transform.

Create a new quaternion, **y** from x where the scalar part is 0 and the
vector part is **x**.

**y'**=**qy**inv(**q**)

And **x'** is the vector part of **y'**

We can easily combine a sequence of rotations into a single quaternion using
quaternion multiplication as defined above. We can think of this the same way
as when combining a sequence of rotation matrices. Start the with first
rotation and left multiply it by the subsequent rotations so that the first
rotation is on the right and the last rotation is on the far left. For the
sequence of rotations *q1*,...,*qn* the composite quaternion Q
is computed as:

Since my research makes use of reflections on the water surface to estimate
depth information it is interesting to note that quaternions can be used for
reflecting a vector across a plane. Define the reflection quaternion,
*q*, as a pure quaternion (0 scalar part) with a vector part equal to
the unit normal of the plane. Then the reflection *r* is a pure
quaternion of the source quaternion *s* by the following
calculation:

Of course, in case of a reflection in a river, the reflection plane is the
**z** axis and source vector **s** is trivially reflected by replacing
its **z** component with its opposite.

- https://en.wikipedia.org/wiki/Quaternion
- https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation
- http://www.euclideanspace.com/maths/geometry/affine/reflection/quaternion/index.htm
- http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/index.htm
- http://tutis.ca/Rotate/7quaternions.htm