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

# from_matrix

static RigidTransform.from_matrix(matrix)

  Initialize from a 4x4 transformation matrix.

  Parameters:
    matrix array_like, shape (…, 4, 4)

      Transformation matrices. Each matrix[…, :, :] represents a 4x4 transformation matrix.

  Returns:
    transform `RigidTransform` instance

  Notes

  4x4 rigid transformation matrices are of the form:

      [       tx]
      [   R   ty]
      [       tz]
      [ 0 0 0  1]

  where `R` is a 3x3 rotation matrix and `t = [tx, ty, tz]` is a 3x1 translation vector. As rotation matrices must be proper orthogonal, the rotation component is orthonormalized using singular value decomposition before initialization.

  Array API Standard Support

  `from_matrix` 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 Support for the array API standard for more information.

  Examples

      >>> from scipy.spatial.transform import RigidTransform as Tf
      >>> import numpy as np

  Creating a transform from a single matrix:

      >>> m = np.array([[0, 1, 0, 2],
      ...               [0, 0, 1, 3],
      ...               [1, 0, 0, 4],
      ...               [0, 0, 0, 1]])
      >>> tf = Tf.from_matrix(m)
      >>> tf.as_matrix()
      array([[0., 1., 0., 2.],
             [0., 0., 1., 3.],
             [1., 0., 0., 4.],
             [0., 0., 0., 1.]])
      >>> tf.single
      True

  Creating a transform from an N-dimensional array of matrices:

      >>> m = np.tile(np.eye(4), (2, 5, 1, 1))  # Shape (2, 5, 4, 4)
      >>> tf = Tf.from_matrix(m)
      >>> tf.shape
      (2, 5)
      >>> tf.single
      False
      >>> len(tf)
      2

  Matrices with a rotation component that is not proper orthogonal are orthogonalized using singular value decomposition before initialization:

      >>> tf = Tf.from_matrix(np.diag([2, 2, 2, 1]))
      >>> tf.as_matrix()
      array([[1., 0., 0., 0.],
             [0., 1., 0., 0.],
             [0., 0., 1., 0.],
             [0., 0., 0., 1.]])