How quaternions (4d numbers) visualize 3d space

Channel: Wumbo Published: 2024-08-16 4,151 words Source: manual_caption
Advanced Mathematics & Geometric Physics

Transcript

What you're looking at right now is  a quaternion number that defines the   orientation of 3D space for this little  sailboat. As the quaternion changes, the   points in the 3D space are transformed according  to its value rotating the boat about the origin.

The quaternion number system is one of those  otherworldly things from math that is truly   interesting and useful. Using quaternion  we can rotate the ship around an axis   by an angle or change the orientation  to a known position like a bird's eye   view of the ship.

A quaternion lives in  four dimensions and its properties as   well as operations are what allow for the  smooth and precise movement of 3D space. In this video, I show how quaternions are  used to describe orientation as well as   rotation and the elegant method they 

provide for computing the transformed   position of points. I also visualize  what's happening in the fourth dimension   to answer some questions that  can arise when using quaternions. A quaternion is written as some real number 

plus some real number time "i" plus some real   number times "j" plus some real number times  "k". In this expression the variables w, x,   y, and z are real numbers and i, j, and k are  the quaternion units which have the properties   i^2 is equal to j^2 is equal to k^2 is  equal to i*j*k which is all equal to -1.

The units i, j, and, k encode rotation  so that the quaternion's operations have   geometric meaning. When we multiply  two quaternion numbers together,   the properties of these units are how we know  the value of the quaternion produced as a result.   Later, we'll investigate the geometric 

meaning these units encode but, for now,   I want to show you conceptually how a quaternion  number can define the orientation of 3D space. Let's say we have a quaternion which  starts with a value of one so it is   written as "1 + 0i + 0j + 0k". The 3D space whose 

orientation the quaternion describes lives in   a coordinate system with an x-axis, y-axis, and  z-axis. Since we starting with the value of one,   the 3D space will initially have the same  orientation as the coordinate system. One   unit in the positive x direction of the 

space is represented by this green arrow,   one unit in the positive y direction of the  space is represented by this red arrow and one   unit in the positive z direction of the space  is represented by this blue arrow. I'll also   draw a cube centered at the origin with a side  length of two units to help visualize the space.

To change the orientation we multiply the  quaternion by another quaternion which   represents the rotation around an axis by  an angle. For example, let's say we have a   rotation quaternion whose axis of rotation is the  vector that passes through what we see as the top   left corner of the cube and angle of 60° in this 

direction. We write this quaternion as the cosine   of theta divided by two plus the sine of theta  divided by two multiplied by the axis of rotation. To perform the rotation we multiply the  orientation by this quaternion from the   left which will write as "q" goes to "r" times 

"q". I'll also draw a copy of the coordinate   system to show how the orientation is rotated.  Think of the arrow in this expression as meaning   the orientation on the left is transformed  to be the orientation on the right and as the   rotation as acting on the orientation. For this 

rotation, the two points of the cube that lie on   the axis of rotation will stay in the same place  while the rest of the cube rotates by the angle   around the axis which gives the orientation  of space that looks something like this. To calculate the position of a point  that's oriented according to "q" we   perform quaternion vector multiplication, which 

you can think of as a function that takes in a   point as input and returns its transformed  position by multiplying the point by q on   the left side and also multiplying by q's  inverse on the right side. Given a point's   original XYZ coordinates which we write as the  quaternion "xi + yj+ zk", this sandwich-like   product gives us the transformed position 

of the point as described by the quaternion. For example, given this point on the cube as input   the function returns the transform  position of the point. In general,   this is how we know the position of the points  in space that are oriented by the quaternion.

Let me show you what this looks like as an  animation. When we apply the rotation to   the orientation, the 3D space is rotated  around the axis by the angle. For each   value between the quaternion's 

starting and ending orientation,   the positions of the points in space are  calculated using the quaternion vector product. This setup works for any orientation. For example,  let's say we have another rotation that describes   a quarter of a rotation around the x-axis.

We can  apply this new rotation to the current orientation   by multiplying by the rotation from the left. A  quarter of a rotation is π/2 radians and half of   that is π/4 so the second rotation is the cosine  of π fourths plus the sine of π fourths times the   axis of rotation which, since we are rotating 

around the x-axis, will be "1i + 0j + 0k". Now that we are explicitly writing the angle,  how do we know which direction this quaternion   will rotate the orientation around the axis? The  answer is one of the distinguishing features of   quaternion numbers: the order in which you  multiply two quaternions matters.

When we   multiply in the order we have here you use what  is called the "right-hand rule" to figure out   the direction the orientation is rotated. If you  stick your right thumb out in the direction of the   axis of rotation, the direction your fingers  curl will be in the positive direction of   rotation.

