SharedEulerMatrix.sagewsOpen in CoCalc
Author: Zach Brockway

(x, y, z) = var('theta_x, theta_y, theta_z')
(sx, sy, sz) = (sin(x), sin(y), sin(z))
(cx, cy, cz) = (cos(x), cos(y), cos(z))

Rx = Matrix([
[1,      0,       0],
[0, cos(x), -sin(x)],
[0, sin(x),  cos(x)]
])

Ry = Matrix([
[ cos(y), 0, sin(y)],
[      0, 1,      0],
[-sin(y), 0, cos(y)]
])

Rz = Matrix([
[cos(z), -sin(z), 0],
[sin(z),  cos(z), 0],
[     0,       0, 1]
])

show( '$R_x=$', Rx, '$, R_y=$', Ry, '$, R_z=$', Rz )
show( '$R_x R_y R_z=$', Rx * Ry * Rz )


$R_x=$ $\displaystyle \left(\begin{array}{rrr} 1 & 0 & 0 \\ 0 & \cos\left(\theta_{x}\right) & -\sin\left(\theta_{x}\right) \\ 0 & \sin\left(\theta_{x}\right) & \cos\left(\theta_{x}\right) \end{array}\right)$ $, R_y=$ $\displaystyle \left(\begin{array}{rrr} \cos\left(\theta_{y}\right) & 0 & \sin\left(\theta_{y}\right) \\ 0 & 1 & 0 \\ -\sin\left(\theta_{y}\right) & 0 & \cos\left(\theta_{y}\right) \end{array}\right)$ $, R_z=$ $\displaystyle \left(\begin{array}{rrr} \cos\left(\theta_{z}\right) & -\sin\left(\theta_{z}\right) & 0 \\ \sin\left(\theta_{z}\right) & \cos\left(\theta_{z}\right) & 0 \\ 0 & 0 & 1 \end{array}\right)$
$R_x R_y R_z=$ $\displaystyle \left(\begin{array}{rrr} \cos\left(\theta_{y}\right) \cos\left(\theta_{z}\right) & -\cos\left(\theta_{y}\right) \sin\left(\theta_{z}\right) & \sin\left(\theta_{y}\right) \\ \cos\left(\theta_{z}\right) \sin\left(\theta_{x}\right) \sin\left(\theta_{y}\right) + \cos\left(\theta_{x}\right) \sin\left(\theta_{z}\right) & -\sin\left(\theta_{x}\right) \sin\left(\theta_{y}\right) \sin\left(\theta_{z}\right) + \cos\left(\theta_{x}\right) \cos\left(\theta_{z}\right) & -\cos\left(\theta_{y}\right) \sin\left(\theta_{x}\right) \\ -\cos\left(\theta_{x}\right) \cos\left(\theta_{z}\right) \sin\left(\theta_{y}\right) + \sin\left(\theta_{x}\right) \sin\left(\theta_{z}\right) & \cos\left(\theta_{x}\right) \sin\left(\theta_{y}\right) \sin\left(\theta_{z}\right) + \cos\left(\theta_{z}\right) \sin\left(\theta_{x}\right) & \cos\left(\theta_{x}\right) \cos\left(\theta_{y}\right) \end{array}\right)$

rXyz = Rx * Ry * Rz

eberlyXyz = Matrix([
[             cy * cz,              -(cy * sz),            sy],
[   cz * sx * sy + cx * sz,    cx * cz - sx * sy * sz, -(cy * sx)],
[-(cx * cz * sy) + (sx * sz),  cz * sx + cx * sy * sz,   cx * cy]
])

unrealXyz = Matrix([
[            cy * cz,               cy * sz,            sy],
[  sx * sy * cz - cx * sz,   sx * sy * sz + cx * cz, - sx * cy],
[-(cx * sy * cz + sx * sz),  cz * sx - cx * sy * sz,   cx * cy]
])

show( eberlyXyz )
show( eberlyXyz - rXyz )

show( unrealXyz )
show( unrealXyz - rXyz )


