# Quaternion Calculator

Created by Maciej Kowalski, PhD candidate
Reviewed by Anna Szczepanek, PhD and Steven Wooding
Last updated: Apr 06, 2022

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: 1, i, j, and 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 : 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 , who used them to describe several properties of the three-dimensional space (for more, see ).

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 q = a + b*i + c*j + d*k, where a, b, c, and d are arbitrary real numbers and i, j, and k are base elements sometimes called the basic unity quaternions.

By the above quaternion definition, we see that the space is spanned by four base elements: 1, i, j, and 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 j and k (i.e., c and 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 — 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 .

This one's easy. Recall from that quaternions are spanned by four base elements: 1, i, j, and 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.

Plain sailing, wouldn't you say? Such addition satisfies all the reasonable properties: it's associative, commutative, and every quaternion q has its opposite -q such that q + (-q) = (-q) + q = 0.

Unfortunately, the story is a bit more difficult for multiplication and even more so for division.

## 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 i, j, and 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:

·1 ·i ·j ·k
1 i j k
i -1 k -j
j -k -1 i
k j -i -1

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 .

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 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 i, j, and k corresponding to the elementary basis (1,0,0), (0,1,0), and (0,0,1), respectively.

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.

1. 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 , 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?

Recall that the magnitude of a vector (in whichever dimension) is the square root of the sum of its coordinates' squares. Analogously, the magnitude (or norm) of a quaternion is:

‖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.

2. 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*k that have one real part (i.e., a) and three non-real ones (i.e., b, c, and 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*k is a - b*i - c*j - d*k.

We denote the conjugate of a quaternion q by q (as above), but some sources also use qᵗ, q̃, or q*.

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 , we'll get:

q * q = q * q = a² + b² + c² + d² = ‖q‖².

Again, a simple fact that will prove very useful.

3. 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 q has its inverse q⁻¹ such that 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 , 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.)

4. Matrix representation

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 2 matrices, we can have the latter as 4 x 4 ones. 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*k as:

 ⌈ 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 2 matrix 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 q corresponds 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: 1, i, j, and 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.

1. 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 above as q = a + b*i + c*j + d*k, we can represent the operation with a 3 x 3 matrix, 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 ):

 ⌈ 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²) ⌋
1. Vector rotation

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 R above and multiply it by v (represented as a one-column matrix). Then, the vector v' = R * v is the result of rotating v around 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 v as 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 v' from conjugating qᵥ by the rotation quaternion q from 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 q is unitary (i.e., has magnitude 1), so 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:

1. The result of quaternion multiplication of q₁ = 2 - i + 3j + k and q₂ = 5 - 4i + k, and
2. 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.

1. 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 + dk and 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 = 1 even though there were no numbers in the corresponding places in q₁ and q₂ above. That is because, by convention, we don't write 1s in front of variables. Also, observe that we input g = 0 since q₂ has no summand with the unity quaternion j: that means its coefficient is, in fact, 0.)

Once we write the last entry, the quaternion calculator will spit out the answer underneath.

2. 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.

1. Here, we simply follow the quaternion multiplication rules described in , 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.

2. 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 :

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²) ⌋

which is:

 ⌈ 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!

## FAQ

### How do I use quaternions for rotation?

To use quaternions for rotation, you need to:

1. Identify the vector defining the axis of rotation.
2. If needed, find its unit equivalent.
3. 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.
4. If needed, rotate v using the formula qᵥ' = q * qᵥ * q⁻¹, where:
• v = (x, y, z) is the vector you rotate;
• q is as in step 3;
• q⁻¹ is the multiplicative inverse of q;
• qᵥ = x*i + y*j + z*k;
• if qᵥ' = 0 + x'*i + y'*j + z'*k, then v' = (x', y', z'); and
• v' is the result of rotating v.

### How do I multiply quaternions?

To multiply quaternions, you need to:

1. Use the distributive property of multiplication over addition.
2. 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.
3. Simplify by combining common summands.

### 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.

Maciej Kowalski, PhD candidate
I want to find the
sum
.
q₁ = a + bi + cj + dk
Real part (a)
The i coefficient (b)
The j coefficient (c)
The k coefficient (d)
q₂ = e + fi + hj + hk
Real part (e)
The i coefficient (f)
The j coefficient (g)
The k coefficient (h)
People also viewed…