Introduction:
This tutorial explains how quaternions work and how they can be used to produce 3D rotations. Basics of linear algebra and trigonometry are required for this tutorial. Complex numbers and polar coordinates are also important, but the important parts are summarized in the next background section. This tutorial works its way up from 2D rotations and how its related to complex numbers, and then it extends that to the 3D space to show how quaternions produce 3D rotations.

Quaternions are very common in computer graphics and animation because of some useful properties, such as avoiding gimbal lock, which is a major problem with euler angles.

Background:
Polar coordinates:
Polar coordinate diagram
Polar coordinates are coordinates expressed in terms of angle θ and magnitude r.

A cartesian coordinate (x,y) is equivalent to the polar coordinate (r,θ) where:
r=x2+y2 θ=arctan(y,x)

Polar coordiantes can be converted back into cartesian coordinates using the properties of sin and cos:
cos(θ)=xr x=r·cos(θ) sin(θ)=yr y=r·sin(θ)
Complex numbers:
Complex numbers diagram
Complex numbers have the form z=a+bi, where a is the real part, b is the imaginary part, and i is the imaginary unit which is the square root of 1. In other words, i2=1.

Complex numbers can be represented in both cartesian and polar coordinates. In cartesian coordinates, the x axis is the real axis, and the y axis is the imaginary axis:
z=x+yi

Similarly, a complex number z=a+bi can be expressed as polar coordinates by substituting:
a=r·cos(θ) b=r·sin(θ)
This gives us:
z = ( r·cos(θ) ) + ( r·sin(θ) ) · i = r · ( cos(θ) + sin(θ) · i )

Two complex numbers z1=a1+b1i and z2=a2+b2i can be multiplied as follows:
z1·z2 = (a1+b1i) · (a2+b2i) = a1a2 + a1b2i + a2b1i + b1b2i2 = a1a2 + a1b2i + a2b1i b1b2 = ( a1a2 b1b2 ) + ( a1b2 + a2b1 ) i

In polar coordinates, this multiplication is equivalent to:
z1 z2 = r1 ( cos(θ1) + sin(θ1)i ) · r2 ( cos(θ2) + sin(θ2)i ) =r1r2 ( ( cos(θ1) + sin(θ1)i ) · ( cos(θ2) + sin(θ2)i ) ) =r1r2 ( cos(θ1)cos(θ2) + cos(θ1)sin(θ2)i + sin(θ1)cos(θ2)i + sin(θ1)sin(θ2)i2 ) =r1r2 ( ( cos(θ1)cos(θ2) sin(θ1)sin(θ2) ) + ( cos(θ1)sin(θ2) + sin(θ1)cos(θ2) ) i )

Then simplying with trigonometric identities, we get:
=r1r2 ( cos(θ1+θ2) + sin(θ1+θ2)i )

In other words, multiplying two complex numbers creates a new complex number where the original angles have added up, and the magnitudes have multiplied.

Complex numbers and 2D rotation:
Complex number rotation diagram
Let's say you have a cartesian point (x1,y1) and you want to rotate it by β radians.

As shown in the background section, this point (x1,y1) is equivalent to the polar coordinate (r,α) such that:
x1=r·cos(α) y1=r·sin(α)

When this point is rotated by β radians, we get a new point (x2,y2) which is equivalent to the polar coordinate (r,α+β):
x2=r·cos(α+β) y2=r·sin(α+β)
Expanding this out using the trigonometric identities, we get:
x2 = r ( cos(α)cos(β) sin(α)sin(β) ) y2 = r ( sin(α)cos(β) + cos(α)sin(β) )

Note that as shown in the background section of complex number multiplication, this is equivalent to multiplying the complex numbers (r,α) and (1,β):
r(cos(α)+sin(α)i) · 1(cos(β)+sin(β)i) = r ( cos(α)cos(β) sin(α)sin(β) ) + r ( cos(α)sin(β) + sin(α)cos(β) ) i =x2+y2i
Simplying using trigonometric identities, this can be simplified to:
r ( cos(α)cos(β) sin(α)sin(β) ) + r ( cos(α)sin(β) + sin(α)cos(β) ) i =r ( cos(α+β) + sin(α+β)i )

