# Maths - Quaternions

This page is an introduction to Quaternions, the pages below this have more detail about their algebra and how to use them to represent 3D rotations.

### Introduction

Quaternions were discovered on 16 October 1843 by William Rowan Hamilton. He spent years trying to find a three dimensional number systems, but with no success, when he looked in 4 dimensions instead of 3 it worked.

Quaternions form an interesting algebra where each object contains 4 scalar variables (sometimes known as Euler Parameters not to be confused with Euler angles), these objects can be added and multiplied as a single unit in a similar way to the usual algebra of numbers. However, there is a difference, unlike the algebra of scalar numbers qa * qb is not necessarily equal to qb * qa (where qa and qb are quaternions). In mathematical terms, quaternion multiplication is not commutative.

The arithmetic of quaternions, such as how to do addition and multiplication, is explained on this page.

On this page we will introduce quaternions as an extension of complex numbers with two additional imaginary dimensions, however there are other ways to think about quaternions and other notations for quaternions which are described on this page.

Quaternions have 4 dimensions (each quaternion consists of 4 scalar numbers), one real dimension and 3 imaginary dimensions. Each of these imaginary dimensions has a unit value of the square root of -1, but they are different square roots of -1 all mutually perpendicular to each other, known as i,j and k. So a quaternion can be represented as follows:

a + i b + j c + k d

It may seem strange that there are 3 square roots of -1, but we have to remember that we are working in 4 dimensions so there are at least 3 ways to get round from +1 to -1. It is not very practical to try to draw 4 dimensions in 2 dimensions, but here is an attempt: We have to be very careful with this picture of quaternions, it gives an intuative feel for how quaternions can represent rotations in 3D but it is misleading, we might think from this (as Hamilton did) that since i² =-1 that therefore i² represents a rotation of 180° and so 'i' represents a rotation of 90°. This is wrong! As we shall see below, to make it work porperly, we need to halve the angle so 'i' represents a rotation of 180° and i² represents a rotation of 360°. So +1 and -1 both represent the same rotation, this will be explained more fully later.

## Use of quaternions to represent transformations in 3D.

The main practical application of this interesting algebra is to represent 3D rotations.

In fact quaternions can represent 3D reflections, rotations and scaling, however a single quaternion operation cannot include translations so if we want to rotate, reflect or scale around a point other than the origin, then we would have to handle the translation part separately (see affine translations). To calculate the resulting point (Pout) when we translate the point (Pin) using quaternions then we use the following equations:

• For Reflection & scaling: Pout = q * Pin * q
• For Rotation & scaling: Pout = q * Pin * conj(q)

The majority of applications involve pure rotations, for this we restrict the quaternions to those with unit magnitude and we use only multiplications and not addition to represent a combination of different rotations. When quaternions are normalised in this way, together with the multiplication operation to combine rotations, form a mathematical group, in this case SU(2).

We can use this to do lots of operations which are required in practical applications such as, combining subsequent rotations (and equivalently orientations), interpolating between them, etc.

When quaternions are used in this way we can think of them as being similar to axis-angle except that real part is equal to cos(angle/2) and the complex part is made up of the axis vector times sin(angle/2). It is quite difficult to give a physical meaning to a quaternion, and many people find this similarity to axis-angle as the most intuitive way to think about it, others may just prefer to think of quaternions as an interesting mathematical system which has the same properties as 3D rotations.

The quaternion in terms of axis-angle is:

q = cos(a/2) + i ( x * sin(a/2)) + j (y * sin(a/2)) + k ( z * sin(a/2))

where:

• a=angle of rotation.
• x,y,z = vector representing axis of rotation.

So it is closely related to the axis angle representation of rotations.

 complex number quaternion number can be used to represent: 2D vector 3D rotation subsequent operations: combined by addition combined by multiplication 'i' represents: rotation by 90 degrees rotation by 180 degrees

The quaternion 'i' represents a rotation of 180 degrees about the x axis, the quaternion 'j' represents a rotation of 180 degrees about the y axis, the quaternion 'k' represents a rotation of 180 degrees about the y axis. So i*i = -1 represents a rotation of 360 degrees about the x axis.

It may seem strange that -1 represents a rotation of 360 degrees, since this is 'no change' I would expect it to be 1.

However it turns out that if (a + b i + c j + d k) represents a rotation then (- a - b i - c j - d k) represents the same rotation. In other words if we negate all the terms we get a different quaternion but it represents the same rotation. This makes sense in terms of axis angle representation, if we take the reverse angle and also reverse the axis this will produce the same result.

So both 1 and -1 represent the identity (do nothing) rotation. An object which, if rotated by 360 degrees it is inverted, is known as a spinor.

Quaternions are therefore spinors.

### Why do we need 4 dimensions to represent 3D rotations?

We can represent 3D rotations as 3 numbers (see euler angles) but such a representation is non-linear and difficult to work with. An analogy is a two dimensional map of the earth, there is no way that we can map the surface of the earth without distorting either angles or areas. However once the 2D map is wrapped round a 3D sphere it becomes linear, in a similar way the 3D space of rotations becomes linear when it is mapped to a 4D hypersphere. I have described this analogy further on this page.

There are few differences in this case for instance:

• We only need half the hypersphere (hyper-hemisphere?) since opposite sides of the hypersphere represent the same rotation.
• Transforms are combined by multiplying quaternions, not by adding them.
• Points are transformed by the sandwich product described above.

## sfrotation

On these pages will be developing a class sfrotation (full listing here) which holds a rotation and encapsulates operations such as combining two rotations, in addition to coding the rotation as a quaternion it can also be coded as euler or axis angle and can convert between these formats.