In this case, the effect of applying  this rotation by multiplying in this order   rotates the space a quarter of a rotation  around the x-axis in this direction. If you were to apply the rotation by  multiplying the orientation from the   other side and also remember to change the 

order of the quaternion vector product,   the effect would be to rotate in the other  direction around the axis of rotation. For now,   we'll keep multiplying in the order we have here. Using the right-hand rule, we can write the  value for the first rotation which rotates   space 60° in this direction around the axis 

that passes through the top left corner of the   cube. If you stick your right thumb out in the  direction of the vector and curl your fingers,   the positive direction is this way. Because the  angle I've drawn rotates the other direction its   value is negative.

A 60° angle in radians is  π over 3 so the value is cosine of negative   π/6 plus sine of negative π/6 times the  axis of rotation which is "-1i - 1j + 1k". We also should normalize the axis of rotation  to be a unit vector by dividing by its magnitude   which is the square root of 3.

That way, every  time we apply rotation the orientation will   maintain a magnitude of one, otherwise we  could get some unexpected behavior. Since   the magnitude of the other rotation quaternion  is already one we don't need to do anything.

Now that we have the values for two rotations,  which act on the orientation according to the   right-hand rule, consider what happens if  we change the order in which we apply the   two rotations. Currently we rotate around  the diagonal axis and then rotate around   the x-axis.

When we apply the rotations in the  reverse order, by rotating around the x-axis   and then rotating around the diagonal axis, we  get a different orientation of the cube. Since   we applied the rotation using multiplication,  this is a good example that provides geometric   intuition for why changing the order of 

multiplication can produce a different   quaternion as a result. As a mathematician would  say quaternion multiplication is non-commutative. Zooming out for a bit this setup demonstrates  why quaternions are so useful.

It shows how to   describe the orientation of 3D space using  a quaternion and how to represent and apply   rotations using multiplication. But, if you're  like me, you may have some questions like:   "why do we divide the angle by two?" and "how  does the quaternion vector product work?" To answer these questions we need to unpack 

what's happening in the fourth dimension. But,   before we cross that threshold,  I want us to take a closer look   at quaternion multiplication and the  geometric meaning of the units i, j,   and k because the other thing we illustrated  with this setup is that the order in which you   multiply matters.

This is important to keep  in mind for the property "ijk = -1" because   this property is saying quite a bit more when the  order matters than when the order doesn't matter. One way to visualize this is to expand  the properties of the units into a table.   The way to read this is that a unit on the left 

multiplies a unit on the right. In the first row,   we have one times one equals one, one  times i equals i, one times j equals j,   and one times k equals k. In the first  column, we have one times one equals one, i   times one equals i, j times one equals j, and 

k times one equals k. In the diagonal, we have   i times i equals negative one, j times j equals  negative one, and k times k equals negative one.   Where things get interesting is the permutations  of i, j, and k, which are a more verbose way to   express what the property i j k equals negative  one is saying.

You can find each of these products   by carefully multiplying both sides of the  property and simplifying, but I’ll just give   them here as i times j equals k, where j times  i equals negative k, and j times k equals i,   where k times j equals negative i, and k times  i equals j, where i times k equals negative j. This is where you can see the non-commutative 

property of multiplication in action and why   you can get different results depending on the  order you multiply quaternions together. When   you multiply two quaternions algebraically by  distributing each of the terms across you need   to make sure to preserve the order of the  units in the expression to get the correct   result.

I’ll spare you the details, but I like  to organize the distributed terms in a table and,   after you sum all of the like-terms  together, we'll get this expression. When we apply a rotation to the orientation using  multiplication, this expression is exactly how you   can tell a computer to calculate the result of 

one of these rotations. For our purposes and,   so we can calculate the products by hand,  let’s visualize what it means to rotate the   orientation by something like i, j, or k.  For example, given how we are writing the   value of a rotation quaternion, what angle  and axis of rotation result in the unit k? Well, if we have an angle of 180 degrees or pi 

radians and an axis of rotation around the z-axis,   the value of this rotation quaternion  will be cosine of pi over 2 plus sine of   pi over 2 multiplied by (0 i plus 0 j plus 1  k). Because we divided the angle by two, the   cosine of the angle is zero, and the sine of the  angle is one, so the value of this rotation is k.

When we visualize the orientation of 3D space  like this, multiplying the orientation by k   has the effect of rotating 180 degrees around the  z-axis. Similarly, multiplying the orientation by   j has the effect of rotating 180 degrees around  the y-axis, and multiplying the orientation   by i has the effect of rotating 180 degrees 

around the x-axis. [pause] However, when we   applied these three rotations, something strange  just happened. The value of the orientation we   ended up at is negative one, which looks exactly  like the orientation described by positive one.