This means complex numbers with magnitude 1 naturally encode 2D rotation; multiplying a point (x,y) with a complex number (1,θ) rotates the point by θ. For example, let's say you want to rotate a point by π2 radians, this is equivalent to the complex number:
cos(π2) + sin(π2)i =0+1·i =i

Multiplication by i diagram
This shows us that multiplying by i is equivalent to rotating by π2 radians. See this picture which shows multiplication by i on the key points +1, +i, 1, and i:
(+1)·i=(+i) (+i)·i=(1) (1)·i=(i) (i)·i=(+1)

Representing 2D rotations as matrices:
As shown in the previous section, the point (x1,y1)=( r·cos(θ1),r·sin(θ1)) can be rotated by θ as follows:
x2 = r ( cos(θ1)cos(θ) sin(θ1)sin(θ) ) y2 = r ( sin(θ1)cos(θ) + cos(θ1)sin(θ) )

This can be expressed as the matrix vector multiplication:
[ x2 y2 ] = [ cos(θ) sin(θ) sin(θ) cos(θ) ] [ rcos(θ1) rsin(θ1) ] = [ cos(θ) sin(θ) sin(θ) cos(θ) ] [ x1 y1 ]

Another way to derive this matrix is represent 1 and i as matrices:
1 = [ 1 0 0 1 ] i = [ 0 1 1 0 ]

This means the rotation complex number cos(θ)+sin(θ)i can be expressed as:
cos(θ) [ 1 0 0 1 ] + sin(θ) [ 0 1 1 0 ] = [ cos(θ) sin(θ) sin(θ) cos(θ) ]

Euler's formula:
Euler's formula states that eix = cos(x) + i·sin(x) . This can be shown as follows:

The Maclaurin expansion of sin(x) is:
sin(x) = x11! x33! + x55! x77! + x99! ...

The Maclaurin expansion of cos(x) is:
cos(x) = x00! x22! + x44! x66! + x88! ...

The Maclaurin expansion of ex is:
ex = x00! + x11! + x22! + x33! + x44! + ...

This means that putting e to the power of ix gives:
eix = (ix)00! + (ix)11! + (ix)22! + (ix)33! + (ix)44! + (ix)55! + (ix)66! + (ix)77! + (ix)88! + (ix)99! ... = x00! + x11!i x22! x33!i + x44! + x55!i x66! x77!i + x88! + x99!i ... = ( x00! x22! + x44! x66! + x88! ... ) + i · ( x11! x33! + x55! x77! + x99! ... ) = cos(x) + i·sin(x)

Using Euler's formula, a rotation of the vector v=(x,y) by θ, can be expressed as follows:
eiθ·v = ( cos(θ) + i·sin(θ) ) · ( x + i·y )

3D Rotation:
Rodrigues rotation formula diagram TODO: INSERT LIVE LINK
A vector v can be rotated in 3D space around any given unit vector u representing the axis of rotation using Rodrigues' rotation formula:
vrot = cos(θ)v + sin(θ) (u×v) + (1cos(θ)) (u·v) u
Note that vrot is v rotated θ radians around u.

To derive this formula, we start by breaking the vector v into v and v such that:
  • v+v=v
  • =v is the part of v parallel to u
  • v is the part of v perpendicular to u

The parallel vector v is the projection of v onto u:
v = v · u u · u u = v · u || u || 2 u
Note that ||u||2=1 since u is a unit vector, so we have:
v = ( v · u ) u

After v is found, we can calulate v as follows:
v = v v = v ( v · u ) u

When the rotation θ is applied, v will remain the same, and v will rotate on the plane with the normal u. This means the rotated vector vrot will be a sum of v and v⊥rot:
vrot = v + v⊥rot.
Note that v⊥rot is v rotated θ radians on the plane with normal u.

The vector u×v is v rotated π2 radians on the plane with the normal u. The rotated perpendicular vector v⊥rot will be a sum of this vector u×v and v:

