# Understanding Quaternions

1. Aug 24, 2013

### Septimra

So I have been heavily trying to understand rotations.
Rotations as i understand is a planar phenomenon. You need at least two dimensions.
That is why rotations cannot work in dim 1.

With 2 dimensions, rotations happen in the only plane that exists: XY. However the axis of rotation cannot actually be described in ℝ2 you have to go to ℝ3to define the primary axis of rotation.

In ℝ3 is different because there are 3 planes of rotation: XY, XZ, YZ.
The problem comes from properly and effectively interpolating and combining rotations.
So just like we did in 2D we can define an axis or plane in ℝ4 all X,Y, and Z vectors rotate around. Or the planes XY, XZ, YZ rotate around. This is the w axis/component.

So now if my intuition is correct then can you please explain how exactly quaternions fit into this and why we do not quaternions defined for 2D rotation like 3D rotation.

A complex number is sufficient for a rotation in 2d because there is only one plane, and a combination of planes are not require, right?

So should there not be a way to define a 2d rotation in terms of an 3d axis like 4-vector quaternion does not 3d?

Thank you so much!

2. Aug 25, 2013

### D H

Staff Emeritus
Defining rotation in ℝ2 as being about an axis perpendicular to the plane doesn't make sense. There is no direction perpendicular to the plane in ℝ2. The concept of an axis of rotation is unique to ℝ3. Consider a rotation in ℝ4 where an object is simultaneously rotating about the XY and WZ planes. There is no axis of rotation here. The XY and WZ planes intersect at one point, the origin, rather than a line.

One way to think of rotation is through proper orthogonal matrices with real elements. The nxn real rotation matrices are one way to represent the group SO(n). These are Lie groups. A Lie algebra is associated with every Lie group. In the case of rotations, these Lie algebras are skew symmetric matrices that describe angular velocity. Once again, rotations in 3D are unique in that a 3x3 skew symmetric matrix behaves a lot like a 3-vector.

With regard to using unit quaternions to represent rotations in 3 space, the 2D analog is the set of unitary complex numbers. There is no good analog for higher dimension spaces (other than the rotation matrices themselves).

3. Aug 25, 2013

### Septimra

I kinda understand what you are saying, but what I am trying to get at is that if you rotate an object in 3d space along the z-axis and project the XY plane you get an understandable rotation. The same can be extended into 4d. If an object is rotated in 4 dimensional space about the w axis and is projected on the respective planes XY YZ ZX to 3d the image becomes understandable in 3d. Is that not how quaternions work? If not, where did I go wrong?

But to follow it up, it does appear that complex numbers rotate in 2d and quaternions rotate in 3d, why? Is it because when you add another dimension to 2d space you get two extra planes hence 2 extra imaginaries?

Also can you point me in the right direction for Lie Groups, I have never heard of them before now.

Thanks again!

4. Aug 27, 2013

### D H

Staff Emeritus
A very brief synopsis of group theory: A group (G,•) is a set G and a binary operator • that operates on elements of G and that satisfies four conditions:
1. Closure. For any two elements a,b in G, ab is also a member of G.
2. Transitivity. For any three elements a,b,c in G, (ab)•c=a•(bc).
3. Identity element. The group G contains an element e such that ae=ea=a for all elements a in G.
4. Invertibility. For every element a in G, there exists an element b such that ab=ba=e.

A simple example is (ℤ,+), the integers with the group operation being addition. Addition is closed and transitive, has 0 as the identity element, and the additive inverse satisfies the invertibility axiom. Similarly, (ℝ,+) and (ℂ,+) also form groups.

A more complex example is the set of n×n orthogonal matrices with real elements with matrix multiplication as the group operation. This is the group O(n,ℝ), or just O(n) if it's clear from the context that one is talking about matrices with real elements. The determinant of an orthogonal matrix is either +1 or -1. The subset of orthogonal matrices with determinant +1 also form a group. These are the special orthogonal matrices, SO(n,ℝ) (or just SO(n) if the context is clear).

Another set of matrices that form a group with matrix multiplication as the group operation are the unitary matrices. A nx[/i]n[/i] matrix U with complex elements is unitary if UU*=U*U=I where U* is the conjugate transpose of U. Once again, the subset of these matrices whose determinant is +1 is of special interest. These are the special unitary matrices, SU(n,ℂ), and like the special orthogonal matrices, the special unitary matrices with complex elements and with matrix multiplication as the group operation form a group.

