{ } Raw JSON

bundles / scipy latest / scipy / spatial / transform / _rotation / Rotation / from_quat

staticmethod

scipy.spatial.transform._rotation:Rotation.from_quat

source: /scipy/spatial/transform/_rotation.py :374

Signature

staticmethod def   from_quat ( quat : ArrayLike * scalar_first : bool = False )  →  Rotation

Summary

Initialize from quaternions.

Extended Summary

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.

Notes

Array API Standard Support

from_quat has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.

====================  ====================  ====================
Library               CPU                   GPU
====================  ====================  ====================
NumPy                 ✅                     n/a                 
CuPy                  n/a                   ✅                   
PyTorch               ✅                     ✅                   
JAX                   ✅                     ✅                   
Dask                  ⛔                     n/a                 
====================  ====================  ====================

See dev-arrayapi for more information.

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()
r = R.from_quat([1, 0, 0, 0], scalar_first=True)
r.as_matrix()
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()
r.as_quat().shape
It is also possible to have a stack of a single rotation:
r = R.from_quat([[0, 0, 0, 1]])
r.as_quat()
r.as_quat().shape
Quaternions are normalized before initialization.
r = R.from_quat([0, 0, 1, 1])
r.as_quat()

Aliases

  • scipy.spatial.transform.Rotation.from_quat

Referenced by

This package