These vectors can be used to calculate the rotated perpendicular vector v⊥rot using sine and cosine:
v⊥rot = cos(θ)v + sin(θ) ( u×v )

All of these definitions can be combined to form the rotation formula:
vrot=v+v⊥rot = v + cos(θ)v + sin(θ) ( u×v ) = v + cos(θ)(vv) + sin(θ) ( u×(vv) ) = v + cos(θ)v cos(θ)v + sin(θ) ( u×v u×v ) Note: u×v=0 since u and v are parallel = cos(θ)v + v cos(θ)v + sin(θ) ( u×v ) = cos(θ)v + (1cos(θ))v + sin(θ) ( u×v ) vrot = cos(θ)v + sin(θ) ( u×v ) + (1cos(θ)) (u·v)u

This formula can be converted into a matrix multiplication by factoring out the original vector v:
cos(θ)v + sin(θ) ( u×v ) + (1cos(θ)) (u·v)u cos(θ) ( vxi + vyj + vzk ) + sin(θ) | i j k ux uy uz vx vy vz | + ( 1 cos ( θ ) ) ( uxvx + uyvy + uzvz ) ( uxi + uyj + uzk ) = cos(θ) ( vxi + vyj + vzk ) + sin(θ) ( ( uy vz uz vy ) i ( ux vz uz vx ) j + ( ux vy uy vx ) k ) + ( 1 cos ( θ ) ) ( uxvx + uyvy + uzvz ) ( uxi + uyj + uzk ) = ( cos(θ)vx + sin(θ) ( uy vz uz vy ) + (1cos(θ)) (uxvx+uyvy+uzvz) ux ) i + ( cos(θ)vy sin(θ) ( ux vz uz vx ) + (1cos(θ)) (uxvx+uyvy+uzvz) uy ) j + ( cos(θ)vz + sin(θ) ( ux vy uy vx ) + (1cos(θ)) (uxvx+uyvy+uzvz) uz ) k = [ cos(θ) + (1cos(θ)) ux2 sin(θ)uz + (1cos(θ))uxuy sin(θ)uy + (1cos(θ))uxuz sin(θ)uz + (1cos(θ))uxuy cos(θ) + (1cos(θ)) uy2 sin(θ)ux + (1cos(θ)) uy uz sin(θ)uy + (1cos(θ)) ux uz sin(θ)ux + (1cos(θ)) uy uz cos(θ) + (1cos(θ)) uz2 ] [ vx vy vz ] = R ( ux , uy , uz ) , θ v

To use Euler angles, this matrix can be turned into seperate matrices for each axis x, y, and z:
Rx = R ( 1 , 0 , 0 ) , θ = [ 1 0 0 0 cos(θ) sin(θ) 0 sin(θ) cos(θ) ] Ry = R ( 0 , 1 , 0 ) , θ = [ cos(θ) 0 sin(θ) 0 1 0 sin(θ) 0 cos(θ) ] Rz = R ( 0 , 0 , 1 ) , θ = [ cos(θ) sin(θ) 0 sin(θ) cos(θ) 0 0 0 1 ]

Quaternions:
Quaternions are a 4D number system of the form a+bi+cj+dk. Quaternions extend the complex number system with 2 additional dimensions j and k.

This chart (from Wikipedia) shows the multiplication rules for the basis vectors i, j, and k (the left column is the first term and the top column is the second term):
1 i j k
1 1 i j k
i i 1 k j
j j k 1 i
k k j i 1
Note: Row × Column

Note that since the multiplication of the basis vectors is not commutative (i.e. i·jj·i), quaternion multiplication is not commutative.

The multiplication result of two quaternions q1 and q2 can be derived using this chart:
q1·q2 = ( a1 + b1i + c1j + d1k ) · ( a2 + b2i + c2j + d2k ) = a1a2 + a1b2i + a1c2j + a1d2k + b1a2i + b1b2i2 + b1c2ij + b1d2ik + c1a2j + c1b2ji + c1c2j2 + c1d2jk + d1a2k + d1b2ki + d1c2kj + d1d2k2 = a1a2 + a1b2i + a1c2j + a1d2k + b1a2i b1b2 + b1c2k b1d2j + c1a2j c1b2k c1c2 + c1d2i + d1a2k + d1b2j d1c2i d1d2 = ( a1a2 b1b2 c1c2 d1d2 ) + ( a1b2 + b1a2 + c1d2 d1c2 ) i + ( a1c2 b1d2 + c1a2 + d1b2 ) j + ( a1d2 + b1c2 c1b2 + d1a2 ) k