Why all this interest in group theory? Rotations in n-dimensional space are isomorphic to the group SO(n) (SO(n,ℝ) to be more specific). What lets us use unit complex numbers to represent rotations in 2-space and unit quaternions to represent rotations in 3-space are the connections between the groups U(1,ℂ) and SO(2,ℝ) and between SU(2,ℂ) and SO(3,ℝ). The unit complex numbers are isomorphic to U(1,ℂ) while the unit quaternions are isomorphic to SU(2,ℂ).

5. Sep 16, 2013

### A David

We don't need an axis to rotate around in 2d, a point works just fine.

3d real space is special because we can equip it with the vector cross product. We can do an analogous operation in higher dimensions but we don't get a vector back.

Notice that the quaternions behave just like taking cross products of unit basis vectors in 3d space: If i=[1 0 0], j=[0 1 0], k=[0 0 1], then ij=k, jk=i, ik=j, with a2=-1 and ab=-ba for a~=b, a,b not 1 or -1. The whole set (group) is eight elements {1,i,j,k,-1,-i,-j,-k} hence the Q8. I't turns out that this group structure is important when talking about the directions of vectors in a space, and the posts above go into more detail about that.

An analogous group for 2d is {1,i,-1,-i}, where i acts like (is) the imaginary unit.

6. Sep 20, 2013

### Septimra

I think i see, it now, but I might need to ponder on it a little bit longer. This is really all new to me, rotations that is. I am working on a game engine and what to properly understand the mathematics of what I am using.

The message that I am getting is that quaternions and complex numbers are isomorphic to certain sets of numbers. Isomorphic mathematically means?

There are different vector spaces for matrices. You have the orthogonal, and the special orthogonal with determinant +1.

Next up there is another vector space formed by unitary vectors. That means that a matrix with complex elements where the (conjugate of the transpose * matrix) = (matrix * conjugate of the transpose) = Identity.

Therefore the connections that these numbers have with these unitary vector spaces is what allows for rotation?

7. Sep 20, 2013

### A David

Isomorphic is from the Greek, meaning "same form." Formally, it means that there exists a bijective map between the sets that preserves the operations of the group (for a group isomorphism) or ring (for a ring isomorphism). Informally, two objects that are isomorphic can be thought of as the same object represented in two different ways.

8. Sep 20, 2013

### D H

Staff Emeritus
Naively, "the same as" or "very similar to". Give two mathematical structures X and Y, there is at a minimum a bijective mapping between the two structures. If the two structures are a group, when one says X and Y are isomorphic to one another implicitly means a group isomorphism. Given two elements in X, if you apply the group operation to two elements in X and then transform to Y you'll get the same result if you had transformed each element to Y and performed the group operation in Y. The reverse needs to be true as well (given two elements in Y ...). In the case of Lie groups, an isomorphism must also preserve the things that make each group a Lie group. In general isomorphism is a very strong statement.

The relationship between complex numbers and the unit circle is something you learned when you were a kid. It's Euler's formula, $e^{ix} = \cos x + i\sin x$. The connection with 2D rotation matrices should also be obvious: $\begin{bmatrix} \cos x & -\sin x \\ \sin x & \phantom{-}\cos x \end{bmatrix}$. It's one-to-one-to-one.

It's a bit trickier for quaternions. Once again, there's an obvious isomorphism between the unit quaternions and the 3-sphere (the unit sphere in four dimensional space). The issue is that the relation between unit quaternions and 3D rotation matrices is *not* an isomorphism. For every unit quaternion there is a unique mapping to a 3D rotation matrix, but the reverse is not true. Given some 3D rotation matrix, there are *two* quaternions that map to that matrix, so the mapping from 3D rotation matrices to unit quaternions is not one-to-one. It's one-to-two. The unit quaternions are called a "double cover" of the 3D rotation matrices. It's easy to overcome this issue because there's a simple relationship between the two quaternions that map to the same matrix: The two quaternions are additive inverses of one another.

9. Sep 20, 2013

### A David

Sorry, I was busy earlier and was only able to answer partially. It turns out that the quaternions are isomorphic not to numbers but to sets of matrices with complex entries. In fact, we know that all groups are isomorphic to a group of matrices with complex entries. The quaternions are isomorphic to the group:

