{"id":7255,"date":"2021-09-06T07:28:53","date_gmt":"2021-09-06T11:28:53","guid":{"rendered":"https:\/\/blogs.mathworks.com\/cleve\/?p=7255"},"modified":"2021-09-11T09:16:53","modified_gmt":"2021-09-11T13:16:53","slug":"quaternions","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/cleve\/2021\/09\/06\/quaternions\/","title":{"rendered":"Quaternions"},"content":{"rendered":"<div class=\"content\"><!--introduction--><p>Quaternions are generalizations of the complex numbers that have found applications in computer graphics and many other fields.<\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#8aaa90c7-8166-4325-b670-d71ff40c423a\">Ed Angel<\/a><\/li><li><a href=\"#744b6e76-6c2d-4747-bae0-d074aecab3e5\">Two dimensions<\/a><\/li><li><a href=\"#74ca6370-28e2-417f-bc3c-5013826b6572\">Three dimensions<\/a><\/li><li><a href=\"#419ccb86-4cc1-49f1-a4cd-8ef22b090470\">William Rowan Hamilton<\/a><\/li><li><a href=\"#1d92ae82-9322-4521-bbab-11a54b86c295\">Dot and cross products<\/a><\/li><li><a href=\"#a09fb4e2-8d61-42a7-8f38-2c013e056c7c\">Quaternions<\/a><\/li><li><a href=\"#6c249cb2-5c3e-4880-beca-63430e62fb44\">Quaternion addition<\/a><\/li><li><a href=\"#3f7e0caf-9c8c-448a-9316-e94374533e1a\">Quaternion multiplication<\/a><\/li><li><a href=\"#e91db608-b9c9-4e5f-a7d2-89aded68fd12\">A Capella Science<\/a><\/li><li><a href=\"#7cac949e-0e12-4d48-aa85-122e0ae9b36c\"><tt>qtn<\/tt><\/a><\/li><li><a href=\"#eb153d95-214b-4b48-92c5-9c7ec5c6a4b2\">basic<\/a><\/li><li><a href=\"#801387d2-da2c-4aa8-8535-429d814c8769\">random<\/a><\/li><li><a href=\"#b8a4c856-760b-455c-8630-70d7850c53fc\">rotations<\/a><\/li><li><a href=\"#d66b61c3-18e5-49a5-8781-47a35584a369\">Code<\/a><\/li><\/ul><\/div><h4>Ed Angel<a name=\"8aaa90c7-8166-4325-b670-d71ff40c423a\"><\/a><\/h4><p>Ed Angel is my longtime friend from Caltech and New Mexico days. Ed is the author of <a href=\"https:\/\/www.amazon.com\/Edward-Angel\/e\/B001IOF5SE\">a popular series of textbooks<\/a> about computer graphics that has now reached its 8th edition. The books feature the evolution of the OpenGL programming language from Silicon Graphics to WebGL and JavaScript. Ed is working on a MATLAB version and I am kibitzing.<\/p><h4>Two dimensions<a name=\"744b6e76-6c2d-4747-bae0-d074aecab3e5\"><\/a><\/h4><p>Life in two dimensions is easy. A point in the plane is readily described by its cartesian coordinates, $(x,y)$, or by its polar coordinates, $(r,\\theta)$, or by a single complex number $z$. Rotation of the point about the origin is easily described by multiplying the cartesian coordinates by a 2-by-2 orthogonal matrix, or by incrementing the polar angle, or by multiplying $z$ by another complex number.<\/p><h4>Three dimensions<a name=\"74ca6370-28e2-417f-bc3c-5013826b6572\"><\/a><\/h4><p>The situation in three dimensions is more complicated. There are cartesian coordinates $(x,y,z)$, or spherical coordinates $(r,\\theta,\\psi)$.  But what is the three-dimensional analog of the single complex number? Rotations are even more complicated. They require the specification of an axis of rotation as well as the angle.  And there are lots of 3-by-3 orthogonal matrices that generate rotations to choose from.<\/p><h4>William Rowan Hamilton<a name=\"419ccb86-4cc1-49f1-a4cd-8ef22b090470\"><\/a><\/h4><p>William Rowan Hamilton (1805-1865) was an Irish mathematician, a professor at Trinity College Dublin, and the Royal Astronomer of Ireland. His name is associated with important concepts throughout mathematics and physics. Like many other mathematicians of his era, he tried unsuccessfully to generalize two-dimensional complex numbers by adding a second complex unit.  In 1846 he realized that he actually needed to add three complex units, $i$, $j$ and $k$.<\/p><h4>Dot and cross products<a name=\"1d92ae82-9322-4521-bbab-11a54b86c295\"><\/a><\/h4><p>The key ingredients of Hamilton's quaternion arithmetic are dot and cross products.  The dot product, also known as the inner product, is a familiar feature of linear algebra in any number of dimensions. The dot product of two vectors is a scalar.<\/p><p>$$ a \\cdot b = a_1 b_1 \\ + \\ a_2 b_2 \\ + \\ a_3 b_3 $$<\/p><p>The cross product is defined only in three dimensions. The cross product of two vectors is a third vector, oriented to be perpendicular to the plane spanned by the first two. If $i$, $j$ and $k$ are unit vectors for the axes in three dimensions, then the cross product of vectors $a$ and $b$ is<\/p><p>$$ a \\times b = (a_2 b_3 - a_3 b_2) \\ i \\ + \\ (a_3 b_1 - a_1 b_3) \\ j\r\n \\ + \\ (a_1 b_2 - a_2 b_1) \\ k $$<\/p><p>Here is a snapshot of the cross product.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/cleve\/files\/cross_pic.png\" alt=\"\"> <\/p><p>There are two possible orientations for the vector perpendicular to the plane spanned by $a$ and $b$.  The conventional choice is made by the right hand rule illustrated on this Swiss bank note and by this emoji on the sign for the Facebook corporate offices.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/cleve\/files\/CHF_200.jpg\" alt=\"\"> <\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/cleve\/files\/thumbs_up.jpg\" alt=\"\"> <\/p><h4>Quaternions<a name=\"a09fb4e2-8d61-42a7-8f38-2c013e056c7c\"><\/a><\/h4><p>A quaternion $q$ has two parts, a scalar $s$ and a vector $v$. The scalar is one real number; think of $s$ as the coefficient of the scalar unit, $1$.  The vector consists of three real numbers; they are the coefficients of three imaginary units, $i$, $j$ and $k$.  So<\/p><p>$$ q = [s,v] = s 1 \\ + \\ v_1 i \\ + v_2 j \\ + \\ v_3 k $$<\/p><p>Quaternion multiplication is defined so that each of the imaginary units is a square root of $-1$ .<\/p><p>$$ i^2 = -1, \\ \\ j^2 = -1, \\ \\ k^2 = -1 $$<\/p><p>Furthermore, the three units are related to each other by a cyclic property of their products.<\/p><p>$$ ij = k, \\ \\ jk = i, \\ \\ ki = j $$<\/p><p>These properties imply<\/p><p>$$ ji = -k, \\ \\ kj = -i, \\ \\ ik = -j $$<\/p><p>Let's call these properties of $i$, $j$ and $k$ under multiplication \"Hamilton's Rules\".<\/p><h4>Quaternion addition<a name=\"6c249cb2-5c3e-4880-beca-63430e62fb44\"><\/a><\/h4><p>Suppose we have two quaternions.<\/p><p>$$ p = [r,u] = r 1 \\ + \\ u_1 i \\ + u_2 j \\ + \\ u_3 k $$<\/p><p>$$ q = [s,v] = s 1 \\ + \\ v_1 i \\ + v_2 j \\ + \\ v_3 k $$<\/p><p>Addition is the obvious.<\/p><p>$$ p + q = [r+s, u+v] $$<\/p><h4>Quaternion multiplication<a name=\"3f7e0caf-9c8c-448a-9316-e94374533e1a\"><\/a><\/h4><p>Quaternion multiplication is ingenious and complicated.<\/p><p>$$ p q = (r + u_1 i + u_2 j + u_3 k)(s + v_1 i + v_2 j + v_3 k) $$<\/p><p>There are four terms in each parentheses, so there are 16 terms in the product.  Expand carefully because $i$, $j$ and $k$ do not commute. Apply Hamilton's Rules, $i^2 = -1$, $i j = k$, $j i = -k$, etc. The 16 terms can be arranged like this.<\/p><p>$$ p q = [\\ r s - (u_1 v_1 + u_2 v_2 + u_3 v_3) \\ , $$<\/p><p>$$ \\ \\ \\ \\ \\ \\ \\ \\ r (v_1 i + v_2 j + v_3 k) + s (u_1 i + u_2 j + u_3 k) \\ +\r\n (u_2 v_3 - u_3 v_2) i + (u_3 v_1 - u_1 v_3) j + (u_1 v_2 - u_2 v_1) k \\ ] $$<\/p><p>We recognize two familiar quantities, the dot and cross products.<\/p><p>$$ u \\cdot v = u_1 v_1 + u_2 v_2 + u_3 v_3 $$<\/p><p>$$ u \\times v = (u_2 v_3 - u_3 v_2) i + (u_3 v_1 - u_1 v_3) j + (u_1 v_2 - u_2 v_1) k $$<\/p><p>So the result in<\/p><p>$$ p q = [r s - u \\cdot v, r v + s u + u \\times v ] $$<\/p><p>This is Hamilton's definition of the quaternion product.<\/p><p>The units $i$, $j$ and $k$ have scalar parts equal to 0 and vector parts equal to [1,0,0], [0,1,0] and [0,0,1]. Plugging these into the product definition produces, for example,<\/p><p>$$ i^2 = [0 -i \\cdot i, 0 + 0 + i \\times i] = -1 $$,<\/p><p>$$ i j = [0 - 0, 0 + 0 + i \\times j] = k $$<\/p><p>and<\/p><p>$$ j i = j \\times i = - i \\times j = - k $$<\/p><h4>A Capella Science<a name=\"e91db608-b9c9-4e5f-a7d2-89aded68fd12\"><\/a><\/h4><p>A Capella Science is a Patreon YouTube channel. <a href=\"https:\/\/www.patreon.com\/acapellascience\">https:\/\/www.patreon.com\/acapellascience<\/a>.<\/p><p>In 2016, A Capella Science produced a video featuring two Hamiltons, the immensely popular Broadway musical Hamilton and the Irish mathematician Hamilton. <a href=\"https:\/\/www.youtube.com\/watch?v=SZXHoWwBcDc\">https:\/\/www.youtube.com\/watch?v=SZXHoWwBcDc<\/a>. Here is a screen capture of one frame from that video.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"http:\/\/blogs.mathworks.com\/cleve\/files\/ACapella.png\" alt=\"\"> <\/p><h4><tt>qtn<\/tt><a name=\"7cac949e-0e12-4d48-aa85-122e0ae9b36c\"><\/a><\/h4><p>The 2021a and 2021b releases of MATLAB include a <tt>quaternion<\/tt> object that is shared among several of the navigation toolboxes.<\/p><p>Ed and I have our own more basic object, <tt>qtn<\/tt>.<\/p><pre class=\"codeinput\">   help <span class=\"string\">qtn<\/span>\r\n<\/pre><pre class=\"codeoutput\">  qtn  Basic constructor for quaternions. \r\n \r\n    qtn(a,b,c,d)\r\n    qtn(a,[v1 v2 v3])\r\n    qtn(a) \r\n \r\n    Methods\r\n \r\n    plus        p + q\r\n    minus       p - q\r\n    uminus      -q\r\n    mtimes      p*q\r\n    mldivide    p\\q\r\n    mrdivide    p\/q \r\n    mpower      q^n\r\n    inv(q)\r\n    norm(q)\r\n    disp(q)\r\n \r\n    Functions\r\n \r\n    rot\r\n    Euler\r\n    randq\r\n    qtn_examples\r\n \r\n    Notes:\r\n       All angles are in degrees.\r\n       This object is not vectorized.\r\n\r\n    Documentation for qtn\r\n       doc qtn\r\n\r\n    Published output in the Help browser\r\n       showdemo qtn\r\n\r\n\r\n<\/pre><p>The most important <tt>qtn<\/tt> method is the quaternion product. I think this code is elegant.<\/p><pre class=\"codeinput\">   dbtype <span class=\"string\">89:96<\/span> <span class=\"string\">qtn<\/span>\r\n<\/pre><pre class=\"codeoutput\">\r\n89            function r = mtimes(p,q)\r\n90                % p*q\r\n91                p = qtn(p);\r\n92                q = qtn(q);\r\n93                r.s = p.s*q.s - dot(p.v,q.v);\r\n94                r.v = p.s*q.v + q.s*p.v + cross(p.v,q.v);\r\n95                r = qtn(r);\r\n96            end\r\n<\/pre><p>The entire <tt>qtn<\/tt> class definition is only about 150 lines long.<\/p><h4>basic<a name=\"eb153d95-214b-4b48-92c5-9c7ec5c6a4b2\"><\/a><\/h4><p>Here are a few examples and tests of our basic quaternion object.<\/p><p>Two of the units.<\/p><pre class=\"codeinput\">   i = qtn(0,[1 0 0])\r\n   j = qtn(0,[0 1 0])\r\n<\/pre><pre class=\"codeoutput\">\r\ni = \r\n\r\n   0 + 1i + 0j + 0k\r\n\r\nj = \r\n\r\n   0 + 0i + 1j + 0k\r\n<\/pre><p>Their product is the third unit.<\/p><pre class=\"codeinput\">   k = i*j\r\n<\/pre><pre class=\"codeoutput\">\r\nk = \r\n\r\n   0 + 0i + 0j + 1k\r\n<\/pre><p>All three of these are -1.<\/p><pre class=\"codeinput\">   ijk = i*j*k\r\n   isq = i^2\r\n   ii = i*i\r\n<\/pre><pre class=\"codeoutput\">\r\nijk = \r\n\r\n   -1 + 0i + 0j + 0k\r\n\r\nisq = \r\n\r\n   -1 + 0i + 0j + 0k\r\n\r\nii = \r\n\r\n   -1 + 0i + 0j + 0k\r\n<\/pre><h4>random<a name=\"801387d2-da2c-4aa8-8535-429d814c8769\"><\/a><\/h4><p>Two random <tt>qtn<\/tt>'s with integer coefficients<\/p><pre class=\"codeinput\">   p = randq\r\n   q = randq\r\n<\/pre><pre class=\"codeoutput\">\r\np = \r\n\r\n   2 + 8i - 1j + 4k\r\n\r\nq = \r\n\r\n   0 + 4i - 6j + 7k\r\n<\/pre><p>Addition.<\/p><pre class=\"codeinput\">   p_plus_q = p + q\r\n<\/pre><pre class=\"codeoutput\">\r\np_plus_q = \r\n\r\n   2 + 12i - 7j + 11k\r\n<\/pre><p>Multiplication.<\/p><pre class=\"codeinput\">   pq = p*q\r\n<\/pre><pre class=\"codeoutput\">\r\npq = \r\n\r\n   -66 + 25i - 52j - 30k\r\n<\/pre><p>Right division.<\/p><pre class=\"codeinput\">   r = pq\/p\r\n<\/pre><pre class=\"codeoutput\">\r\nr = \r\n\r\n   0 + 9.6i + 2j - 2.2k\r\n<\/pre><p>Left division. <tt>p\\(p*q\/p)*p<\/tt> should be within roundoff error of <tt>q<\/tt>.<\/p><pre class=\"codeinput\">   test1 = norm(p\\r*p - q)\r\n<\/pre><pre class=\"codeoutput\">\r\ntest1 =\r\n\r\n   4.4409e-16\r\n\r\n<\/pre><h4>rotations<a name=\"b8a4c856-760b-455c-8630-70d7850c53fc\"><\/a><\/h4><pre class=\"codeinput\">   type <span class=\"string\">rot<\/span>\r\n<\/pre><pre class=\"codeoutput\">\r\nfunction r = rot(theta, v)\r\n    % rot   r = rot(theta,v) is the quaternion (qtn) for\r\n    % rotation of angle theta, in degrees, about axis v.\r\n    v = v\/norm(v);\r\n    r = qtn(cosd(theta), sind(theta)*v);\r\nend\r\n<\/pre><p>A random axis of rotation.<\/p><pre class=\"codeinput\">   v = randn(1,3)\r\n<\/pre><pre class=\"codeoutput\">\r\nv =\r\n\r\n   -0.1241    1.4897    1.4090\r\n\r\n<\/pre><p>Rotate 36 degrees about <tt>v.<\/tt><\/p><pre class=\"codeinput\">   r = rot(36,v)\r\n<\/pre><pre class=\"codeoutput\">\r\nr = \r\n\r\n   0.809017 - 0.0355214i + 0.426247j + 0.403167k\r\n<\/pre><p>Repeating the 36 degree rotation 10 times is a 360 degree rotation, which should be within roundoff error of the quaternion multiplicative identity.<\/p><pre class=\"codeinput\">   e = qtn(1)\r\n\r\n   test2 = norm(r^10 - e)\r\n<\/pre><pre class=\"codeoutput\">\r\ne = \r\n\r\n   1 + 0i + 0j + 0k\r\n\r\ntest2 =\r\n\r\n   4.6059e-16\r\n\r\n<\/pre><h4>Code<a name=\"d66b61c3-18e5-49a5-8781-47a35584a369\"><\/a><\/h4><p>(Added, September 11, 2021). You can get the code for <tt>qtn<\/tt> and related functions from these links: <a href=\"https:\/\/blogs.mathworks.com\/cleve\/files\/qtn.m\">qtn.m<\/a>, <a href=\"https:\/\/blogs.mathworks.com\/cleve\/files\/rot.m\">rot.m<\/a>, <a href=\"https:\/\/blogs.mathworks.com\/cleve\/files\/Euler.m\">Euler.m<\/a>, <a href=\"https:\/\/blogs.mathworks.com\/cleve\/files\/randq.m\">randq.m<\/a>, <a href=\"https:\/\/blogs.mathworks.com\/cleve\/files\/qtn_examples_1.m\">qtn_examples_1.m<\/a>.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_2fe5a45d67224755892f853c0bcb1197() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='2fe5a45d67224755892f853c0bcb1197 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 2fe5a45d67224755892f853c0bcb1197';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2021 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_2fe5a45d67224755892f853c0bcb1197()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2021b<br><\/p><\/div><!--\r\n2fe5a45d67224755892f853c0bcb1197 ##### SOURCE BEGIN #####\r\n%% Quaternions\r\n% Quaternions are generalizations of the complex numbers that have\r\n% found applications in computer graphics and many other fields.\r\n\r\n%% Ed Angel\r\n% Ed Angel is my longtime friend from Caltech and New Mexico days. \r\n% Ed is the author of\r\n% <https:\/\/www.amazon.com\/Edward-Angel\/e\/B001IOF5SE\r\n% a popular series of textbooks>\r\n% about computer graphics that has now reached its 8th edition.\r\n% The books feature the evolution of the OpenGL programming language\r\n% from Silicon Graphics to WebGL and JavaScript.\r\n% Ed is working on a MATLAB version and I am kibitzing.\r\n\r\n%% Two dimensions\r\n% Life in two dimensions is easy.\r\n% A point in the plane is readily described by its cartesian coordinates,\r\n% $(x,y)$, or by its polar coordinates, $(r,\\theta)$, or by a single complex\r\n% number $z$.  \r\n% Rotation of the point about the origin is easily described by \r\n% multiplying the cartesian coordinates by a 2-by-2 orthogonal matrix,\r\n% or by incrementing the polar angle, or by multiplying $z$ by another\r\n% complex number.\r\n\r\n%% Three dimensions\r\n% The situation in three dimensions is more complicated.\r\n% There are cartesian coordinates $(x,y,z)$, or spherical coordinates\r\n% $(r,\\theta,\\psi)$.  But what is the three-dimensional analog of the\r\n% single complex number?\r\n% Rotations are even more complicated.\r\n% They require the specification of an axis of rotation as well as\r\n% the angle.  And there are lots of 3-by-3 orthogonal matrices that\r\n% generate rotations to choose from.\r\n\r\n%% William Rowan Hamilton\r\n% William Rowan Hamilton (1805-1865) was an Irish mathematician, a\r\n% professor at Trinity College Dublin, and the Royal Astronomer of Ireland.\r\n% His name is associated with important concepts throughout mathematics and\r\n% physics. \r\n% Like many other mathematicians of his era, he tried unsuccessfully\r\n% to generalize two-dimensional complex numbers by adding a second\r\n% complex unit.  In 1846 he realized that he actually needed to add\r\n% three complex units, $i$, $j$ and $k$.\r\n\r\n%% Dot and cross products\r\n% The key ingredients of Hamilton's quaternion arithmetic are dot and\r\n% cross products.  The dot product, also known as the inner product, is a\r\n% familiar feature of linear algebra in any number of dimensions.\r\n% The dot product of two vectors is a scalar.\r\n%\r\n% $$ a \\cdot b = a_1 b_1 \\ + \\ a_2 b_2 \\ + \\ a_3 b_3 $$\r\n%\r\n\r\n%%\r\n% The cross product is defined only in three dimensions.\r\n% The cross product of two vectors is a third vector,\r\n% oriented to be perpendicular to the plane spanned by the first two.\r\n% If $i$, $j$ and $k$ are unit vectors for the axes in three dimensions,\r\n% then the cross product of vectors $a$ and $b$ is\r\n%\r\n% $$ a \\times b = (a_2 b_3 - a_3 b_2) \\ i \\ + \\ (a_3 b_1 - a_1 b_3) \\ j \r\n%  \\ + \\ (a_1 b_2 - a_2 b_1) \\ k $$\r\n\r\n%%\r\n% Here is a snapshot of the cross product.\r\n%\r\n% <<cross_pic.png>>\r\n\r\n%%\r\n% There are two possible orientations for the vector perpendicular\r\n% to the plane spanned by $a$ and $b$.  The conventional choice is\r\n% made by the right hand rule illustrated on this Swiss bank note\r\n% and by this emoji on the sign for the Facebook corporate offices.\r\n%\r\n% <<CHF_200.jpg>>\r\n%\r\n% <<thumbs_up.jpg>>\r\n\r\n%% Quaternions\r\n% A quaternion $q$ has two parts, a scalar $s$ and a vector $v$.\r\n% The scalar is one real number; think of $s$ as the coefficient\r\n% of the scalar unit, $1$.  The vector consists of three real numbers;\r\n% they are the coefficients of three imaginary\r\n% units, $i$, $j$ and $k$.  So\r\n% \r\n% $$ q = [s,v] = s 1 \\ + \\ v_1 i \\ + v_2 j \\ + \\ v_3 k $$\r\n%\r\n% Quaternion multiplication is defined so that\r\n% each of the imaginary units is a square root of $-1$ .\r\n%\r\n% $$ i^2 = -1, \\ \\ j^2 = -1, \\ \\ k^2 = -1 $$\r\n%\r\n% Furthermore, the three units are related to each other by a cyclic property\r\n% of their products.\r\n%\r\n% $$ ij = k, \\ \\ jk = i, \\ \\ ki = j $$\r\n%\r\n% These properties imply\r\n%\r\n% $$ ji = -k, \\ \\ kj = -i, \\ \\ ik = -j $$\r\n%\r\n\r\n%%\r\n% Let's call these properties of $i$, $j$ and $k$ under multiplication\r\n% \"Hamilton's Rules\".\r\n\r\n%% Quaternion addition\r\n% Suppose we have two quaternions.\r\n%\r\n% $$ p = [r,u] = r 1 \\ + \\ u_1 i \\ + u_2 j \\ + \\ u_3 k $$\r\n%\r\n% $$ q = [s,v] = s 1 \\ + \\ v_1 i \\ + v_2 j \\ + \\ v_3 k $$\r\n%\r\n% Addition is the obvious.\r\n%\r\n% $$ p + q = [r+s, u+v] $$\r\n%\r\n\r\n%% Quaternion multiplication\r\n% Quaternion multiplication is ingenious and complicated.\r\n%\r\n% $$ p q = (r + u_1 i + u_2 j + u_3 k)(s + v_1 i + v_2 j + v_3 k) $$\r\n%\r\n% There are four terms in each parentheses, so there are 16 terms in\r\n% the product.  Expand carefully because $i$, $j$ and $k$ do not commute.\r\n% Apply Hamilton's Rules, $i^2 = -1$, $i j = k$, $j i = -k$, etc.\r\n% The 16 terms can be arranged like this.\r\n%\r\n% $$ p q = [\\ r s - (u_1 v_1 + u_2 v_2 + u_3 v_3) \\ , $$\r\n%\r\n% $$ \\ \\ \\ \\ \\ \\ \\ \\ r (v_1 i + v_2 j + v_3 k) + s (u_1 i + u_2 j + u_3 k) \\ + \r\n%  (u_2 v_3 - u_3 v_2) i + (u_3 v_1 - u_1 v_3) j + (u_1 v_2 - u_2 v_1) k \\ ] $$\r\n%\r\n% We recognize two familiar quantities, the dot and cross products.\r\n%\r\n% $$ u \\cdot v = u_1 v_1 + u_2 v_2 + u_3 v_3 $$\r\n%\r\n% $$ u \\times v = (u_2 v_3 - u_3 v_2) i + (u_3 v_1 - u_1 v_3) j + (u_1 v_2 - u_2 v_1) k $$\r\n%\r\n% So the result in\r\n%\r\n% $$ p q = [r s - u \\cdot v, r v + s u + u \\times v ] $$\r\n%\r\n% This is Hamilton's definition of the quaternion product.\r\n\r\n%%\r\n% The units $i$, $j$ and $k$ have scalar parts equal to 0 and\r\n% vector parts equal to [1,0,0], [0,1,0] and [0,0,1].\r\n% Plugging these into the product definition produces, for\r\n% example,\r\n%\r\n% $$ i^2 = [0 -i \\cdot i, 0 + 0 + i \\times i] = -1 $$,\r\n%\r\n% $$ i j = [0 - 0, 0 + 0 + i \\times j] = k $$\r\n%\r\n% and\r\n%\r\n% $$ j i = j \\times i = - i \\times j = - k $$\r\n\r\n%% A Capella Science\r\n% A Capella Science is a Patreon YouTube channel.\r\n% <https:\/\/www.patreon.com\/acapellascience>.\r\n\r\n%%\r\n% In 2016, A Capella Science produced a video featuring two Hamiltons,\r\n% the immensely popular Broadway musical Hamilton and the Irish\r\n% mathematician Hamilton.\r\n% <https:\/\/www.youtube.com\/watch?v=SZXHoWwBcDc>.\r\n% Here is a screen capture of one frame from that video.\r\n%\r\n% <<ACapella.png>>\r\n\r\n%% |qtn|\r\n% The 2021a and 2021b releases of MATLAB include a |quaternion|\r\n% object that is shared among several of the navigation toolboxes.\r\n%\r\n% Ed and I have our own more basic object, |qtn|.\r\n\r\n   help qtn\r\n\r\n%%\r\n% The most important |qtn| method is the quaternion product.\r\n% I think this code is elegant.\r\n\r\n   dbtype 89:96 qtn\r\n\r\n%%\r\n% The entire |qtn| class definition is only about 150 lines long.\r\n   \r\n%% basic\r\n% Here are a few examples and tests of our basic quaternion object.\r\n\r\n%%\r\n% Two of the units.\r\n\r\n   i = qtn(0,[1 0 0])\r\n   j = qtn(0,[0 1 0])\r\n\r\n%%\r\n% Their product is the third unit.\r\n\r\n   k = i*j\r\n\r\n%%\r\n% All three of these are -1.\r\n\r\n   ijk = i*j*k\r\n   isq = i^2\r\n   ii = i*i \r\n\r\n%% random\r\n% Two random |qtn|'s with integer coefficients\r\n\r\n   p = randq\r\n   q = randq\r\n   \r\n%%\r\n% Addition.\r\n\r\n   p_plus_q = p + q\r\n\r\n%%\r\n% Multiplication.\r\n\r\n   pq = p*q\r\n\r\n%%\r\n% Right division.\r\n\r\n   r = pq\/p\r\n   \r\n%%\r\n% Left division.\r\n% |p\\(p*q\/p)*p| should be within roundoff error of |q|.\r\n\r\n   test1 = norm(p\\r*p - q) \r\n\r\n%% rotations\r\n\r\n   type rot\r\n\r\n%%\r\n% A random axis of rotation.\r\n\r\n   v = randn(1,3)\r\n\r\n%%\r\n% Rotate 36 degrees about |v.|\r\n\r\n   r = rot(36,v)\r\n\r\n%%\r\n% Repeating the 36 degree rotation 10 times is a 360 degree\r\n% rotation, which should be within roundoff error of the quaternion \r\n% multiplicative identity.\r\n\r\n   e = qtn(1)    \r\n\r\n   test2 = norm(r^10 - e)\r\n\r\n%% Code\r\n% (Added, September 11, 2021).\r\n% You can get the code for |qtn| and related functions from\r\n% these links:\r\n% <https:\/\/blogs.mathworks.com\/cleve\/files\/qtn.m qtn.m>,\r\n% <https:\/\/blogs.mathworks.com\/cleve\/files\/rot.m rot.m>,\r\n% <https:\/\/blogs.mathworks.com\/cleve\/files\/Euler.m Euler.m>,\r\n% <https:\/\/blogs.mathworks.com\/cleve\/files\/randq.m randq.m>,\r\n% <https:\/\/blogs.mathworks.com\/cleve\/files\/qtn_examples_1.m qtn_examples_1.m>.\r\n\r\n\r\n##### SOURCE END ##### 2fe5a45d67224755892f853c0bcb1197\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/cleve\/files\/cross_pic.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><!--introduction--><p>Quaternions are generalizations of the complex numbers that have found applications in computer graphics and many other fields.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/cleve\/2021\/09\/06\/quaternions\/\">read more >><\/a><\/p>","protected":false},"author":78,"featured_media":7360,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,23,4,8,37],"tags":[40],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/7255"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/users\/78"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/comments?post=7255"}],"version-history":[{"count":7,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/7255\/revisions"}],"predecessor-version":[{"id":7440,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/posts\/7255\/revisions\/7440"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/media\/7360"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/media?parent=7255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/categories?post=7255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/cleve\/wp-json\/wp\/v2\/tags?post=7255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}