Search code examples
pythonscipyrotationquaternions

Scipy Rotation Module produces differing results when re-entering a converted quaternion


So, imagine my situation. I need to handle rotation conversions in Python with no prior knowledge of the subject. I stumbled upon the scipy.spatial.transform.Rotation class and thought "Neat! That is all taken care of for me!". But as I did a few tests with the conversions I noticed something odd. When I take a random quaternion and convert it to a rotation matrix and back, the new quaternion is completely different from the original one, like beyond any plausible rounding errors.

Can anybody explain this difference?

Code:

from scipy.spatial.transform import Rotation as R

quat = [0.40, 0.27, 0.29, 0.14]

print("\nOriginal Quaternion")
print(quat)

matrix_from_quat = R.from_quat(quat).as_matrix()

print("\nMatrix from Quaternion")
print(matrix_from_quat)

quat_from_matrix = R.from_matrix(matrix_from_quat).as_quat()

print("\nQuaternion from Matrix")
print(quat_from_matrix)

Output:

Original Quaternion
[0.4, 0.27, 0.29, 0.14]

Matrix from Quaternion
[[ 0.06714201  0.40047534  0.91384433]
 [ 0.88294712 -0.45038622  0.13250149]
 [ 0.46464646  0.7979798  -0.38383838]]

Quaternion from Matrix
[0.68945025 0.46537892 0.49985143 0.24130759]

Solution

  • Convert your quaternion to a unit quaternion (by dividing by the norm), then it should work