Welcome to Omni's quaternion calculator, where we'll deal with this mysterious extension of complex numbers: the quaternions. In short, we represent them using four real values, each corresponding to one of the basic unity quaternions:
k. To some, they may seem like an artificial creation to make math even more tricky than it already is. To others, they are a useful tool in 3D geometry: mainly to study quaternion rotation. Unfortunately, this doesn't kill the "tricky" part, and, e.g., quaternion multiplication is not a straightforward operation.
But let's not get ahead of ourselves! We begin with where every math topic begins: with an introduction. In our case, it's the quaternion definition.
The quaternion definition
Quaternions are an extension of complex numbers. They were first introduced by Sir William Hamilton, who used them to describe several properties of the three-dimensional space (for more, see the dedicated section).
However, nowadays, most often, we introduce them from the algebraic point of view. Let's take a look at the formal quaternion definition.
|💡 Quaternions are expressions of the form |
By the above quaternion definition, we see that the space is spanned by four base elements:
k. The three letters don't stand for any particular value: they simply denote independent base vectors. Nevertheless, if
i seems familiar, it well should! In fact, if the coefficients of
d in the above quaternion definition) are both zero, then we obtain the (well-known) complex numbers! As such, we indeed obtain their extension.
More or less, Hamilton's idea was to have one expression whose individual parts (i.e., the base elements' coefficients) describe the distinct directions in a three-dimensional space. From there, he only needed to introduce operations between these new thingies. On the one hand, we'd like them to satisfy some properties and form a nice algebraic structure. On the other, the operations should be usable and have real-life explanations. After all, quaternions originated from geometry — a very real-life area of mathematics.
Now that we know what a quaternion is, we can go further. The subsequent sections describe the basic rules governing these numbers. Note how Omni's quaternion calculator lets you find all values and objects we mention. For more information on how to use the tool, see the last section.
Adding and subtracting quaternions
This one's easy. Recall from the above section that quaternions are spanned by four base elements:
k. As such, if we want to add or subtract two such expressions, we do it the same way as in any vector space: move from one base element to the other and add or subtract the respective coefficients of the two quaternions. And since these coefficients are simply real numbers, it boils down to the very basics of mathematics which we learned in primary school.
(a + b*i + c*j + d*k) + (e + f*i + g*j + h*k) = (a + e) + (b + f)*i + (c + g)*j + (d + h)*k,
(a + b*i + c*j + d*k) - (e + f*i + g*j + h*k) = (a - e) + (b - f)*i + (c - g)*j + (d - h)*k.
Quaternion multiplication and division
In real and complex numbers, we got used to the distributive property of multiplication over addition, so we'd like to have a similar law here. After all, quaternions are sums of four elements: the real part and the
k parts. In other words, we want to have:
(a + b*i + c*j + d*k) * (e + f*i + g*j + h*k)
= a * (e + f*i + g*j + h*k)
+ b*i * (e + f*i + g*j + h*k)
+ c*j * (e + f*i + g*j + h*k)
+ d*k * (e + f*i + g*j + h*k).
And indeed, it is so. However, we now have to explain how quaternity multiplication works on the individual parts, i.e., on the basic unity quaternions. The following table contains the answer:
For completeness, let's write the individual products below:
1 * i = i * 1 = i,
1 * j = j * 1 = j,
1 * k = k * 1 = k,
i² = j² = k² = -1,
i * j = k,
j * k = i,
k * i = j,
j * i = -k,
k * j = -i,
i * k = -j.
In particular, we see that quaternion multiplication is not commutative. However, it is associative, which means the quaternion space is not a field (unlike, e.g., the real or complex numbers). Instead, it's a division ring since every non-zero quaternion has an inverse, as we'll see in the next section.
Coming back to the formula for the product of two quaternions, we can now use the rules given above to write:
(a + b*i + c*j + d*k) * (e + f*i + g*j + h*k)
= a*e - b*f - c*g - d*h
+ (a*f + b*e + c*h - d*g) * i
+ (a*g - b*h + c*e + d*f) * j
+ (a*h + b*g - c*f + d*e) * k.
The operation is often called the Hamilton product in honor of its creator.
Recall from the first section that the idea behind the quaternion definition was for the
4-tuples to represent the three-dimensional space. A keen eye may observe that if we multiply two quaternions whose real parts are zero (i.e.,
a = e = 0 above), then we get precisely the same formula as that of the cross product of two three-dimensional vectors with
k corresponding to the elementary basis
As for dividing quaternions, the matter is even more tricky. As you may know, algebraic structures don't have the operation of division, per se. The expression
x / y is simply an abbreviation of
x * y⁻¹. What is more, in real or complex numbers, we have
x / y = x * y⁻¹ = y⁻¹ * x.
On the other hand, we've already mentioned that quaternion multiplication is not commutative, so in general,
x * y⁻¹ is not the same as
y⁻¹ * x. Therefore, Omni's quaternion calculator returns both possibilities for the two operations, i.e.,
x * y and
y * x for multiplication, and
x * y⁻¹ and
y⁻¹ * x for division.
Either way, division requires finding
y⁻¹, i.e., the multiplicative inverse of a quaternion. And how do we do that?
The magnitude, conjugate, inverse, and matrix representation
The magnitude (or norm), conjugate, inverse, and matrix representation are all assigned to a single quaternion. Below, we explain them one by one.
The magnitude (norm) of a quaternion
If the name rings a bell, and you feel there was something similar connected with vectors, you're on the right track. As mentioned in the first section, quaternions are simply
4-tuples of real numbers: same as vectors of a four-dimensional Euclidean space. Those vectors all had their magnitude, which basically described their length. So why don't we repeat the reasoning here?
‖a + b*i + c*j + d*k‖ = √(a² + b² + c² + d²).
Note how, by definition, the value is always a non-negative real number. What is more, it's equal to zero if and only if the quaternion is zero, i.e.,
0 + 0*i + 0*j + 0*k.
Lastly, observe that if we divide a quaternion by its norm (i.e., divide each of its coefficients), we'll obtain a new quaternion whose magnitude is
1. This simple fact will become quite useful very soon.
The conjugate of a quaternion
Recall how the conjugate of a complex number gave the number with the same real part but the opposite imaginary part. This time, we're dealing with expressions of the form
a + b*i + c*j + d*kthat have one real part (i.e.,
a) and three non-real ones (i.e.,
d). Nevertheless, the concept stays the same: we keep the first unchanged and flip the signs in the others.
To be precise, the quaternion conjugate formula states that the conjugate of
a + b*i + c*j + d*kis
a - b*i - c*j - d*k.
We denote the conjugate of a quaternion
q(as above), but some sources also use
Also, observe that if we multiply a quaternion by its conjugate in whichever order, we'll obtain a difference of squares:
q * q = (a + bi + cj + dk) * (a - bi - cj - dk)
= a² - (b*i + c*j + d*k)².
If we expand it according to the rules from the above section, we'll get:
q * q = q * q = a² + b² + c² + d² = ‖q‖².
Again, a simple fact that will prove very useful.
The inverse of a quaternion
Let's begin with the obvious: only non-zero quaternions have an inverse. Secondly, even with quaternion multiplication not being commutative, every non-zero
qhas its inverse
q * q⁻¹ = q⁻¹ * q = 1, where
1 = 1 + 0*i + 0*j + 0*k.
To find the reciprocal, recall that for every quaternion
q, we have
q * q = q * q = ‖q‖². For a non-zero
q, this gives
q * (q / ‖q‖²) = (q / ‖q‖²) * q = 1, which means that:
q⁻¹ = q / ‖q‖².
(Note how in the above section, we mentioned some issues with dividing quaternions. However, here, we divide by a positive real number, i.e., divide each of the quaternions coefficients. In other words, everything is fine.)
Complex numbers are pairs of real numbers, and quaternions are
4-tuples of real numbers. And just as we can represent the prior as
2 x 2matrices, we can have the latter as
4 x 4ones. The matrix representation basically takes the quaternion's coefficients and puts them in the right places with the right signs (which come from the relations between unity quaternions). To be precise, we can write
q = a + b*i + c*j + d*kas:
⌈ a -b -c -d ⌉ | b a -d c ｜ | c d a -b ｜ ⌊ d -c b a ⌋
In fact, alternatively, we can also use an equivalent
2 x 2matrix representation with complex entries:
⌈ a + b*i c + d*i ⌉ ⌊ -c + d*i a - b*i ⌋
To motivate the concept, let us mention a couple of its properties: the magnitude
‖q‖is equal to the square root of the matrix' determinant, and the conjugate
qcorresponds to the conjugate transpose.
Alright, we've learned the basic operations, including quaternions, but so far, we've only seen formulas that may seem useless by themselves. Now, it's time for some practical uses! We'll get back to geometry, learn to convert a quaternion to a rotation matrix, and see how it all works in the three-dimensional space.
3D geometry: quaternion rotation
The three-dimensional Euclidean space consists of points
(x, y, z) with arbitrary real numbers as coordinates. If we see it as a vector space, it's spanned by three elements:
(1, 0, 0),
(0, 1, 0), and
(0, 0, 1), which we call the elementary basis. On the other hand, there are four basic unity quaternions:
k. However, if we think of the non-real ones as analogous to the elementary euclidean vectors, we can use them for quaternion rotation.
To have rotation, we need an axis around which we rotate and an angle. The prior will simply be a straight line, but for calculations, we don't really need any line equations. In fact, a single non-zero vector will suffice: it uniquely defines a line by setting its direction.
Clearly, we can define a line using many different vectors as long as they are scaled versions of one another. Therefore, for our purposes, we'll stick to unit vectors (i.e., those of length
1). You can transform any non-zero vector
v into a unit one by dividing it (i.e., dividing all its coordinates) by its length:
v / |v|. Obviously, we could alter the formulas to allow other vectors, but it's far simpler to write them with this simple assumption. Nevertheless, note that Omni's quaternion calculator allows non-unit ones: it simply converts them itself.
Now, let's cover two topics connecting quaternions to rotation, one by one.
Rotation around an axis
Suppose a unit vector
vₐ = (xₐ, yₐ, zₐ)defines a line in a three-dimensional space.
Then, we can represent the rotation around the line by an angle
θby the quaternion:
q = cos(θ/2) + (xₐ*i + yₐ*j + zₐ*k) * sin(θ/2)
= cos(θ/2) + (xₐ * sin(θ/2)) * i + (yₐ * sin(θ/2)) * j + (zₐ * sin(θ/2)) * k.
Alternatively, we can recall that a three-dimensional space consists of triples of real numbers. Therefore, we can choose to write such triples as matrices with three rows and one column. Then, if we write the
q = a + b*i + c*j + d*k, we can represent the operation with a
3 x 3matrix, i.e., we can change a quaternion to a rotation matrix given by the following formula (note how this is a very different concept than the matrix representation in the above section):
|⌈||1 - 2*(c² + d²)||2*(b*c - a*d)||2*(b*d + a*c)||⌉|
||||2*(b*c + a*d)||1 - 2*(b² + d²)||2*(c*d - a*b)||｜|
|⌊||2*(b*d - a*c)||2*(c*d + a*b)||1 - 2*(b² + c²)||⌋|
Point 1. tells us how to "store" the data and the idea of rotating around an axis. Point 2. tells us what we get when we apply the rotation to a given vector
v = (x, y, z).
The first option is to use the rotation matrix
Rabove and multiply it by
v(represented as a one-column matrix). Then, the vector
v' = R * vis the result of rotating
varound the axis given by
vₐ = (xₐ, yₐ, zₐ)by an angle
Alternatively, we can use quaternions and quaternion multiplication. However, before we begin, we need to understand
vas a quaternion: after all, right now, it's simply a triple of real numbers.
Define a quaternion
qᵥ = 0 + x*i + y*j + z*k. In this language, we the rotated vector
qᵥby the rotation quaternion
qfrom point 1. To be precise, if we set
qᵥ' = q * qᵥ * q⁻¹, then the result will take the form
qᵥ' = 0 + x'*i + y'*j + z'*k, and from there we read off the vector:
v' = (x', y', z'). In fact, in this case, the rotation quaternion
qis unitary (i.e., has magnitude
q⁻¹ = q, meaning we could have used the conjugation instead of inverse above.
That concludes the theoretical part. Let's now make good use of the knowledge of what a quaternion is and all the formulas and take on an example or two.
Example: using the quaternion calculator
The above sections took us through all the functionalities of Omni's quaternion calculator. Arguably, there are quite a few, so let's try out a couple to get a taste. We'll find:
- The result of quaternion multiplication of
q₁ = 2 - i + 3j + kand
q₂ = 5 - 4i + k, and
- The quaternion of rotation corresponding to the line given by the vector
vₐ = (1, 0, -1)and the angle
θ = 60°.
Obviously, we'll apply the formulas we've learned. However, before we do that, we let the quaternion calculator do the talking and work out the solution for us.
To multiply two quaternions using our tool, we begin by telling the calculator what we want from it. In our case, this means choosing "product" from the list in the variable field "I want to find the…" That will trigger two sections underneath, each dedicated to one of the quaternions. Note how at the top of them, we can see the symbolic representation that is used below:
q₁ = a + bi + cj + dkand
q₂ = e + fi + hj + hk. Looking back at the example, we input:
a = 2,
b = -1,
c = 3,
d = 1,
e = 5,
f = -4,
g = 0,
h = 1.
(Note how we have
b = -1,
d = 1, and
h = 1even though there were no numbers in the corresponding places in
q₂above. That is because, by convention, we don't write
1s in front of variables. Also, observe that we input
g = 0since
q₂has no summand with the unity quaternion
j: that means its coefficient is, in fact,
Once we write the last entry, the quaternion calculator will spit out the answer underneath.
Again, we begin by stating what we need. This time, we choose "quaternion of rotation" under "I want to find the…" This will trigger two sections: one for the coordinates of the vector defining the rotation axis and one for the angle. The prior is written symbolically as
vₐ = (xₐ, yₐ, zₐ), and the latter simply by
θ. Looking back at the example, we input:
xₐ = 1,
yₐ = 0,
zₐ = -1,
θ = 60°.
(Note how the tool lets you change the angle unit if needed.) The moment we give the last value, the quaternion calculator presents the answer underneath. Note how apart from the rotation quaternion, it also converts the quaternion to a rotation matrix.
Alright, we've played around a bit; now it's time to get our hands dirty. Surely, we won't be as fast as the calculator, but let's go through the calculations nevertheless.
Here, we simply follow the quaternion multiplication rules described in the dedicated section, i.e., use the distributive property of multiplication over addition, and compute the products of basic unity quaternions.
q₁ * q₂ = (2 - i + 3j + k) * (5 - 4i + k)
= 2 * (5 - 4i + k) - i * (5 - 4i + k) + 3j * (5 - 4i + k) + k * (5 - 4i + k)
= 10 - 8i + 2k - 5i - 4 + j + 15j + 12k + 3i + 5k - 4j - 1
= 5 - 10i + 12j + 19k.
To find the quaternion of rotation, we begin by finding the unitary equivalent of the vector
v. For that, we start with its length:
|v| = √(1² + 0² + (-1)²) = √(1 + 0 + 1) = √2,
and use the appropriate formula:
v / |v| = (1 / √2, 0 / √2, -1 / √2) = (0.5√2, 0, -0.5√2).
Now, it's enough to use the formula from the dedicated section:
q = cos(60° / 2) + (0.5√2 * i + 0 * j - 0.5√2 * k) * sin(60° / 2)
= 0.5√3 + (0.5√2 * i - 0.5√2 * k) * 1/2
= 0.5√3 + 0.25√2 * i - 0.25√2 * k.
Lastly, if we choose to convert the quaternion to a rotation matrix, the same section provides the formula to do so:
⌈ 1 - 2 * (0² + (-√2/4)²) 2 * (√2/4 * 0 - √3/2 * (-√2/4)) 2 * (√2/4 * (-√2/4) + √3/2 * 0) ⌉ | 2 * (√2/4 * 0 + √3/2 * (-√2/4)) 1 - 2 * ((√2/4)² + (-√2/4)²) 2 * (0 * (-√2/4) - √3/2 * √2/4) ｜ ⌊ 2 * (√2/4 * (-√2/4) - √3/2 * 0) 2 * (0 * (-√2/4) + √3/2 * √2/4) 1 - 2 * ((√2/4)² + 0²) ⌋
⌈ 0.75 √6/4 -0.25 ⌉ | -√6/4 0.5 -√6/4 ｜ ⌊ -0.25 √6/4 0.75 ⌋
Phew, that was some work, wouldn't you say? And the sine and cosine were fairly simple, so it can even longer in general. Oh, it's a good thing we have Omni's quaternion calculator to spare us the hassle!
How do I use quaternions for rotation?
To use quaternions for rotation, you need to:
- Identify the vector defining the axis of rotation.
- If needed, find its unit equivalent.
- The quaternion of rotation is
q = cos(θ/2) + (xₐ*i + yₐ*j + zₐ*k) * sin(θ/2), where:
vₐ = (xₐ, yₐ, zₐ)is the unit vector defining the axis; and
θis the angle by which you rotate.
- If needed, rotate
vusing the formula
qᵥ' = q * qᵥ * q⁻¹, where:
v = (x, y, z)is the vector you rotate;
qis as in step 3;
q⁻¹is the multiplicative inverse of
qᵥ = x*i + y*j + z*k;
qᵥ' = 0 + x'*i + y'*j + z'*k, then
v' = (x', y', z'); and
v'is the result of rotating
How do I multiply quaternions?
To multiply quaternions, you need to:
- Use the distributive property of multiplication over addition.
- Multiply basic unity quaternions according to the rules:
1 * i = i * 1 = i,
1 * j = j * 1 = j,
1 * k = k * 1 = k;
i² = j² = k² = -1;
i * j = k,
j * k = i,
k * i = j; and
j * i = -k,
k * j = -i,
i * k = -j.
- Simplify by combining common summands.
- Enjoy your quaternion product.
Are all rotations expressible as unit quaternions?
Yes, we can express any rotation around an axis in a three-dimensional space by a unit quaternion. By default, the formulas are for axes going through the origin, but you can always compose the rotation with a translation to suit your purposes.
Are the quaternions a field?
No. Quaternion multiplication is not commutative, which is a must-have for a structure to be a field. However, all other field properties are satisfied, so, in the end, quaternions form a division ring.
Are the quaternions commutative?
No, or, to be precise, quaternion multiplication isn't commutative. On the other hand, quaternion addition does commute.