To see what’s going on, let’s say we start from  the standard orientation of one and apply the   rotation represented by k multiple times. When we  rotate by k the first time, we get the orientation   of k since k multiplied by one equals k. When we 

rotate by k again, we get the orientation negative   one, since k multiplied by k equals negative  one, which looks exactly like the orientation we   started at. Similarly, if we rotate by k again,  we get the orientation negative k, which looks   exactly like the orientation k. And, finally, 

when we apply k once more, we get back to one. From how we are writing the rotation quaternion,  it would be fair to assume that rotating by k   corresponds to a 90-degree rotation around the  z-axis since pi over two is the same thing as   90 degrees. However, a byproduct of visualizing 

3D space using the quaternion vector product is   that rotations are doubled because we compute the  transformed position of a point by multiplying by   q twice—once from the left and again from  the right by its inverse. This is why the   orientations positive one and negative one look  the same, and why k and negative k look the same.

When you visualize 3D space like this, every  possible orientation has two quaternions that   describe it. In general, if you have an  orientation and rotate 360 degrees around   any axis, you would get the corresponding  quaternion whose coordinates are negated,   which describes the same orientation. 

In practice, this is useful because you   can choose the shortest path when  rotating between two orientations. The reason why there are two possible  quaternions for every orientation of space,   and that multiplying by a unit like k  visually corresponds to a 180-degree   rotation and not a 90-degree rotation, lies 

in the mechanics of the quaternion vector   product. This function really operates in  four dimensions and, to see how it works,   let’s peek behind the curtain  to see what’s happening in 4D. If we change the quaternion vector product 

so that we are only multiplying a point by   the quaternion on the left, we can partially  visualize what rotations look like in four   dimensions. I say “partially” because there is  another dimension that is not currently shown. Let’s visualize applying the rotation represented 

by k to the 4D space. Focus on the vectors drawn   as the green and red arrows which correspond  to the points i and j. When we rotate the   space by applying k, the position where i  lands is given by k times i, which is j,   and the position where j lands is given by 

k times j, which is negative i. Now that we   have modified the function to no longer multiply  by the inverse, multiplying the orientation by   k from the left has the effect of rotating  the space around the z-axis by 90 degrees. If we were just talking about points in the xy 

plane, then this appears to have the effect we   want. However, watch what happens to the rest of  the points when we rotate by k. The cube appears   to get squished in the direction of the axis of  rotation.

If we follow where the point k goes,   we can see that its position is given by k times  k, which equals negative one. Currently, our setup   shows this point at the origin, but really it  lies in the fourth dimension at a distance of   negative one from the origin on another axis. The 

same goes for other points in the space – some   of their coordinates remain in three dimensions  while others disappear into the fourth dimension. Now that we’ve changed the function, the  uncomfortable reality is that 4D space is   rotated in two directions at once when we apply  k.

There’s the rotation around the z-axis by   90 degrees according to the right-hand rule and,  at the same time, there is another rotation we   see as the cube getting squished. The question at  hand is: how can we preserve the rotation around   the z-axis that we want while reversing this other  rotation so that the cube doesn’t get squished? In some sense, I’ve already spoiled the answer 

for you – right multiplying by the inverse   of the quaternion does just the trick.  But, suppose you didn’t know that works,   it’s not clear why you would consider multiplying  by the inverse in the first place. Typically,   when you multiply an expression by a number  and its inverse, it’s like multiplying by one,   so the expression remains unchanged.

Something  rather clever is going on in the construction   of the full quaternion vector product to  preserve the rotation around the axis we want. Let me show you how we can visualize the rotation  that we want to reverse by following what happens   to the input of one to the function. 

Due to the limitations of our setup,   this point appears to be at the origin, but it  actually sits at a distance of one on the fourth   axis. Because we are rotating around the z-axis,  we can visualize the fourth dimensions by drawing   another perspective of the space without  the x and y axes.

Instead, we’ll draw the   axis that represents the fourth dimension where we  can see the input sits a distance of one from the   origin. Technically, this axis is perpendicular to  the other three axes, which is why it’s so hard to   draw them all at once and explains why the number  appears to be at the origin of the xyz setup.

When we apply the rotation k, the points  one and k rotate counterclockwise around the   origin of this other view of the coordinate  system. Let me continue applying k so you   can become familiar with the two rotations  that happen at once.

Each time we apply k,   space is rotated 90 degrees around the  z-axis according to the right-hand rule   and 90 degrees counterclockwise relative  to the z-axis and the fourth dimension. Watch what happens if we change our function to  visualize space by multiplying the orientation   from the right.

Now, when we apply the rotation  represented by k, the space rotates around the   z-axis in the opposite direction but continues  to rotate in the same direction as before for   the other rotation. At first glance, this doesn’t  appear to help our cause since the rotation around   the z-axis is reversed and the other rotation 

