Search code examples
pythonvalidationscipyquaternionswolframalpha

Disagreement between SciPy quaternion and Wolfram


I'm calculating rotation quaternions from Euler angles in Python using SciPy and trying to validate against an external source (Wolfram Alpha).

This Scipy code gives me one answer:

from scipy.spatial.transform import Rotation as R

rot = R.from_euler('xyz', [30,45,60], degrees=1)
quat = rot.as_quat()
print(quat[3], quat[0], quat[1], quat[2]) # w, x, y, z

(w,x,y,z) = 0.8223, 0.0222, 0.4396, 0.3604

while Wolfram Alpha gives a different answer

(w,x,y,z) = 0.723, 0.392, 0.201, 0.532

Wolfram Alpha solution

Why the difference? Is it a difference in paradigm for how the object is rotated (ex. extrinsic rotations vs. intrinsic rotations)?


Solution

  • Indeed, when you switch from small "xyz", which denotes extrinsic rotations, to capital "XYZ", which denotes intrinsic rotations in from_euler(), the results will match those of WolframAlpha:

    from scipy.spatial.transform import Rotation
    import numpy as np
    np.set_printoptions(precision=3)
    
    rot = Rotation.from_euler("XYZ", [30, 45, 60], degrees=True)
    
    print(rot.as_matrix().T)
    # [[ 0.354  0.927  0.127]
    #  [-0.612  0.127  0.78 ]
    #  [ 0.707 -0.354  0.612]]
    
    print(np.roll(rot.as_quat(), shift=1))
    # [0.723 0.392 0.201 0.532]