$\begin{array}{cccc} 1 = \left( \begin{array}{cc} 1 & 0 \\ 0 & 1 \\ \end{array} \right) & \mathbf{i} = \left( \begin{array}{cc} i & 0 \\ 0 & -i \\ \end{array} \right) & \mathbf{j} = \left( \begin{array}{cc} 0 & 1 \\ -1 & 0 \\ \end{array} \right) & \mathbf{k} = \left( \begin{array}{cc} 0 & i \\ i & 0 \\ \end{array} \right) \end{array}$

and their additive inverses, where the $i$ inside the matrices is the imaginary unit. This a very important subgroup of $SL_2(\mathbb{C})$. The the group operation is matrix multiplication. You should verify that the group of matrices follows the same rules as the quaternions that they represent, which verifies that they are isomorphic.

Now, we can use these matrices as a basis for a vector space over the real numbers by taking linear combinations. Consider the set of vectors

$Q = \left \{ a + b\mathbf{i} + c\mathbf{j} + d\mathbf{k} \;|\; a,b,c,d \in \mathbb R \right \} .$

You should verify that this set satisfies the axioms of a vector space (in this case over the field of real numbers). Notice that if that we let $a=0$ we have a vector in $\mathbb{R}^3,$ say $v$. This will be important later.

Now, notice that if I write out the $\mathbf{i,j,k}$ in their matrix form and carry out the scalar multiplication and matrix addition, this set is the same as the set of (technically, still "vectors," since this is a vector space) matrices:

$\mathbf{Q} = \left \{ \left( \begin{array}{cc} a+bi & c+di \\ -c+di & a-bi \\ \end{array} \right) \;|\;a,b,c,d \in \mathbb{R} \right \}$

For some $u = u_x\mathbf{i}+u_y\mathbf{j}+u_z\mathbf{k}\; \in \mathbb{R}^3,$ and $\theta \in [0,2\pi)$ consider the vector $u_\theta \in Q$ given by

$u_\theta = \cos{\frac{\theta}{2}} + u_x\sin{\frac{\theta}{2}}\mathbf{i} + u_y\sin{\frac{\theta}{2}}\mathbf{j} + u_z\sin{\frac{\theta}{2}}\mathbf{k}$

Then in $\mathbf{Q}$, the representation of $u_\theta$ is

$\mathbf{u}_\theta = \left( \begin{array}{cc} \cos{\frac{\theta}{2}} + u_x\sin{\frac{\theta}{2}}i & u_y\sin{\frac{\theta}{2}} + u_zsin{\frac{\theta}{2}}i \\ \cos{\frac{\theta}{2}}- u_y\sin{\frac{\theta}{2}} + u_z\sin{\frac{\theta}{2}}i & \cos{\frac{\theta}{2}} - u_x\sin{\frac{\theta}{2}}i \\ \end{array} \right)$

Think back to $v$, our vector in $\mathbb{R}^3.$ $v$'s representation in $\mathbf{Q}$ is simply

$\mathbf{v} = \left( \begin{array}{cc} bi & c+di \\ -c+di & -bi \\ \end{array} \right)$.

And here's where the magic happens. To rotate any vector $v$ by $\theta$ degrees around any axis $u$, we just multiply matrices in $\mathbf{Q}$:

$\mathbf{v}' = \mathbf{u}_\theta \mathbf{v} \mathbf{u}_\theta^{-1} = \mathbf{u}_\theta \mathbf{v} \mathbf{u}_{-\theta}$,

and then rewrite it in it's vector form, which is easy.

So, what we've shown is that using the quaternions, we can write the complicated operation of vector rotation in terms of the simple notation of matrix multiplication. Formally, we've shown that the map $\mathbf{v} \rightarrow \mathbf{u}_\theta \mathbf{v} \mathbf{u}_\theta^{-1}$ defines a group action of $\langle i,j,k \rangle$ on $\mathbb{R}^3$ by conjugation, where the action is a rotation about $\mathbf{u}$ by $\theta$ radians.

This is valuable not only because we can compute the rotations more easily (especially computationally), but by replacing $\mathbb{R}$ with another field, we can begin to think about describing rotations in three dimensional vector spaces over any field, and actually extend the theory to talk about rotation in a general vector space, which is incredibly powerful.

Last edited: Sep 20, 2013
10. Sep 20, 2013

### D H

Staff Emeritus
Some representational issues:

Others use $u_\theta = \cos{\frac{\theta}{2}} - u_x\sin{\frac{\theta}{2}}\mathbf{i} - u_y\sin{\frac{\theta}{2}}\mathbf{j} - u_z\sin{\frac{\theta}{2}}\mathbf{k}$