$\displaystyle \left(\begin{array}{rrr} \cos\left(\theta_{y}\right) \cos\left(\theta_{z}\right) & -\cos\left(\theta_{y}\right) \sin\left(\theta_{z}\right) & \sin\left(\theta_{y}\right) \\ \cos\left(\theta_{z}\right) \sin\left(\theta_{x}\right) \sin\left(\theta_{y}\right) + \cos\left(\theta_{x}\right) \sin\left(\theta_{z}\right) & -\sin\left(\theta_{x}\right) \sin\left(\theta_{y}\right) \sin\left(\theta_{z}\right) + \cos\left(\theta_{x}\right) \cos\left(\theta_{z}\right) & -\cos\left(\theta_{y}\right) \sin\left(\theta_{x}\right) \\ -\cos\left(\theta_{x}\right) \cos\left(\theta_{z}\right) \sin\left(\theta_{y}\right) + \sin\left(\theta_{x}\right) \sin\left(\theta_{z}\right) & \cos\left(\theta_{x}\right) \sin\left(\theta_{y}\right) \sin\left(\theta_{z}\right) + \cos\left(\theta_{z}\right) \sin\left(\theta_{x}\right) & \cos\left(\theta_{x}\right) \cos\left(\theta_{y}\right) \end{array}\right)$
$\displaystyle \left(\begin{array}{rrr} 0 & 0 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \end{array}\right)$
$\displaystyle \left(\begin{array}{rrr} \cos\left(\theta_{y}\right) \cos\left(\theta_{z}\right) & \cos\left(\theta_{y}\right) \sin\left(\theta_{z}\right) & \sin\left(\theta_{y}\right) \\ \cos\left(\theta_{z}\right) \sin\left(\theta_{x}\right) \sin\left(\theta_{y}\right) - \cos\left(\theta_{x}\right) \sin\left(\theta_{z}\right) & \sin\left(\theta_{x}\right) \sin\left(\theta_{y}\right) \sin\left(\theta_{z}\right) + \cos\left(\theta_{x}\right) \cos\left(\theta_{z}\right) & -\cos\left(\theta_{y}\right) \sin\left(\theta_{x}\right) \\ -\cos\left(\theta_{x}\right) \cos\left(\theta_{z}\right) \sin\left(\theta_{y}\right) - \sin\left(\theta_{x}\right) \sin\left(\theta_{z}\right) & -\cos\left(\theta_{x}\right) \sin\left(\theta_{y}\right) \sin\left(\theta_{z}\right) + \cos\left(\theta_{z}\right) \sin\left(\theta_{x}\right) & \cos\left(\theta_{x}\right) \cos\left(\theta_{y}\right) \end{array}\right)$
$\displaystyle \left(\begin{array}{rrr} 0 & 2 \, \cos\left(\theta_{y}\right) \sin\left(\theta_{z}\right) & 0 \\ -2 \, \cos\left(\theta_{x}\right) \sin\left(\theta_{z}\right) & 2 \, \sin\left(\theta_{x}\right) \sin\left(\theta_{y}\right) \sin\left(\theta_{z}\right) & 0 \\ -2 \, \sin\left(\theta_{x}\right) \sin\left(\theta_{z}\right) & -2 \, \cos\left(\theta_{x}\right) \sin\left(\theta_{y}\right) \sin\left(\theta_{z}\right) & 0 \end{array}\right)$

show( r'Given $$x=y=z= \frac{\pi}{2}$$, ')
show( '$Rx Ry Rz = Rxyz =$', Rx.subs( x == pi/2 ), Ry.subs( y == pi/2 ), Rz.subs( z == pi/2 ), '$=$', (Rx * Ry * Rz).subs( x == pi/2, y == pi/2, z == pi/2 ) )


Given $x=y=z= \frac{\pi}{2}$,
$Rx Ry Rz = Rxyz =$ $\displaystyle \left(\begin{array}{rrr} 1 & 0 & 0 \\ 0 & 0 & -1 \\ 0 & 1 & 0 \end{array}\right)$ $\displaystyle \left(\begin{array}{rrr} 0 & 0 & 1 \\ 0 & 1 & 0 \\ -1 & 0 & 0 \end{array}\right)$ $\displaystyle \left(\begin{array}{rrr} 0 & -1 & 0 \\ 1 & 0 & 0 \\ 0 & 0 & 1 \end{array}\right)$ $=$ $\displaystyle \left(\begin{array}{rrr} 0 & 0 & 1 \\ 0 & -1 & 0 \\ 1 & 0 & 0 \end{array}\right)$

Vx = Matrix([ [1], [0], [0] ])
Vy = Matrix([ [0], [1], [0] ])
Vz = Matrix([ [0], [0], [1] ])

Rdev = Matrix([
[0, 0, 1],
[1, 0, 0],
[0, 1, 0]
])

Rprod = Matrix([
[0, 1,  0],
[1, 0,  0],
[0, 0, -1]
])

show( Rdev.det(), Rprod.det() )

$\displaystyle 1$ $\displaystyle 1$

def QuatFromMatrix( m ):
# wxyz, or rijk
q = vector([1.0, 0.0, 0.0, 0.0])

q[0] = 0.5 * sqrt(1.0 + m[0][0] - m[1][1] - m[2][2])
q[1] = (1.0 / 4.0 * q[0]) * (m[2][1] - m[1][2])
q[2] = (1.0 / 4.0 * q[0]) * (m[0][2] - m[2][0])
q[3] = (1.0 / 4.0 * q[0]) * (m[1][0] - m[0][1])

return q

show( QuatFromMatrix( Rprod ) )

$\displaystyle \left(0.707106781186548,\,0.000000000000000,\,0.000000000000000,\,0.000000000000000\right)$

show( (Rx * Ry * Rz).subs( x == 0, y == pi, z == 0 ) )
show( (Rz * Ry * Rx).subs( x == 0, y == pi, z == 0 ) )

$\displaystyle \left(\begin{array}{rrr} -1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & -1 \end{array}\right)$
$\displaystyle \left(\begin{array}{rrr} -1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & -1 \end{array}\right)$