Quaternions can also be expressed as a pair with a real part r=a, and a scalar part v=(b,c,d):
q=(r,v)

In this form, the multiplication can be expressed in terms of dot products and cross products:
q1·q2 = (r1,v1) · (r2,v2) = ( r1r2 v1 · v2 , r1v2 + r2v1 + v1 × v2 )
Note that this scalar/vector product is the exact same calculation as the manual multiplication of quaternions derived above:
r1r2 v1 · v2 = a1a2 b1b2 c1c2 d1d2 r1v2 + r2v1 + v1 × v2 = ( a1b2 + b1a2 + c1d2 d1c2 ) i + ( a1c2 b1d2 + c1a2 + d1b2 ) j + ( a1d2 + b1c2 c1b2 + d1a2 ) k

Lastly, the conjugate of a quaternion q=(r,v), denoted by q*, is simply its vector part multipled by 1:
q* = ( r , v )
The product of a quaternion q and its conjugate q* is:
qq* = ( r , v ) ( r , v ) = ( r2 + v · v , r · v r · v + ( v × v ) ) = ( r2 + v · v , 0 ) = a2 + b2 + c2 + d2
This means that if q is a unit quaternion, this product qq* will be equal to 1. This fact will be useful later on.

Quaternion rotation demo:
Blender animation/demo download
Visualizing how quaternion multiplication represents rotations is extremely helpful for understanding how quaternions can be used to represent 3D rotation.

It is a bit hard to see, but this demo has 3 cubes in it: a center cube, an inner cube, and the outer cube. The center cube represents the real dimension's point +1. The outer cube's parts all represent the real dimension's point 1.

The colored spheres represent the initial points: 1, +1, +i, i, +j, j, +k, k.

The inner cube represents the 3D imaginary space of the i, j, and k axis. The colored spheres can identify which dimension an inner part represents (for example: the inner cube part with the red sphere is the i dimension).

Lastly, the colored arrows represent a quaternion multiplication. The red arrows represent a multiplication by +i, the green arrows represent a multiplication by +j, and the blue arrows represent a multiplication by +k.

Notice which location each initial point takes after a multiplication. For example, a muliplication by +i causes the initial points to move as follows:
+i1 1i i+1 +1+i

+jk kj j+k +k+j

Note that this creates two rotations. The first rotation happens on the i axis and follows the regular 2D complex number rules/rotation, and affects the i and real dimensions. The second rotation happens on the j and k axis/dimensions.

This demo shows that quaterion multiplication does a 3D rotation as expected, but also does a 2D rotation which we want to avoid. In the section "Quaternions and 3D rotation", we will discuss this more and show how the additional 2D rotation can be cancelled out to produce just the 3D rotation.

Euler's formula for quaternions:
Euler's formula can be extended to quaternions by expanding the Maclaurin series of evθ, where v is a pure vector quaternion (no scalar part):
evθ = (vθ)00! + (vθ)11! + (vθ)22! + (vθ)33! + (vθ)44! + (vθ)55! + (vθ)66! + (vθ)77! + (vθ)88! + (vθ)99! + ...
Evaluating the first few quaternion terms, we see the pattern:
(0,vθ)0 = ( ||vθ||0 , 0 ) (0,vθ)1 = ( 0 , ||vθ||0 · vθ ) (0,vθ)2 = (0,vθ) · (0,vθ) = ( ||vθ||2 , 0 ) (0,vθ)3 = ( ||vθ||2 , 0 ) · (0,vθ) = ( 0 , ||vθ||2 · vθ ) (0,vθ)4 = ( 0 , ||vθ||2 · vθ ) · (0,vθ) = ( ||vθ||4 , 0 )
This means evθ equals:
evθ = ||vθ||00! + ||vθ||0·vθ1! ||vθ||22! ||vθ||2·vθ3! + ||vθ||44! + ||vθ||4·vθ5! ||vθ||66! ||vθ||6·vθ7! + ||vθ||88! + ||vθ||8·vθ9! ... = ( ||vθ||00! ||vθ||22! + ||vθ||44! ||vθ||66! + ||vθ||88! ... ) + vθ||vθ|| · ( ||vθ||11! ||vθ||33! + ||vθ||55! ||vθ||77! + ||vθ||99! ... ) = cos(||vθ||) + vθ||vθ|| · sin(||vθ||)

