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

# from_matrix

classmethod Rotation.from_matrix(cls, matrix)

  Initialize from rotation matrix.

  Rotations in 3 dimensions can be represented with 3 x 3 orthogonal matrices [1]. If the input is not orthogonal, an approximation is created by orthogonalizing the input matrix using the method described in [2], and then converting the orthogonal rotation matrices to quaternions using the algorithm described in [3]. Matrices must be right-handed.

  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:
    rotation `Rotation` instance

      Object containing the rotations represented by the rotation matrices.

  Notes

  This function was called from_dcm before.

  Added in version 1.4.0.

  References

  [1]

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

  [2]

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

  [3]

  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
      >>> import numpy as np

  Initialize a single rotation:

      >>> r = R.from_matrix([
      ... [0, -1, 0],
      ... [1, 0, 0],
      ... [0, 0, 1]])
      >>> r.single
      True
      >>> 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)
      >>> r.single
      False
      >>> len(r)
      2

  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.125
      >>> r = R.from_matrix(a)
      >>> matrix = r.as_matrix()
      >>> matrix
      array([[ 0., -1.,  0.],
             [ 1.,  0.,  0.],
             [ 0.,  0.,  1.]])
      >>> np.linalg.det(matrix)
      1.0

  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)