Skip to content

Instantly share code, notes, and snippets.

@kdrnic
Created September 24, 2021 04:24
Show Gist options
  • Save kdrnic/da2f5cf2e0b6494f29a9340a15877e88 to your computer and use it in GitHub Desktop.
Save kdrnic/da2f5cf2e0b6494f29a9340a15877e88 to your computer and use it in GitHub Desktop.
EXPANDED FORMS OF QUATERNION PRODUCTS
HAMILTON'S
with
s1=w
s2=w'
v1=[x,y,z]
v2=[x',y',z']
qp = [s1,v1][s2,v2] = [s1s2 - v1.v2, s1v2 + s2v1 + v1 x v2]
(w1 * w2) - x1 * x2 - y1 * y2 - z1 * z2
(w1 * x2) + (x1 * w2) + (y1 * z2 - z1 * y2)
(w1 * y2) + (y1 * w2) + (z1 * x2 - x1 * z2)
(w1 * z2) + (z1 * w2) + (x1 * y2 - y1 * x2)
w1 * w2 - x1 * x2 - y1 * y2 - z1 * z2
w1 * x2 + x1 * w2 + y1 * z2 - z1 * y2
w1 * y2 - x1 * z2 + y1 * w2 + z1 * x2
w1 * z2 + x1 * y2 - y1 * x2 + z1 * w2
w" = ww' - xx' - yy` - zz`
x" = wx' + xw' + yz` - zy`
y" = wy' - xz' + yw` + zx`
z" = wz' + xy' - yx` + zw`
SHUSTER'S
qp = [s1,v1][s2,v2] = [s1s2 - v1.v2, s1v2 + s2v1 - v1 x v2]
(w1 * w2) - x1 * x2 - y1 * y2 - z1 * z2
(w1 * x2) + (x1 * w2) - (y1 * z2 - z1 * y2)
(w1 * y2) + (y1 * w2) - (z1 * x2 - x1 * z2)
(w1 * z2) + (z1 * w2) - (x1 * y2 - y1 * x2)
w1 * w2 - x1 * x2 - y1 * y2 - z1 * z2
w1 * x2 + x1 * w2 - y1 * z2 + z1 * y2
w1 * y2 + y1 * w2 - z1 * x2 + x1 * z2
w1 * z2 + z1 * w2 - x1 * y2 + y1 * x2
w1 * w2 - x1 * x2 - y1 * y2 - z1 * z2
w1 * x2 + x1 * w2 - y1 * z2 + z1 * y2
w1 * y2 + x1 * z2 + y1 * w2 - z1 * x2
w1 * z2 - x1 * y2 + y1 * x2 + z1 * w2
w" = ww' - xx' - yy' - zz'
x" = wx' + xw' - yz' + zy'
y" = wy' + xz' + yw' - zx'
z" = wz' - xy' + yx' + zw'
TWO FORMS OF QUATERNION ROTATIONS
since (pq)^-1 = (q^-1)(p^-1)
v' = q^-1 v q -> q1q2 ? (q1q2)^-1 v (q1q2) = q2^-1 q1^-1 v q1 q2 = q1 followed by q2
v' = q v q^-1 -> q2q1 ? (q2q1) v (q2q1)^-1 = q2 q1 v q1^-1 q2^-1 = q1 followed by q2
DERIVING ROTATION MATRICES EQUIVALENT TO QUATERNION ROTATIONS
HAMILTON
[s1,v1][s2,v2] = [s1s2 - v1.v2, s1v2 + s2v1 + v1 X v2]
|s1 -v1x -v1y -v1z| |s2 | |s1s2-v1.v2 |
|0 0 0 0 | |v2x| = | 0 | = [s1s2-v1.v2,0]
|0 0 0 0 | |v2y| | 0 |
|0 0 0 0 | |v2z| | 0 |
|0 0 0 0 | |s2 | | 0 |
|0 s1 0 0 | |v2x| = |s1*v2x | = [0,s1*v2]
|0 0 s1 0 | |v2y| |s1*v2y |
|0 0 0 s1 | |v2z| |s1*v2z |
|0 0 0 0 | |s2 | | 0 |
|v1x 0 0 0 | |v2x| = |s2*v1x | = [0,s2*v1]
|v1y 0 0 0 | |v2y| |s2*v1y |
|v1z 0 0 0 | |v2z| |s2*v1z |
|0 0 0 0 | |s2 | | 0 |
|0 0 -v1z v1y | |v2x| = |v1y*v2z-v1z*v2y| = [0,v1 X v2]
|0 v1z 0 -v1x| |v2y| |v1z*v2x-v1x*v2z|
|0 -v1y v1x 0 | |v2z| |v1x*v2y-v1y*v2x|
|s1 -v1x -v1y -v1z| |s2 |
|v1x s1 -v1z v1y | |v2x| = [s1s2 - v1.v2, s1v2 + s2v1 + v1 X v2] = premult
|v1y v1z s1 -v1x| |v2y|
|v1z -v1y v1x s1 | |v2z|
|s1 v1x v1y v1z | |s2 |
|-v1x s1 v1z -v1y| |v2x| = [s1s2 + v1.v2, s1v2 - s2v1 - v1 X v2] = conjugated premult
|-v1y -v1z s1 v1x | |v2y|
|-v1z v1y -v1x s1 | |v2z|
|s1 -v1x -v1y -v1z| |s2 |
|v1x s1 v1z -v1y| |v2x| = [s1s2 - v1.v2, s1v2 + s2v1 + v2 X v1] = postmult
|v1y -v1z s1 v1x | |v2y|
|v1z v1y -v1x s1 | |v2z|
|s1 v1x v1y v1z | |s2 |
|-v1x s1 -v1z v1y | |v2x| = [s1s2 + v1.v2, s1v2 - s2v1 - v2 X v1] = conjugated postmult
|-v1y v1z s1 -v1x| |v2y|
|-v1z -v1y v1x s1 | |v2z|
q* = {{w,-x,-y,-z},{x,w,-z,y},{y,z,w,-x},{z,-y,x,w}}
(q^-1)* = {{w,x,y,z},{-x,w,z,-y},{-y,-z,w,x},{-z,y,-x,w}}
*q = {{w,-x,-y,-z},{x,w,z,-y},{y,-z,w,x},{z,y,-x,w}}
*(q^-1) = {{w,x,y,z},{-x,w,-z,y},{-y,z,w,-x},{-z,-y,x,w}}
q*v*(q^-1) = {{w,-x,-y,-z},{x,w,-z,y},{y,z,w,-x},{z,-y,x,w}}*{{w,x,y,z},{-x,w,-z,y},{-y,z,w,-x},{-z,-y,x,w}}
(q^-1)*v*q = {{w,x,y,z},{-x,w,z,-y},{-y,-z,w,x},{-z,y,-x,w}}*{{w,-x,-y,-z},{x,w,z,-y},{y,-z,w,x},{z,y,-x,w}}
q*v*(q^-1) =
= {{w^2 + x^2 + y^2 + z^2, 0, 0, 0}, {0, w^2 + x^2 - y^2 - z^2, 2 x y - 2 w z, 2 w y + 2 x z}, {0, 2 x y + 2 w z, w^2 - x^2 + y^2 - z^2, -2 w x + 2 y z}, {0, -2 w y + 2 x z, 2 w x + 2 y z, w^2 - x^2 - y^2 + z^2}}
-> {{w^2 + x^2 - y^2 - z^2, 2 x y - 2 w z, 2 w y + 2 x z}, {2 x y + 2 w z, w^2 - x^2 + y^2 - z^2, -2 w x + 2 y z}, {-2 w y + 2 x z, 2 w x + 2 y z, w^2 - x^2 - y^2 + z^2}}
=
|w²+x²-y²-z² 2xy-2wz 2wy+2xz |
|2xy+2wz w²-x²+y²-z² 2yz-2wx |
|2xz-2wy 2wx+2yz w²-x²-y²+z² |
=
|1-2(y²+z²) 2(xy-wz) 2(wy+xz) |
|2(xy+wz) 1-2(x²+z²) 2(yz-wx) |
|2(xz-wy) 2(wx+yz) 1-2(x²+y²) |
Above is one of a few possible simplifications:
M11 = w²+x²-y²-z² = 2w²+2x²-1 = 2(w²+x²)-1 = 1-2y²-2z² = 1-2(y²+z²)
M22 = w²-x²+y²-z² = 2w²+2y²-1 = 2(w²+y²)-1 = 1-2x²-2z² = 1-2(x²+z²)
M33 = w²-x²-y²+z² = 2w²+2z²-1 = 2(w²+z²)-1 = 1-2x²-2y² = 1-2(x²+y²)
(q^-1)*v*q =
{{w^2 + x^2 + y^2 + z^2, 0, 0, 0}, {0, w^2 + x^2 - y^2 - z^2, 2 x y + 2 w z, -2 w y + 2 x z}, {0, 2 x y - 2 w z, w^2 - x^2 + y^2 - z^2, 2 w x + 2 y z}, {0, 2 w y + 2 x z, -2 w x + 2 y z, w^2 - x^2 - y^2 + z^2}}
-> {{w^2 + x^2 - y^2 - z^2, 2 x y + 2 w z, -2 w y + 2 x z}, {2 x y - 2 w z, w^2 - x^2 + y^2 - z^2, 2 w x + 2 y z}, {2 w y + 2 x z, -2 w x + 2 y z, w^2 - x^2 - y^2 + z^2}}
=
|w²+x²-y²-z² 2xy+2wz 2xz-2wy |
|2xy-2wz w²-x²+y²-z² 2wx+2yz |
|2wy+2xz 2yz-2wx w²-x²-y²+z² |
=
|1-2(y²+z²) 2(xy+wz) 2(xz-wy) |
|2(xy-wz) 1-2(x²+z²) 2(wx+yz) |
|2(wy+xz) 2(yz-wx) 1-2(x²+y²) |
SHUSTER
|s1 -v1x -v1y -v1z| |s2 |
|v1x s1 v1z -v1y| |v2x| = [s1s2 - v1.v2, s1v2 + s2v1 - v1 X v2] = premult
|v1y -v1z s1 v1x | |v2y|
|v1z v1y -v1x s1 | |v2z|
|s1 v1x v1y v1z | |s2 |
|-v1x s1 -v1z v1y | |v2x| = [s1s2 + v1.v2, s1v2 - s2v1 + v1 X v2] = conjugated premult
|-v1y v1z s1 -v1x| |v2y|
|-v1z -v1y v1x s1 | |v2z|
|s1 -v1x -v1y -v1z| |s2 |
|v1x s1 -v1z v1y | |v2x| = [s1s2 - v1.v2, s1v2 + s2v1 - v2 X v1] = postmult
|v1y v1z s1 -v1x| |v2y|
|v1z -v1y v1x s1 | |v2z|
|s1 v1x v1y v1z | |s2 |
|-v1x s1 v1z -v1y| |v2x| = [s1s2 + v1.v2, s1v2 - s2v1 + v2 X v1] = conjugated postmult
|-v1y -v1z s1 v1x | |v2y|
|-v1z v1y -v1x s1 | |v2z|
q*v*(q^-1) =
{{w^2 + x^2 + y^2 + z^2, 0, 0, 0}, {0, w^2 + x^2 - y^2 - z^2, 2 x y + 2 w z, -2 w y + 2 x z}, {0, 2 x y - 2 w z, w^2 - x^2 + y^2 - z^2, 2 w x + 2 y z}, {0, 2 w y + 2 x z, -2 w x + 2 y z, w^2 - x^2 - y^2 + z^2}}
-> {{w^2 + x^2 - y^2 - z^2, 2 x y + 2 w z, -2 w y + 2 x z}, {2 x y - 2 w z, w^2 - x^2 + y^2 - z^2, 2 w x + 2 y z}, {2 w y + 2 x z, -2 w x + 2 y z, w^2 - x^2 - y^2 + z^2}}
=
|w²+x²-y²-z² 2xy+2wz 2xz-2wy |
|2xy-2wz w²-x²+y²-z² 2wx+2yz |
|2wy+2xz 2yz-2wx w²-x²-y²+z² |
=
|1-2(y²+z²) 2(xy+wz) 2(xz-wy) |
|2(xy-wz) 1-2(x²+z²) 2(wx+yz) |
|2(wy+xz) 2(yz-wx) 1-2(x²+y²) |
(q^-1)*v*q =
= {{w^2 + x^2 + y^2 + z^2, 0, 0, 0}, {0, w^2 + x^2 - y^2 - z^2, 2 x y - 2 w z, 2 w y + 2 x z}, {0, 2 x y + 2 w z, w^2 - x^2 + y^2 - z^2, -2 w x + 2 y z}, {0, -2 w y + 2 x z, 2 w x + 2 y z, w^2 - x^2 - y^2 + z^2}}
-> {{w^2 + x^2 - y^2 - z^2, 2 x y - 2 w z, 2 w y + 2 x z}, {2 x y + 2 w z, w^2 - x^2 + y^2 - z^2, -2 w x + 2 y z}, {-2 w y + 2 x z, 2 w x + 2 y z, w^2 - x^2 - y^2 + z^2}}
=
|w²+x²-y²-z² 2xy-2wz 2wy+2xz |
|2xy+2wz w²-x²+y²-z² 2yz-2wx |
|2xz-2wy 2wx+2yz w²-x²-y²+z² |
=
|1-2(y²+z²) 2(xy-wz) 2(wy+xz) |
|2(xy+wz) 1-2(x²+z²) 2(yz-wx) |
|2(xz-wy) 2(wx+yz) 1-2(x²+y²) |
DERIVING A ROTATION MATRIX OF P AROUND AXIS R BY ANGLE ALPHA
z = r(r.p)
x = p - z
y = r X x = r X p - r X z = r X p - r X r(r.p) = r X p - 0 = r X p
p' = z + cos(alpha)x + sin(alpha)y
p'= Mz*p + cos(alpha)Mx*p + sin(alpha)My*p = (Mz + cos(alpha)Mx + sin(alpha)My)p = Mp*p
| x 0 0 | | x y z | | x² xy xz |
Mz = | 0 y 0 | | x y z | = | xy y² yz |
| 0 0 z | | x y z | | xz yz z² |
{{x^2,x y,x z},{x y,y^2,y z},{x z,y z,z^2}}
| 1-x² 1-xy 1-xz |
Mx = | 1-xy 1-y² 1-yz |
| 1-xz 1-yz 1-z² |
{{1-x^2,1-x y,1-x z},{1-x y,1-y^2,1-y z},{1-x z,1-y z,1-z^2}}
| 0 -z y |
My = | z 0 -x |
| -y x 0 |
with C=cos(alpha) and S=sin(alpha)
| x²+C-Cx²+S0 xy+C-Cxy-Sz xz+C-Cxz+Sy |
Mp= | xy+C-Cxy+Sz y²+C-Cy²+S0 yz+C-Cyz-Sy |
| xz+C-Cxz-Sy yz+C-Cyz+Sx z²+C-Cz²+S0 |
| x²+C-Cx² xy+C-Cxy-Sz xz+C-Cxz+Sy |
Mp= | xy+C-Cxy+Sz y²+C-Cy² yz+C-Cyz-Sy |
| xz+C-Cxz-Sy yz+C-Cyz+Sx z²+C-Cz² |
| C+x²(1-C) xy(1-C)-Sz xz(1-C)+Sy |
Mp= | xy(1-C)+Sz C+y²(1-C) yz(1-C)-Sy |
| xz(1-C)-Sy yz(1-C)+Sx C+z²(1-C) |
DERIVING A QUATERNION FOR A ROTATION AROUND AN AXIS BY AN ANGLE
From the quaternion matrix:
M11 + M22 + M33 = T = 1-2(y²+z²) + 1-2(x²+z²) + 1-2(x²+y²)
T = 3-2(2y²+2z²+2x²)
T = 3-4y²-4z²-4x²
by using w²+x²+y²+z²=1: T+4 = 3+4w²
T+1 = 4w²
(T+1)/4 = w²
From the rotation matrix
(on the lhs x,y,z are those of the axis of rotation not those of the quaternion):
(C+x²(1-C) + C+y²(1-C) + C+z²(1-C) + 1)/4 = w²
(3C + (x²+y²+z²)(1-C) + 1)/4 = w²
by using x²+y²+z²=1: (3C + 1-C + 1)/4 = w²
(2C + 2)/4 = w²
(C + 1)/2 = w²
sqrt((C + 1)/2) = w
cos(alpha/2)=sqrt((cos(alpha)+1)/2): cos(alpha/2) = w
Finding remaining quaternion components
(lhs x,y,z are those of the quaternion while those in rhs are those of the axis of rotation):
M11 = 2w²+2x²-1 = C+x²(1-C) M22 = 2w²+2y²-1 = C+y²(1-C) M33 = 2w²+2z²-1 = C+z²(1-C)
(C+1)+2x²-1 = C+x²(1-C) (C+1)+2y²-1 = C+y²(1-C) (C+1)+2z²-1 = C+z²(1-C)
C+2x² = C+x²(1-C) C+2y² = C+y²(1-C) C+2z² = C+z²(1-C)
2x² = x²(1-C) 2y² = y²(1-C) 2z² = z²(1-C)
x = sqrt(x²(1-C))/2 y = sqrt(y²(1-C))/2 z = sqrt(z²(1-C))
x = x sqrt(1-C)/2 y = y sqrt(1-C)/2 z = z sqrt(1-C)/2
sqrt(1-cos alpha)=sin(alpha/2): x = sin(alpha/2) x y = sin(alpha/2) y z = sin(alpha/2) z
Therefore, a rotation around r by angle alpha is described by the quaternion
Q = [cos(alpha/2), r sin(alpha/2)]
PROOF OF TRIG IDENTITIES USED
(cos alpha)² + (sin alpha)² = 1
(cos alpha)² + (sin alpha)² - 1 = 0
(cos alpha)² + (sin alpha)² = 1
1 - (cos alpha)² - (sin alpha)² = 0
cos(alpha + beta) = cos alpha cos beta - sin alpha sin beta
cos(2 alpha) = cos(alpha + alpha)
cos(2 alpha) = cos alpha cos alpha - sin alpha sin alpha
cos(2 alpha) = (cos alpha)² - (sin alpha)²
cos(2 alpha) = (cos alpha)² - (sin alpha)²
cos(2 alpha) = (cos alpha)² - (sin alpha)² + (cos alpha)² + (sin alpha)² - 1
cos(2 alpha) = 2(cos alpha)² - 1
2(cos alpha)² - 1 = cos(2 alpha)
2(cos alpha)² = cos(2 alpha) + 1
(cos alpha)² = (cos(2 alpha) + 1)/2
cos(alpha) = sqrt((cos(2 alpha) + 1)/2)
cos(alpha/2) = sqrt((cos(alpha) + 1)/2)
cos(2 alpha) = (cos alpha)² - (sin alpha)²
cos(2 alpha) = (cos alpha)² - (sin alpha)² - (cos alpha)² - (sin alpha)² + 1
cos(2 alpha) = 1-2(sin alpha)²
cos(alpha) = 1-2(sin(alpha/2))²
2(sin(alpha/2))² = 1 - cos(alpha)
(sin(alpha/2))² = (1 - cos(alpha))/2
sin(alpha/2) = sqrt((1 - cos(alpha))/2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment