# Available on quaternions and their benefits

- Transfer

*From the translator: exactly 175 years and 3 days ago quaternions were invented. In honor of this round date, I decided to choose a material that explains this concept in an understandable language.*

The quaternion concept was invented by the Irish mathematician Sir William Rowan Hamilton on Monday October 16, 1843 in Dublin, Ireland. Hamilton and his wife went to the Irish Royal Academy , and going over the Royal Canal along the Broome Bridge , he made an amazing discovery, which he immediately scrawled on the stone of the bridge.

$$

*Memorial plaque on Broome Bridge over the Royal Canal in honor of the discovery of the fundamental multiplication formula for quaternions.*

In this article I will try to explain the concept of quaternions in a simple to understand way. I will explain how you can visualize quaternion, and also talk about the different operations that can be performed with quaternions. In addition, I will compare the use of matrices, Euler angles and quaternions, and then I will try to explain when to use quaternions instead of Euler angles or matrices, and when this is not necessary.

## Content

- 1. Introduction
- 2. Complex numbers
- 2.1. Addition and subtraction of complex numbers
- 2.2. Multiply a complex number by a scalar value
- 2.3. The product of complex numbers
- 2.4. Square of complex numbers
- 2.5. Conjugate complex numbers
- 2.6. The absolute value of a complex number
- 2.7. Quotient of two complex numbers

- 3. Degrees $$
- 4. Complex plane
- 4.1. Rotors

- 5. Quaternions
- 5.1. Quaternions as an ordered pair
- 5.2. Addition and subtraction of quaternions
- 5.3. Quaternion product
- 5.4. Real Quaternion
- 5.5. Quaternion multiplication by scalar
- 5.6. Pure Quaternions
- 5.7. Additive Quaternion Form
- 5.8. Unit quaternion
- 5.9. Binary quaternion form
- 5.10. Conjugate Quaternions
- 5.11. Quaternion norm
- 5.12. Normalization of Quaternion
- 5.13. Reverse Quaternion
- 5.14. Scalar product of quaternions

- 6. Turns
- 7. Interpolation of Quaternions
- 7.1. SLERP
- 7.1.1. Quaternion Difference
- 7.1.2. Raising a quaternion to a power
- 7.1.3. Fractional Quaternion Difference
- 7.1.4. Factors to Consider

- 7.2. SQUAD

- 7.1. SLERP
- 8. Conclusion
- 9. Download demo
- 10. Reference materials

It’s impossible to fully understand quaternions in 45 minutes.

There is a lot of math in this article, so it’s not for wimps.

# Introduction

In computer graphics, matrices are used to describe the position in space (movement) as well as orientation in space (rotation). You can also use a single transformation matrix to describe the scale of the object. This matrix can be considered the “basis space”. If we multiply a vector or a point (or even another matrix) by a transformation matrix, then we “transform” this vector, point or matrix into the space represented by this matrix.

In this article I will not talk in detail about the transformation matrices. Details on transformation matrices can be found in my article Matrices .

In this article I want to talk about an alternative way of describing the orientation of an object (rotation) in space using quaternions.

# Complex numbers

In order to fully understand quaternions, we first need to understand where they came from. The quaternion principle is based on the concept of a system of complex numbers.

Along with the well-known sets of numbers ( natural , integer , real and rational ), the system of complex numbers adds a new set of numbers, called imaginary numbers. Imaginary numbers were invented to solve certain equations that did not have solutions, for example:

$$

To solve this expression, we need to declare that $$, and this, as is known, is impossible, because the square of any number (positive or negative) is always positive.

Mathematicians could not accept the fact that the expression has no solution, so a new concept was invented - an imaginary number that can be used to solve such equations.

The imaginary number is as follows:

$$

Do not try to understand this assumption, because there are no logical reasons for its existence. We just need to accept that$$ - it's just some value, the square of which is equal $$.

The set of imaginary numbers can be denoted as$$.

