SciPy

scipy.spatial.transform.Rotation.from_matrix

classmethod Rotation.from_matrix(matrix)[source]

Initialize from rotation matrix.

Rotations in 3 dimensions can be represented with 3 x 3 proper orthogonal matrices [1]. If the input is not proper orthogonal, an approximation is created using the method described in [2].

Parameters
matrixarray_like, shape (N, 3, 3) or (3, 3)

A single matrix or a stack of matrices, where matrix[i] is the i-th matrix.

Returns
rotationRotation instance

Object containing the rotations represented by the rotation matrices.

References

1

https://en.wikipedia.org/wiki/Rotation_matrix#In_three_dimensions

2

F. Landis Markley, “Unit Quaternion from Rotation Matrix”, Journal of guidance, control, and dynamics vol. 31.2, pp. 440-442, 2008.

Examples

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

Initialize a single rotation:

>>> r = R.from_matrix([
... [0, -1, 0],
... [1, 0, 0],
... [0, 0, 1]])
>>> r.as_matrix().shape
(3, 3)

Initialize multiple rotations in a single object:

>>> r = R.from_matrix([
... [
...     [0, -1, 0],
...     [1, 0, 0],
...     [0, 0, 1],
... ],
... [
...     [1, 0, 0],
...     [0, 0, -1],
...     [0, 1, 0],
... ]])
>>> r.as_matrix().shape
(2, 3, 3)

If input matrices are not special orthogonal (orthogonal with determinant equal to +1), then a special orthogonal estimate is stored:

>>> a = np.array([
... [0, -0.5, 0],
... [0.5, 0, 0],
... [0, 0, 0.5]])
>>> np.linalg.det(a)
0.12500000000000003
>>> r = R.from_matrix(a)
>>> matrix = r.as_matrix()
>>> matrix
array([[-0.38461538, -0.92307692,  0.        ],
       [ 0.92307692, -0.38461538,  0.        ],
       [ 0.        ,  0.        ,  1.        ]])
>>> np.linalg.det(matrix)
1.0000000000000002

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

>>> r = R.from_matrix([[
... [0, -1, 0],
... [1, 0, 0],
... [0, 0, 1]]])
>>> r.as_matrix()
array([[[ 0., -1.,  0.],
        [ 1.,  0.,  0.],
        [ 0.,  0.,  1.]]])
>>> r.as_matrix().shape
(1, 3, 3)