If v is a unit vector, we get:
||v||=1 ||vθ||=||v||·||θ|| = ||θ||
This gives us: evθ = cos(||θ||) + vθ||θ|| · sin(||θ||)

This can be simplified further using the following facts:
cos(θ) = cos(θ) for allθ cos(||θ||) = cos(θ) θ||θ|| · sin(||θ||) = sign(θ)sin(||θ||) = sin(θ) for allθ vθ||θ|| · sin(||θ||) = v · sin(θ)

This leaves us with:
evθ = cos(θ) + v · sin(θ)

Quaternions and 3D rotation:
Quaternion rotation TODO: INSERT LIVE LINK
Applying the quaternion euθ to a vector v produces the result:
euθ · v =(cos(θ),sin(θ)u) (0,v) = ( sin(θ)(u·v) , cos(θ)v + sin(θ)(u×v) )
This product is not a pure 3D rotation - it introduces the scalar component sin(θ)(u·v), which makes the product a 4D point. Another problem is that the vector parts rotation happens on the plane formed by the vectors v and u×v - which is the plane with normal v×(u×v). Also note that when the angle between u and v is not a multiple of π2, the lengths of v and u×v will not be the same and so the vector part will also end up being scaled between ||v|| and ||u×v||.

This multiplication is equivalent to applying euθ to both the v and v parts:
euθ · v = euθ · ( v + v ) = euθ · v + euθ · v = (cos(θ),sin(θ)u) (0,v) + (cos(θ),sin(θ)u) (0,v) = ( sin(θ)(u·v) , cos(θ)v ) + ( 0 , cos(θ)v + sin(θ)(u×v) ) = ( sin(θ)(u·v) , cos(θ)v + sin(θ)(u×v) ) Note: u · v = u · ( v + v ) = u · v + 0 sin(θ)(u·v) = sin(θ)(u·v) Note: u × v = u × ( v + v ) = 0 + u × v sin(θ)(u×v) = sin(θ)(u×v) = ( sin(θ)(u·v) , cos(θ)v + sin(θ)(u×v) )

This shows that the multiplication euθ·v has side effects on v. For a valid 3D rotation, v should not be affected like this. Also note that the the multiplication euθ·v produces cos(θ)v + sin(θ) ( u×v ) , which is v⊥rot from Rodrigues' rotation formula. If these side effects can be removed from v, the product will be exactly the rotated vector from Rodrigues' rotation formula.

Note that we cannot apply euθ to only v because this equation only applies the rotation to the vector part of v which is perpendicular to u, and that operation cannot be converted to a single matrix vector multiplication. To build matrices which can be applied to any arbitrary 3D vector v, the rotation must be applied to both v and v:
euθ · v + euθ · v

The side effects on v can be cancelled out by multiplying with the quaternion conjugate:
euθ · euθ · v = (1,0) (0,v) = (0,v) = v