The set of complex numbers (denoted by the symbol$$ Is the sum of the real and imaginary numbers in the following form:

$$

It can also be stated that all real numbers are complex with $$, and all imaginary numbers are complex with $$.

## Addition and subtraction of complex numbers

Complex numbers can be added and subtracted by adding and subtracting real and imaginary parts.

Addition:

$$

Subtraction:

$$

## Multiply a complex number by a scalar value

The complex number is multiplied by the scalar by multiplying each member of the complex number by the scalar:

$$

## The product of complex numbers

In addition, complex numbers can also be multiplied using ordinary algebraic rules.

$$

## Square of complex numbers

Also, a complex number can be squared by multiplying by itself:

$$

## Conjugate complex numbers

**The conjugate value of a**complex number is a complex number with a modified sign of the imaginary part, denoted as$$ or how $$.

$$

The multiplication of a complex number with its conjugate value gives an interesting result.

$$

## The absolute value of a complex number

We can use the

**adjoint number of a**complex number to calculate the absolute value (or

**rate**, or

**value**) of a complex number. The absolute value of a complex number is the square root of a complex number multiplied by its

**conjugate number**. It is denoted as$$:

$$

## Quotient of two complex numbers

To calculate the quotient of two complex numbers, we multiply the numerator and denominator by the conjugate number of the denominator.

$$

# Degrees $$

If we claim that $$then it must be possible to erect $$ and to other degrees.

$$

If we continue to record this series, we notice the pattern $$.

A similar pattern occurs with increasing negative degrees.

$$

You may have seen this pattern in mathematics, but in the form $$which is obtained by rotating the point 90 ° counterclockwise on a two-dimensional Cartesian plane; row$$ created by turning the point 90 ° degrees on a two-dimensional Cartesian plane.

*Cartesian plane*

# Complex plane

We can similarly apply complex numbers on a two-dimensional grid, called the

**complex plane**, by tying the

**real**part to the horizontal axis, and the

**imaginary part**to the vertical one.

*Complex plane*

As can be seen from the previous row, we can say that if we multiply the complex number by$$then we can rotate the complex number on the complex plane in increments of 90 °.

Let's check if this is true. We take an arbitrary point on the complex plane.$$:

$$

and multiply it by $$by receiving $$:

$$

Multiplying $$ on $$get $$:

$$

And multiplying $$ on $$get $$:

$$

And multiplying $$ on $$get $$:

$$

And we got exactly what we started ($$). If we put these complex numbers on the complex plane, we get the following result.

*Complex numbers on the complex plane*

Now we can rotate on the complex plane and clockwise, multiplying the complex number by$$.

## Rotors

We can also perform arbitrary turns on the complex plane by specifying a complex number in the following form:

$$

When multiplying any complex number by the rotor $$ we get the general formula:

$$

What can also be written in matrix form:

$$

What is the way to turn counterclockwise of an arbitrary point on the complex plane relative to the point of origin of coordinates.

# Quaternions

Having learned about the system of complex numbers and the complex plane, we can derive them into three-dimensional space, adding to the system of numbers along with $$two more imaginary numbers.

Quaternions have the following generalized form.

$$

Where according to Hamilton's famous expression:

$$

$$

You may notice that the relationship between $$, $$ and $$ very similar to the rules of vector multiplication of unitary Cartesian vectors:

$$

Hamilton also noticed that imaginary numbers $$, $$ and $$ can be used to represent three Cartesian unit vectors $$, $$ and $$ with the same properties of imaginary numbers, so $$.

*Graphic representation of properties $$, $$, $$*

The above image graphically presents the relationships between Cartesian unit vectors in the form $$, $$ and $$.

## Quaternions as an ordered pair

We can also represent quaternions in the form of an ordered pair:

$$

Where $$ can also be represented as its individual components:

$$

With this entry, we can more easily present the common features of quaternions and complex numbers.

## Addition and subtraction of quaternions

Quaternions can be added and subtracted in the same way as complex numbers:

$$

## Quaternion products

We can also express the product of two quaternions:

$$

What gives us another quaternion. If we replace the imaginary numbers in the previous expression$$, $$ and $$ ordered pairs (also known as quaternion units), we obtain

$$

And substituting back into the original expression with $$, we get:

$$

Expanding this expression into the sum of ordered pairs, we get:

$$

If you multiply by a quaternion unit and extract the common vector components, you can rewrite this equation as follows:

$$

This equation gives us the sum of two ordered pairs. The first ordered pair is a

**real quaternion**, and the second is a

**pure**quaternion. These two ordered pairs can be combined into one ordered pair:

$$

If we substitute, we get

$$

We get:

$$

This is the general equation of the product of quaternions.

## Real Quaternion

**A real**quaternion is a quaternion that includes a vector$$:

$$

And the product of two

**real**quaternions is another

**real**quaternion:

$$

Which is similar to the product of two complex numbers containing the zero imaginary term.

$$

## Quaternion multiplication by scalar

We can also multiply the quaternion by a scalar, while adhering to the following rule:

$$

We can verify this with the help of the above product of

**real**quaternions, by multiplying the quaternion by a scalar as the

**real**quaternion:

$$

## Pure Quaternions

In addition to

**real**quaternions, Hamilton also defined a

**pure**quaternion as a quaternion with a zero scalar member:

$$

Or if you write down the components:

$$

And again we can take the product of two

**pure**quaternions:

$$

in accordance with the above rule of quaternion products.

## Additive Quaternion Form

In addition, we can express quaternions as the sum of

**real**and

**pure**parts of a quaternion:

$$

## Unit quaternion

Taking an arbitrary vector $$, it is possible to express this vector both through its scalar value, and through its direction as follows:

$$

Combining this definition with the definition of a pure quaternion, we get:

$$

We can also describe a unit quaternion having a zero scalar and a unit vector:

$$

## Binary quaternion form

Now we can combine the definitions of the single quaternion and the additive form of the quaternion, obtaining the form of quaternions, similar to the record used in the description of complex numbers:

$$

This gives us a way to represent a quaternion in a form very similar to complex numbers:

$$

## Quaternion Conjugate Number

The adjoint number of a quaternion can be calculated by taking the vector part of the quaternion opposite in sign:

$$

The product of a quaternion and its conjugate number gives us the following:

$$

## Quaternion norm

Recall the definition of the norm of a complex number:

$$

Similarly, the rate (or value) of a quaternion is defined as:

$$

This allows us to express the quaternion rate as follows:

$$

## Normalization of Quaternion

Having a definition of the quaternion norm, we can use it to normalize the quaternion. Quaternion is normalized by dividing by$$:

$$

For example, let's normalize the quaternion:

$$

First we need to calculate

**the**quaternion

**rate**:

$$

Then we must divide the quaternion by the quaternion norm to calculate the normalized quaternion:

$$

## Reverse Quaternion

Reverse quaternion is denoted by $$. To calculate the inverse quaternion, we take the conjugate number of the quaternion and divide it by the square of the norm:

$$

To show this, we can use the definition of a reciprocal:

$$

And multiply both sides by the conjugate number of the quaternion, which will give us:

$$

By substitution we get:

$$

For single quaternions-norms whose norm is equal to 1, we can write:

$$

## Scalar product of quaternions

Similarly to the scalar product of vectors, we can calculate the scalar product of two quaternions by multiplying the corresponding scalar parts and summing up the results:

$$

We can also use the scalar product of quaternions to calculate the angular difference between quaternions:

$$

For unit quaternions-norms, we can simplify the equation:

$$

# Turns

Let me remind you that we have defined a special form of a complex number called a

**rotor**, which can be used to rotate a point on a two-dimensional plane as follows:

$$

Due to the similarity of complex numbers with quaternions, it should be possible to express a quaternion that can be used to rotate a point in three-dimensional space:

$$

Let's check if this theory is correct by calculating the quaternion product $$ and vectors $$. First, we can express$$as

**pure quaternion**in the following form:

$$

BUT $$ - is a single quaternion-norm in the form:

$$

Then

$$

We see that the result is a generic quaternion with scalar and vector parts.

Let's first consider the “special” case in which$$ perpendicular $$. In this case, the scalar member$$and the result becomes a

**pure**quaternion:

$$

In this case, to turn $$ regarding $$ we just substitute $$ and $$.

$$

For example, let's rotate the vector $$45 ° relative to the Z axis; then our quaternion$$ will be equal to:

$$

And let's take a vector $$which refers to a special case where $$ perpendicular $$:

$$

Now let's find the piece

$$

:$$

What gives us a

**pure**quaternion, rotated 45 ° relative to the axis$$. We can also make sure that the value of the final vector is preserved:

$$

Exactly what we expected!

We can show it graphically with the following image:

*The rotation of the quaternion (1)*

Now let's consider a quaternion that is not orthogonal to$$. If we take for the vector part of the quaternion an offset of 45 ° from$$, we get:

$$

And multiplying our vector $$ on $$, we get:

$$

After substitution $$, $$ and $$ we get:

$$

That is, it is no longer a

**pure**quaternion, it is not rotated by 45 °, and the vector norm is no longer equal to 2 (it decreased to$$).

This result can be shown graphically.

*Quaternion rotation (2)*

Strictly speaking, it is incorrect to represent quaternion $$in three-dimensional space, because in fact it is a four-dimensional vector! For the sake of simplification, I will show only the vector component of quaternions.

However, all is not lost. Hamilton found out (but did not publish this) that if we then multiply the result$$ the opposite of $$, the result is a

**pure**quaternion, and the norm of the vector component is preserved. Let's see if we can apply this in our example.

First, let's calculate$$:

$$

With $$ we get:

$$

Combining the previous value $$ and $$, we get:

$$

What is a

**pure**quaternion, and the result rate is equal to:

$$

which is equal to $$, that is, the norm of the vector is preserved.

The image below shows the result of the rotation.

*Rotation of the quaternion (3)*

We see that the result is a pure quaternion, and the norm of the original vector is preserved, but the vector is rotated 90 °, not 45 °, which is twice as much as necessary! Therefore, to correctly rotate the vector$$ at an angle $$ relatively arbitrary axis $$ we need to take a half angle and create the following quaternion:

$$

What is the general type of quaternion turning!

# Quaternion Interpolation

One of the most important reasons for using quaternions in computer graphics is that quaternions describe turns in space very well. Quaternions eliminate the problems that aggravate other ways of turning points in 3D space, such as folding a frame , in which the problem lies in the representation of turning in the corners of Euler.

Using quaternions, we can define several methods that represent the interpolation of rotation in 3D space. The first method I consider is called

**SLERP**. It is used to smoothly interpolate a point between two orientations. The second method is the development of

**SLERP**and is called

**SQUAD**. It is used for interpolation in a number of orientations that specify the path.

## SLERP

**SLERP**stands for

**S**pherical

**L**inear Int

**erp**olation (spherical linear interpolation).

**SLERP**provides the ability to smoothly interpolate a point between two orientations.

I will mark the first orientation as$$and the second as $$. The interpolated point is denoted by$$the interpolated point is denoted by $$. Interpolation parameter$$ will interpolate $$ from $$ at $$ before $$ at $$.

The standard linear interpolation formula is:

$$

Here are the basic steps for applying this equation:

- Calculate the difference between $$ and $$.
- Take the fractional part of this difference.
- Adjust the initial value to a fractional difference between two points.

We can use the same basic principle to interpolate between two orientations of quaternions.

### Quaternion Difference

The first step means that we need to calculate the difference between $$ and $$. In the context of quaternions, this is analogous to calculating the angular difference between two quaternions.

$$

### Raising a quaternion to a power

The next step is to take the fractional part of this difference. We can calculate the fractional part of a quaternion by raising it to a power whose value is in the interval$$.

The general formula for raising a quaternion to a power is as follows:

$$

Where the exponential function for quaternions looks like this:

$$

And the logarithm of quaternion has the form:

$$

With $$ we have the following:

$$

And at $$ we have

$$

### Fractional Quaternion Difference

To calculate the interpolated angular rotation, we change the initial orientation. $$ on the fractional part of the difference between $$ and $$.

$$

What is the general form of spherical linear interpolation for quaternions? However, this is not the kind of

**SLERP**equation that is commonly used in practice.

We can apply a similar formula to perform spherical interpolation of vectors into quaternions. The general view of spherical interpolation for vectors is given by:

$$

Graphically, this can be shown in the following image.

*Quaternion Interpolation*

This formula can be applied to quaternions without any changes:

$$

And we can get a corner $$by calculating the scalar product $$ and $$.

$$

### Factors to Consider

This implementation has two problems to consider when using.

First, if the scalar product of quaternions turns out to be a negative value, then the interpolation will go along the “long way” on the four-dimensional sphere, and this is not always desirable. To solve this problem, we can check the result of the scalar product, and if it is negative, then we can take the opposite of one of the orientations. Inverting the scalar and vector parts of the quaternion does not change the orientation it represents, but by doing this, we guarantee that the turn will occur along the “shortest” path.

Another problem arises if the angular difference between$$ and $$ very small $$ becomes 0. If this happens, then when dividing by $$we can get an uncertain result. In this case, you can return to using linear interpolation between$$ and $$.

## SQUAD

Just as

**SLERP**can be used for interpolation between two quaternions,

**SQUAD**(

**S**pherical and

**Quad**rangle - spherical and quadrilateral) can be used for smooth interpolation along the turn path.

If we have a number of quaternions:

$$

And we defined the "auxiliary" quaternion ($$), which we can consider as an intermediate reference point:

$$

Orientation along part of a curve is defined as:

$$

at time

**t**it gives us:

$$

# Conclusion

Despite the difficulty to understand, when working with turns, quaternions provide several obvious advantages compared to Euler matrices and angles.

- Interpolation of quaternions using SLERP and SQUAD provides a way to smoothly interpolate between orientations in space.
- The concatenation of turns using quaternions is faster than the union of turns expressed in a matrix form.
- For single quaternions-norms, the reciprocal of the rotation is taken by subtracting the vector part of the quaternion. The calculation of the inverse of the rotation matrix is much slower if the matrix is not orthonormal (if it is orthonormal, then this is just the matrix transposition).
- The conversion of quaternions to matrices is slightly faster than for Euler angles.
- To describe the rotation, quaternions require only 4 numbers (3, if they are normalized. The real part can be calculated during program execution), while matrices need at least 9 values.

However, along with all the advantages of using quaternions, there are also a few drawbacks.

- Quaternions may become invalid due to a round-off error of floating-point numbers; However, this "crept error" can be eliminated by renormalization of quaternion.
- Probably the most significant obstacle to the use of quaternions is the high complexity of their understanding. I hope you solve this problem by reading my article.

There are many mathematical libraries that implement quaternions, and only some of them implement quaternions correctly. In my own experience, a good mathematical library with a qualitative implementation of quaternions is the GLM (OpenGL Math Library). If you want to use quaternions in your own applications, then I recommend this library.

# Download demo

I created a small demo demonstrating the use of quaternion to rotate an object in space. The demo was created in Unity 3.5.2, you can download free download this engine and view the source code of the demo. The zip file also contains the Windows binary executable, but in Unity you can build the application for Mac.

Understanding Quaternions.zip

# Reference materials

Vince, J (2011). Quaternions for Computer Graphics. 1st. ed. London: Springer. | Dunn, F. and Parberry, I. (2002). 3D Math Primer for Graphics and Game Development. 1st. ed. Plano, Texas: Wordware Publishing, Inc. |