scipy.spatial.transform.Rotation.as_quat#

Rotation.as_quat(self, canonical=False)#

Represent as quaternions.

Active rotations in 3 dimensions can be represented using unit norm quaternions [1]. The mapping from quaternions to rotations is two-to-one, i.e. quaternions q and -q, where -q simply reverses the sign of each component, represent the same spatial rotation. The returned value is in scalar-last (x, y, z, w) format.

Parameters:
canonicalbool, default False

Whether to map the redundant double cover of rotation space to a unique “canonical” single cover. If True, then the quaternion is chosen from {q, -q} such that the w term is positive. If the w term is 0, then the quaternion is chosen such that the first nonzero term of the x, y, and z terms is positive.

Returns:
quatnumpy.ndarray, shape (4,) or (N, 4)

Shape depends on shape of inputs used for initialization.

References

Examples

>>> from scipy.spatial.transform import Rotation as R
>>> import numpy as np

Represent a single rotation:

>>> r = R.from_matrix([[0, -1, 0],
...                    [1, 0, 0],
...                    [0, 0, 1]])
>>> r.as_quat()
array([0.        , 0.        , 0.70710678, 0.70710678])
>>> r.as_quat().shape
(4,)

Represent a stack with a single rotation:

>>> r = R.from_quat([[0, 0, 0, 1]])
>>> r.as_quat().shape
(1, 4)

Represent multiple rotations in a single object:

>>> r = R.from_rotvec([[np.pi, 0, 0], [0, 0, np.pi/2]])
>>> r.as_quat().shape
(2, 4)

Quaternions can be mapped from a redundant double cover of the rotation space to a canonical representation with a positive w term.

>>> r = R.from_quat([0, 0, 0, -1])
>>> r.as_quat()
array([0. , 0. , 0. , -1.])
>>> r.as_quat(canonical=True)
array([0. , 0. , 0. , 1.])