continues in the same direction. However,   when you think about it, when we multiply  from the right, both rotations now travel   in the opposite direction from how we want,  and this is where we can use the inverse. Typically, when you think of the inverse of 

something, there is a sense that the product   of the thing and its inverse results in one. If  this is the property we want the inverse of a   quaternion to have, we can define the inverse  using something called the conjugate. When you   multiply a quaternion and its conjugate 

together, all of the i, j, k units turn   into real numbers and we get w squared plus  x squared plus y squared plus z squared. This   expression is a real number that is equal  to the norm squared of a quaternion,   so we can define the inverse as the  conjugate divided by the norm squared.

We can visualize the inverse as two  transformations. When we apply a rotation   like k, space is rotated like this, because  we are multiplying by the orientation from   the right. Then, if we multiply the 

points by the orientation’s inverse,   this reverses the transformation back to the  original orientation. If we visualize space   by just multiplying by the quaternion’s inverse  from the right, space rotates around the z-axis   in the same direction as if we are  left multiplying by the quaternion and,   crucially, in the clockwise direction 

relative to the fourth dimension. This is just what we need to preserve the  rotation we want while reversing the other.   When we multiply by the quaternion from the  left, space rotates around the axis of rotation   and around this other axis, which we see as the  cube getting squished.

Then, multiplying by the   quaternion’s inverse from the right rotates space  around the axis of rotation in the same direction   again and reverses the other rotation. If  we perform both multiplications at once,   space is only rotated around the first axis, but  since we are multiplying by the quaternion twice,   once by the quaternion and again by 

its inverse, the angle of rotation   is doubled. This is why we divide the angle by  two when expressing a rotation as a quaternion. So far, we’ve been visualizing applying  the rotation k to the orientation,   but this works for any angle and axis.

For  example, to rotate 120 degrees around the x-axis,   we write the rotation as cosine of pi over  three, since half of 120 degrees is pi over   three in radians, plus sine of pi over three  times the axis of rotation, which is one times i   plus zero times j plus zero times k. Because 

we are rotating around one of the xyz axes,   we can visualize the other rotation by graphing  the fourth axis against the axis of rotation. When we apply the rotation and multiply by  the quaternion from the left, space rotates   60 degrees around the axis of rotation and 60  degrees counterclockwise in the other view of   space.

Then, when we multiply by the quaternion’s  inverse, space rotates 60 degrees around the axis   of rotation again and reverses the other rotation.  When we perform both multiplications at once,   space rotates 120 degrees around the axis of  rotation, and the cube stays the same shape. Now that we are rotating around the x-axis, 

this other view of space is an illustration of   something you may recognize – the complex number  system. This coordinate system visualizes how a   property like i squared equals negative one  encodes rotation. The complex number system   is worth a lesson in and of itself, but 

I wanted to call it out by name because   quaternions extend complex numbers by adding  the units j and k. It’s a very interesting   question why you can’t extend the complex  number system by adding just one unit like   j to represent rotations in three dimensions, but  alas, that too is out of the scope of this video.

This 2D slice of the full space helps  visualize how the second rotation   is reversed when we rotate around  an axis like the x-axis. However,   when we rotate around an axis like the one that  points towards the top-right corner of the cube,   this plane no longer captures the full motion 

of the other rotation. That said, you can   still see the same general principle for how the  quaternion vector product works in the xyz setup.   When we multiply the points by the quaternion  from the left, space rotates around the axis   of rotation by 60 degrees and also 

rotates in this other direction. Then,   multiplying by the quaternion’s inverse from the  right, space rotates around the axis of rotation   by 60 degrees again, and the other rotation is  reversed. When we perform both multiplications   at once, the cube stays the same shape and 

space rotates 120 degrees around the axis. This is how the quaternion vector product  visualizes the orientation of 3D space   and why we write the value of the rotation  quaternion with the angle divided by two. It’s satisfying to understand how and why the 

quaternion vector product visualizes 3d space,   but it can be equally fulfilling to simply  use quaternions to rotate the orientation   of something like the sailboat I showed at  the beginning. Any time you want to describe   the orientation of an object, such as earth,  consider that quaternions provide an elegant and   computationally efficient method for computing 

the transformed position of points in space. In the next video, I show how to  implement quaternions on a computer   using the concepts talked about here. We  also define the method that quaternions   provide to smoothly transition 

from one orientation to another. If math resources like this add value  to your life, it would mean a lot if   you consider joining the patreon. The  link is in the description.

I’ve also   added links to a series of excellent videos and  interactive articles that show another way to   visualize how the quaternion vector product  works made by 3Blue1Brown and Ben Eater.