Skip to main content
`Ctrl`+`K`
scipy.spatial.transform.Rotation.

# from_quat

static Rotation.from_quat(quat, *, scalar_first=False)

  Initialize from quaternions.

  Rotations in 3 dimensions can be represented using unit norm quaternions [1].

  The 4 components of a quaternion are divided into a scalar part `w` and a vector part `(x, y, z)` and can be expressed from the angle `theta` and the axis `n` of a rotation as follows:

      w = cos(theta / 2)
      x = sin(theta / 2) * n_x
      y = sin(theta / 2) * n_y
      z = sin(theta / 2) * n_z

  There are 2 conventions to order the components in a quaternion:

  - scalar-first order – `(w, x, y, z)`

  - scalar-last order – `(x, y, z, w)`

  The choice is controlled by scalar_first argument. By default, it is False and the scalar-last order is assumed.

  Advanced users may be interested in the “double cover” of 3D space by the quaternion representation [2]. As of version 1.11.0, the following subset (and only this subset) of operations on a `Rotation` `r` corresponding to a quaternion `q` are guaranteed to preserve the double cover property: `r = Rotation.from_quat(q)`, `r.as_quat(canonical=False)`, `r.inv()`, and composition using the `*` operator such as `r*r`.

  Parameters:
    quat array_like, shape (…, 4)

      Each row is a (possibly non-unit norm) quaternion representing an active rotation. Each quaternion will be normalized to unit norm.

    scalar_first bool, optional

      Whether the scalar component goes first or last. Default is False, i.e. the scalar-last order is assumed.

  Returns:
    rotation `Rotation` instance

      Object containing the rotations represented by input quaternions.

  References

  [1]

  https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation

  [2]

  Hanson, Andrew J. “Visualizing quaternions.” Morgan Kaufmann Publishers Inc., San Francisco, CA. 2006.

  Examples

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

  A rotation can be initialized from a quaternion with the scalar-last (default) or scalar-first component order as shown below:

      >>> r = R.from_quat([0, 0, 0, 1])
      >>> r.as_matrix()
      array([[1., 0., 0.],
             [0., 1., 0.],
             [0., 0., 1.]])
      >>> r = R.from_quat([1, 0, 0, 0], scalar_first=True)
      >>> r.as_matrix()
      array([[1., 0., 0.],
             [0., 1., 0.],
             [0., 0., 1.]])

  It is possible to initialize multiple rotations in a single object by passing an N-dimensional array:

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

  It is also possible to have a stack of a single rotation:

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

  Quaternions are normalized before initialization.

      >>> r = R.from_quat([0, 0, 1, 1])
      >>> r.as_quat()
      array([0.        , 0.        , 0.70710678, 0.70710678])