{ } Raw JSON

bundles / scipy 1.18.0 / scipy / spatial / transform / _rotation / Rotation / apply

function

scipy.spatial.transform._rotation:Rotation.apply

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

Signature

def apply(self, vectors: ArrayLike, inverse: bool = False) → Array

Summary

Apply this rotation to a set of vectors.

Extended Summary

If the original frame rotates to the final frame by this rotation, then its application to a vector can be seen in two ways:

  • As a projection of vector components expressed in the final frame to the original frame.

  • As the physical rotation of a vector being glued to the original frame as it rotates. In this case the vector components are expressed in the original frame before and after the rotation.

In terms of rotation matrices, this application is the same as (self.as_matrix() @ vectors[..., np.newaxis])[..., 0]. For a single rotation, this is the same as vectors @ self.as_matrix().T.

Parameters

vectors : array_like, shape (..., 3)

Each vectors[..., :] represents a vector in 3D space. The shape of rotations and shape of vectors given must follow standard numpy broadcasting rules: either one of them equals unity or they both equal each other.

inverse : bool, optional

If True then the inverse of the rotation(s) is applied to the input vectors. Default is False.

Returns

rotated_vectors : ndarray, shape (..., 3)

Result of applying rotation on input vectors. Shape is determined according to numpy broadcasting rules. I.e., the result will have the shape np.broadcast_shapes(r.shape, v.shape[:-1]) + (3,)

Examples

from scipy.spatial.transform import Rotation as R
import numpy as np
✓
Single rotation applied on a single vector:
vector = np.array([1, 0, 0])
r = R.from_rotvec([0, 0, np.pi/2])
✓
r.as_matrix()
r.apply(vector)
✗
array([0., 1., 0.])
r.apply(vector).shape
✓
(3,)
Single rotation applied on multiple vectors:
vectors = np.array([
[1, 0, 0],
[1, 2, 3]])
r = R.from_rotvec([0, 0, np.pi/4])
✓
r.as_matrix()
r.apply(vectors)
✗
array([[ 0.70711,  0.70711,  0.     ],
       [-0.70711,  2.12132,  3.     ]])
r.apply(vectors).shape
✓
(2, 3)
Multiple rotations on a single vector:
r = R.from_rotvec([[0, 0, np.pi/4], [np.pi/2, 0, 0]])
vector = np.array([1,2,3])
✓
r.as_matrix()
r.apply(vector)
✗
array([[-0.70711,  2.12132,  3.     ],
       [ 1.     , -3.     ,  2.     ]])
r.apply(vector).shape
✓
(2, 3)
Multiple rotations on multiple vectors. Each rotation is applied on the corresponding vector:
r = R.from_euler('zxy', [
[0, 0, 90],
[45, 30, 60]], degrees=True)
vectors = [
[1, 2, 3],
[1, 0, -1]]
✓
r.apply(vectors)
✗
array([[ 3.     ,  2.     , -1.     ],
       [-0.09026,  1.11237, -0.86861]])
r.apply(vectors).shape
✓
(2, 3)
Broadcasting rules apply:
r = R.from_rotvec(np.tile([0, 0, np.pi/4], (5, 1, 4, 1)))
vectors = np.ones((3, 4, 3))
r.shape, vectors.shape
r.apply(vectors).shape
✓
(5, 3, 4, 3)
It is also possible to apply the inverse rotation:
r = R.from_euler('zxy', [
[0, 0, 90],
[45, 30, 60]], degrees=True)
vectors = [
[1, 2, 3],
[1, 0, -1]]
✓
r.apply(vectors, inverse=True)
✗
array([[-3.     ,  2.     ,  1.     ],
       [ 1.09534, -0.83652,  0.31699]])

Aliases

  • scipy.spatial.transform.Rotation.apply

Referenced by