Which one is right? That's the wrong question. Both are right, if used correctly.

A couple of representational issues here.

One is that $\mathbf{u}_\theta^{-1} \mathbf{v} \mathbf{u}_\theta$ works just as well. Think of it along the lines of a similarity transform. Some write PAP-1, others P-1AP. There is no right or wrong. There's just left and right. On which side of the vector does the unconjugated quaternion lie?

One last representation issue: Does the quaternion represent a physical rotation or does it represent a way to transform coordinate representations from one reference frame to another? Wikipedia (and others) distinguish these as active versus passive transformation. I don't like that nomenclature myself; which is which?

These three choices are not independent. Choose any two and the third follows. Usually one chooses active versus passive and left versus right. These choices will dictate whether the vector (imaginary) part of the quarternion has a +sin(θ/2) or -sin(θ/2) factor.

It's a good idea to be aware that different representations exist. You have to be particularly careful when reading papers or books because a lot of authors aren't aware of all these different schemes. Don't just take the equations for granted. Make sure they are consistent with your chosen representation scheme.

Computation issues:
You don't have to go to the matrix formulation to perform the rotation or transformation. Simply view the vector to be rotated/transformed as a pure quaternion and perform the quaternion multiplications. The result will be a pure quaternion; the vector part is the rotated/transformed vector. Some shortcut optimizations become available with this approach since the result *has* to be a pure quaternion.

Rotating/transforming with a matrix involves less computations than with a quaternion. It's better to compute the matrix from the quaternion and use that if you are going to perform more than a few rotations/transformations with the same quaternion.

So why use quaternions? While it's easier computationally to rotate/transform with a matrix, it's easier to compute a kinematic chain with quaternions. Arm joint A is connected to B is connected to C is connected to an end effector. Another is stability. Multiply two orthogonal matrices and you should get an orthogonal matrix. You don't if you're using IEEE floating point. You don't get a unit quaternion when you multiply two unit quaternions, but the error is smaller and is easily corrected. Finally, there's state propagation (numerical integration). If things are moving around, how to you keep track? I've found quaternions to be much better for this than are matrices.

11. Sep 20, 2013

### A David

Notice that this is equal to $\cos{\frac{-\theta}{2}} + u_x\sin{\frac{-\theta}{2}}\mathbf{i} + u_y\sin{\frac{-\theta}{2}}\mathbf{j} + u_z\sin{\frac{-\theta}{2}}\mathbf{k}$.

It only depends on whether you want $\theta$ to correspond to a clockwise or anticlockwise rotation.

If we have a set of vectors $V$, say that generates the whole space, then I would say $\mathbf{u}_\theta V \mathbf{u}_\theta^{-1}$ is a change in the coordinate frame of reference.

Last edited: Sep 20, 2013
12. Sep 20, 2013

### D H

Staff Emeritus
No!

I'll denote the choice of $qvq^{-1}$ as using a "left" quaternion, $q^{-1}vq$ as involving a "right" quaternion. The nomenclature is based on the location of the unconjugated quaternion. It's on the left in $qvq^{-1}$, on the right in $q^{-1}vq$. I'll denote quaternions used to perform a physical rotation as a rotation quaternion, those to transform coordinates as a transformation quaternion. These choices dictate the sign convention of the imaginary part of the quaternion. (They also dictate the formulation of the quaternion derivative, which we haven't discussed yet.)

If you don't think this is important, consider that some parts of NASA uses right rotation quaternions, other parts use left transformation quaternions, and another country's space agency uses right transformation quaternions. I'm sure someone uses left rotation quaternions just to round things out.

13. Sep 20, 2013

### A David

Yes, but that's a different map.

I wouldn't say that it's not important, only note that $\mathbf{q}_\theta \mathbf{v} \mathbf{q}_\theta^{-1} = \mathbf{q}_{-\theta}^{-1} \mathbf{v} \mathbf{q}_{-\theta},$ so the inverse map takes us from left to right or vice versa. You can call it "left" or "right" or "snake" or "purple". Like you said, the convention dictates the notation. But mathematically speaking, they're equivalent, and I'm speaking mathematically- I'm familiar with vector spaces; I don't know anything about space agencies.

Last edited: Sep 20, 2013
14. Sep 20, 2013

### D H

Staff Emeritus
Sure. They are completely equivalent mathematically. However, since the OP is concerned with using quaternions numerically for a game engine, knowing about conventions can be rather important.

15. Sep 20, 2013

Of course.