spacepy.coordinates.quaternionFromMatrix

spacepy.coordinates.quaternionFromMatrix(matrix, scalarPos='last')[source]

Given an input rotation matrix, return the equivalent quaternion

The output has one fewer axis than the input (the last axis) and the shape is otherwise unchanged, allowing multi-dimensional matrix input.

Parameters:
matrixarray_like

input rotation matrix or array of matrices

Returns:
outarray_like

Quaternions representing the same rotation as the input rotation matrices.

Other Parameters:
scalarPosstr

Location of the scalar component of the output quaternion, either ‘last’ (default) or ‘first’.

Raises:
NotImplementedError

for invalid values of scalarPos

ValueError

for inputs which are obviously not valid 3D rotation matrices or arrays thereof: if the size doesn’t end in (3, 3), if the matrix is not orthogonal, or not a proper rotation.

Notes

New in version 0.2.2.

No attempt is made to resolve the sign ambiguity; in particular, conversions of very similar matrices may result in equivalent quaternions with the opposite sign. This may have implications for interpolating a sequence of quaternions.

The conversion of a rotation matrix to a quaternion suffers from some of the same disadvantages inherent to rotation matrices, such as potential numerical instabilities. Working in quaternion space as much as possible is recommended.

There are several algorithms; the most well-known algorithm for this conversion is Shepperd’s [1], although the many “rediscoveries” indicate it is not sufficiently well-known. This function uses the method of Bar-Itzhack [2] (version 3), which should be resistant to small errors in the rotation matrix. As a result, the input checking is quite coarse and will likely accept many matrices that do not represent valid rotations.

Also potentially of interest, although not implemented here, is Sarabandi and Thomas [3].

References

Examples

>>> import spacepy.coordinates
>>> spacepy.coordinates.quaternionFromMatrix(
...     [[ 0.,  0.,  1.],
...      [ 1.,  0.,  0.],
...      [ 0.,  1.,  0.]])
array([0.5, 0.5, 0.5, 0.5])