Note that this can also be accomplished by right multiplying with the conjugate:
euθ · v · euθ = ( cos(θ) , sin(θ)u ) ( 0 , v ) ( cos(θ) , sin(θ)u ) = ( sin(θ) ( u · v ) , cos(θ) v + sin(θ) ( u × v ) ) ( cos(θ) , sin(θ)u ) u × v = 0 since u and v are parallel = ( sin(θ) ( u · v ) , cos(θ) v ) ( cos(θ) , sin(θ)u ) = (' sin(θ) cos(θ) ( u · v ) + sin(θ) cos(θ) ( u · v ) , sin2(θ) ( u · v ) u + cos2(θ) v sin(θ) cos(θ) ( v × u ) ) Note: ( u · v ) u = v = ( 0 , sin2(θ) v + cos2(θ) v sin(θ) cos(θ) ( v × u ) ) = ( 0 , ( sin2(θ) + cos2(θ) ) v sin(θ) cos(θ) ( v × u ) ) Note: sin2(θ) + cos2(θ) = 1 Note: v × u = 0 = ( 0 , v ) = v

Also note what happens when v is right multiplied by the conjugate:
euθ · v · euθ = ( cos(θ) , sin(θ)u ) ( 0 , v ) ( cos(θ) , sin(θ)u ) = ( sin(θ) ( u · v ) , cos(θ) v + sin(θ) ( u × v ) ) ( cos(θ) , sin(θ)u ) u · v = 0 since u and v are perpendicular = ( 0 , cos(θ) v + sin(θ) ( u × v ) ) ( cos(θ) , sin(θ)u ) = ( sin(θ) u · ( cos(θ) v + sin(θ) ( u × v ) ) , cos(θ) ( cos(θ) v + sin(θ) ( u × v ) ) + ( cos(θ) v + sin(θ) ( u × v ) ) × ( sin(θ) u ) ) = ( sin(θ) cos(θ) ( u · v ) sin2(θ) ( u · ( u × v ) ) , cos2(θ) v + sin(θ) cos(θ) ( u × v ) sin(θ) cos(θ) ( v × u ) sin2(θ) ( ( u × v ) × u ) ) u · v = 0 since u and v are perpendicular u · ( u × v ) = 0 since u and u × v are perpendicular u × v = v × u by definition of the cross product ( u × v ) × u = ( u · v ) u + ( u · u ) v by the triple product formula = ( 0 , cos2(θ) v + 2 sin(θ) cos(θ) ( u × v ) sin2(θ) ( ( u · v ) u + ( u · u ) v ) ) ( u · v ) u = 0 since u and v are perpendicular u · u = ||u||2 = 1 = ( 0 , cos2(θ) v + 2 sin(θ) cos(θ) ( u × v ) sin2(θ) v ) = ( 0 , ( cos2(θ) sin2(θ) ) v + 2 sin(θ) cos(θ) ( u × v ) ) cos2(θ) sin2(θ) = cos(2θ) 2 sin(θ) cos(θ) = sin(2θ) = ( 0 , cos(2θ) v + sin(2θ) ( u × v ) )

This means the operation euθ · v · euθ does not change v, but it does rotate v to compute v⊥rot, which gives the final result:
euθ · v · euθ = euθ · ( v + v ) · euθ = euθ · v · euθ + euθ · v · euθ = v + cos(2θ) v + sin(2θ) ( u × v )

In other words, v remains the same, and v gets rotated by 2θ around u. This is exactly Rodrigues' rotation formula, but with the angle of rotation doubled. This can be worked around by simply dividing the angle of rotation by two, euθ2.

Converting quaternions to matrices:
The operation euθ · v · euθ can be converted into a matrix multiplication by factoring out v:
euθ · v · euθ = ( cos(θ) , sin(θ) u ) · ( 0 , v ) · ( cos(θ) , sin(θ) u ) = ( sin(θ) ( u · v ) , cos(θ) v + sin(θ) ( u × v ) ) · ( cos(θ) , sin(θ) u )

The scalar part of the product becomes:
sin(θ) cos(θ) ( u · v ) ( cos(θ) v + sin(θ) ( u × v ) ) · ( sin(θ) u ) = sin(θ) cos(θ) ( u · v ) + sin(θ) cos(θ) ( u · v ) + sin2(θ) ( u · ( u × v ) ) = sin2(θ) ( u · ( u × v ) ) u · ( u × v ) = 0 since u and u × v are perpendicular = 0

The vector part of the product becomes:
sin2(θ) ( u · v ) u + cos(θ) ( cos(θ) v + sin(θ) ( u × v ) ) + ( cos(θ) v + sin(θ) ( u × v ) ) × ( sin(θ) u ) = sin2(θ) ( u · v ) u + cos2(θ) v + sin(θ) cos(θ) ( u × v ) sin(θ) cos(θ) ( v × u ) sin2(θ) ( ( u × v ) × u ) ( u × v ) × u = ( u · v ) u + ( u · u ) v by the triple product formula = sin2(θ) ( u · v ) u + cos2(θ) v + 2 sin(θ) cos(θ) ( u × v ) sin2(θ) ( ( u · v ) u + ( u · u ) v ) ( u · u ) v = || u || 2 v = v = sin2(θ) ( u · v ) u + cos2(θ) v + 2 sin(θ) cos(θ) ( u × v ) + sin2(θ) ( u · v ) u sin2(θ) v = 2sin2(θ) ( u · v ) u + ( cos2(θ) sin2(θ) ) v + 2 sin(θ) cos(θ) ( u × v ) sin2(θ) = 1 cos(2θ) 2 cos2(θ) sin2(θ) = cos(2θ) 2 sin(θ) cos(θ) = sin(2θ) = (1cos(2θ)) ( u · v ) u + cos(2θ) v + sin(2θ) ( u × v )

This has just become Rodrigues' rotation formula. As shown previously, this can be converted directly into the matrix-vector multiplication:
[ cos(2θ) + (1cos(2θ)) ux2 sin(2θ)uz + (1cos(2θ))uxuy sin(2θ)uy + (1cos(2θ))uxuz sin(2θ)uz + (1cos(2θ))uxuy cos(2θ) + (1cos(2θ)) uy2 sin(2θ)ux + (1cos(2θ)) uy uz sin(2θ)uy + (1cos(2θ)) ux uz sin(2θ)ux + (1cos(2θ)) uy uz cos(2θ) + (1cos(2θ)) uz2 ] [ vx vy vz ]

This rotation matrix can be generated directly from a given quaternion q =a+bi+cj+dk = cos(θ) + sin(θ)uxi + sin(θ)uyj + sin(θ)uzk =euθ with the following substitutions using trigonometric identities:
[ (2a21) + 2b2 2ad + 2bc 2ac + 2bd 2ad + 2bc (2a21) + 2c2 2ab + 2cd 2ac + 2bd 2ab + 2cd (2a21) + 2d2 ] [ vx vy vz ]

Then simplifying a bit more with factoring and the fact that a2+b2+c2+d2=1:
[ 12(c2+d2) 2(bcad) 2(ac+bd) 2(ad+bc) 12(b2+d2) 2(cdab) 2(bdac) 2(ab+cd) 12(b2+c2) ] [ vx vy vz ]

Euler angle's to quaternions:
A quaternion can be created from Euler angles as follows.

First construct a quaternion for each axis:
qx = ( cos(θx) , sin(θx) ( 1i + 0j + 0k ) ) = ( cos(θx) , sin(θx) i ) qy = ( cos(θy) , sin(θy) ( 0i + 1j + 0k ) ) = ( cos(θy) , sin(θy) j ) qz = ( cos(θz) , sin(θz) ( 0i + 0j + 1k ) ) = ( cos(θz) , sin(θz) k )

Then combine them all to form the combined quaternion:
qxqyqz = ( cos(θx) , sin(θx) i ) ( cos(θy) , sin(θy) j ) ( cos(θz) , sin(θz) k ) = ( cos(θx) cos(θy) sin(θx) sin(θy) (i·j) , sin(θx) cos(θy) i + sin(θy) cos(θx) j + sin(θx) sin(θy) (i×j) ) ( cos(θz) , sin(θz) k ) = ( cos(θx) cos(θy) , sin(θx) cos(θy) i + sin(θy) cos(θx) j + sin(θx) sin(θy) k ) ( cos(θz) , sin(θz) k ) = cos(θx) cos(θy) cos(θz) ( sin(θx) cos(θy) i + sin(θy) cos(θx) j + sin(θx) sin(θy) k ) · sin(θz) k + cos(θz) ( sin(θx) cos(θy) i + sin(θy) cos(θx) j + sin(θx) sin(θy) k ) + cos(θx) cos(θy) sin(θz) k + ( sin(θx) cos(θy) i + sin(θy) cos(θx) j + sin(θx) sin(θy) k ) × sin(θz) k = cos(θx) cos(θy) cos(θz) sin(θx) sin(θy) sin(θz) + sin(θx) cos(θy) cos(θz) i + cos(θx) sin(θy) cos(θz) j + sin(θx) sin(θy) cos(θz) k + cos(θx) cos(θy) sin(θz) k sin(θx) cos(θy) sin(θz) j + sin(θy) cos(θx) sin(θz) i = cos(θx) cos(θy) cos(θz) sin(θx) sin(θy) sin(θz) + ( sin(θx) cos(θy) cos(θz) + cos(θx) sin(θy) sin(θz) ) i + ( cos(θx) sin(θy) cos(θz) sin(θx) cos(θy) sin(θz) ) j + ( sin(θx) sin(θy) cos(θz) + cos(θx) cos(θy) sin(θz) ) k

Quaternion to axis angle:
Let q=(cos(θ2),sin(θ2)u) be the quaternion used to represent a rotation of θ radians around the axis u.

The angle of rotation θ can be read from the scalar component qs=cos(θ2):
θ = 2arccos(qs)

The axis of rotation u can be obtained by normalizing the vector component qv=sin(θ2)u:
qv ||qv|| = sin(θ2)u || sin(θ2)u || = u

Rotation matrix to quaternion:
Let R be the rotation matrix generated from the quaternion q=(cos(θ),sin(θ)u):
R = [ 12(c2+d2) 2(bcad) 2(ac+bd) 2(ad+bc) 12(b2+d2) 2(cdab) 2(bdac) 2(ab+cd) 12(b2+c2) ]

The quaternion can be read from the matrix as follows:
R11 + R22 + R33 = (12(c2+d2)) + (12(b2+d2)) + (12(b2+c2)) = 3 4(b2+c2+d2) Note: a2+b2+c2+d2=1 b2+c2+d2=1a2 since qis a unit quaternion = 3 4(1a2) =4a21 R11 + R22 + R33 + 1 4 = 12 R11+R22+R33+1 = a R23R32 4a = 2(ab+cd) 2(cdab) 4a = 4ab 4a = b R31R13 4a = 2(ac+bd) 2(bdac) 4a = 4ac 4a = c R12R21 4a = 2(ad+bc) 2(bcad) 4a = 4ad 4a = d

This only works if a is greater than zero. These are the other equivalent calculations if a is zero:

R11 R22 R33 = (12(c2+d2)) (12(b2+d2)) (12(b2+c2)) = 4b2 1 R11 R22 R33 + 1 4 = 12 R11 R22 R33 + 1 = b R23 R32 4b = 2(ab+cd) 2(cdab) 4b = 4ab 4b = a R12 + R21 4b = 2(ad+bc) + 2(bcad) 4b = 4bc 4b = c R31 + R13 4b = 2(ac+bd) + 2(bdac) 4b = 4bd 4b = d


R11 + R22 R33 = (12(c2+d2)) + (12(b2+d2)) (12(b2+c2)) = 4c2 1 R11 + R22 R33 + 1 4 = 12 R11 + R22 R33 + 1 = c R31 R13 4c = 2(ac+bd) 2(bdac) 4c = 4ac 4c = a R12 + R21 4c = 2(ad+bc) + 2(bcad) 4c = 4bc 4c = b R23 + R32 4c = 2(ab+cd) + 2(cdab) 4c = 4cd 4c = d


R11 R22 + R33 = (12(c2+d2)) (12(b2+d2)) + (12(b2+c2)) = 4d2 1 R11 R22 + R33 + 1 4 = 12 R11 R22 + R33 + 1 = d R12 R21 4d = 2(ad+bc) 2(bcad) 4d = 4ad 4d = a R31 + R13 4d = 2(ac+bd) + 2(bdac) 4d = 4bd 4d = b R23 + R32 4d = 2(ab+cd) + 2(cdab) 4d = 4cd 4d = c

Sources: