package scipy

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type
type tag = [
  1. | `Rotation
]
type t = [ `Object | `Rotation ] Obj.t
val of_pyobject : Py.Object.t -> t
val to_pyobject : [> tag ] Obj.t -> Py.Object.t
val create : ?normalize:Py.Object.t -> ?copy:Py.Object.t -> quat:Py.Object.t -> unit -> t

Rotation in 3 dimensions.

This class provides an interface to initialize from and represent rotations with:

  • Quaternions
  • Rotation Matrices
  • Rotation Vectors
  • Euler Angles

The following operations on rotations are supported:

  • Application on vectors
  • Rotation Composition
  • Rotation Inversion
  • Rotation Indexing

Indexing within a rotation is supported since multiple rotation transforms can be stored within a single `Rotation` instance.

To create `Rotation` objects use ``from_...`` methods (see examples below). ``Rotation(...)`` is not supposed to be instantiated directly.

Methods ------- __len__ from_quat from_matrix from_rotvec from_euler as_quat as_matrix as_rotvec as_euler apply __mul__ inv magnitude mean reduce create_group __getitem__ identity random align_vectors

See Also -------- Slerp

Notes ----- .. versionadded: 1.2.0

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

A `Rotation` instance can be initialized in any of the above formats and converted to any of the others. The underlying object is independent of the representation used for initialization.

Consider a counter-clockwise rotation of 90 degrees about the z-axis. This corresponds to the following quaternion (in scalar-last format):

>>> r = R.from_quat(0, 0, np.sin(np.pi/4), np.cos(np.pi/4))

The rotation can be expressed in any of the other formats:

>>> r.as_matrix() array([ 2.22044605e-16, -1.00000000e+00, 0.00000000e+00], [ 1.00000000e+00, 2.22044605e-16, 0.00000000e+00], [ 0.00000000e+00, 0.00000000e+00, 1.00000000e+00]) >>> r.as_rotvec() array(0. , 0. , 1.57079633) >>> r.as_euler('zyx', degrees=True) array(90., 0., 0.)

The same rotation can be initialized using a rotation matrix:

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

Representation in other formats:

>>> r.as_quat() array(0. , 0. , 0.70710678, 0.70710678) >>> r.as_rotvec() array(0. , 0. , 1.57079633) >>> r.as_euler('zyx', degrees=True) array(90., 0., 0.)

The rotation vector corresponding to this rotation is given by:

>>> r = R.from_rotvec(np.pi/2 * np.array(0, 0, 1))

Representation in other formats:

>>> r.as_quat() array(0. , 0. , 0.70710678, 0.70710678) >>> r.as_matrix() array([ 2.22044605e-16, -1.00000000e+00, 0.00000000e+00], [ 1.00000000e+00, 2.22044605e-16, 0.00000000e+00], [ 0.00000000e+00, 0.00000000e+00, 1.00000000e+00]) >>> r.as_euler('zyx', degrees=True) array(90., 0., 0.)

The ``from_euler`` method is quite flexible in the range of input formats it supports. Here we initialize a single rotation about a single axis:

>>> r = R.from_euler('z', 90, degrees=True)

Again, the object is representation independent and can be converted to any other format:

>>> r.as_quat() array(0. , 0. , 0.70710678, 0.70710678) >>> r.as_matrix() array([ 2.22044605e-16, -1.00000000e+00, 0.00000000e+00], [ 1.00000000e+00, 2.22044605e-16, 0.00000000e+00], [ 0.00000000e+00, 0.00000000e+00, 1.00000000e+00]) >>> r.as_rotvec() array(0. , 0. , 1.57079633)

It is also possible to initialize multiple rotations in a single instance using any of the `from_...` functions. Here we initialize a stack of 3 rotations using the ``from_euler`` method:

>>> r = R.from_euler('zyx', ... [90, 0, 0], ... [0, 45, 0], ... [45, 60, 30], degrees=True)

The other representations also now return a stack of 3 rotations. For example:

>>> r.as_quat() array([0. , 0. , 0.70710678, 0.70710678], [0. , 0.38268343, 0. , 0.92387953], [0.39190384, 0.36042341, 0.43967974, 0.72331741])

Applying the above rotations onto a vector:

>>> v = 1, 2, 3 >>> r.apply(v) array([-2. , 1. , 3. ], [ 2.82842712, 2. , 1.41421356], [ 2.24452282, 0.78093109, 2.89002836])

A `Rotation` instance can be indexed and sliced as if it were a single 1D array or list:

>>> r.as_quat() array([0. , 0. , 0.70710678, 0.70710678], [0. , 0.38268343, 0. , 0.92387953], [0.39190384, 0.36042341, 0.43967974, 0.72331741]) >>> p = r0 >>> p.as_matrix() array([ 2.22044605e-16, -1.00000000e+00, 0.00000000e+00], [ 1.00000000e+00, 2.22044605e-16, 0.00000000e+00], [ 0.00000000e+00, 0.00000000e+00, 1.00000000e+00]) >>> q = r1:3 >>> q.as_quat() array([0. , 0.38268343, 0. , 0.92387953], [0.39190384, 0.36042341, 0.43967974, 0.72331741])

Multiple rotations can be composed using the ``*`` operator:

>>> r1 = R.from_euler('z', 90, degrees=True) >>> r2 = R.from_rotvec(np.pi/4, 0, 0) >>> v = 1, 2, 3 >>> r2.apply(r1.apply(v)) array(-2. , -1.41421356, 2.82842712) >>> r3 = r2 * r1 # Note the order >>> r3.apply(v) array(-2. , -1.41421356, 2.82842712)

Finally, it is also possible to invert rotations:

>>> r1 = R.from_euler('z', 90, 45, degrees=True) >>> r2 = r1.inv() >>> r2.as_euler('zyx', degrees=True) array([-90., 0., 0.], [-45., 0., 0.])

These examples serve as an overview into the `Rotation` class and highlight major functionalities. For more thorough examples of the range of input and output formats supported, consult the individual method's examples.

val __getitem__ : indexer:[ `Slice of Np.Wrap_utils.Slice.t | `PyObject of Py.Object.t ] -> [> tag ] Obj.t -> Py.Object.t

Extract rotation(s) at given index(es) from object.

Create a new `Rotation` instance containing a subset of rotations stored in this object.

Parameters ---------- indexer : index, slice, or index array Specifies which rotation(s) to extract. A single indexer must be specified, i.e. as if indexing a 1 dimensional array or list.

Returns ------- rotation : `Rotation` instance Contains

  • a single rotation, if `indexer` is a single index
  • a stack of rotation(s), if `indexer` is a slice, or and index array.

Examples -------- >>> from scipy.spatial.transform import Rotation as R >>> r = R.from_quat( ... [1, 1, 0, 0], ... [0, 1, 0, 1], ... [1, 1, -1, 0]) >>> r.as_quat() array([ 0.70710678, 0.70710678, 0. , 0. ], [ 0. , 0.70710678, 0. , 0.70710678], [ 0.57735027, 0.57735027, -0.57735027, 0. ])

Indexing using a single index:

>>> p = r0 >>> p.as_quat() array(0.70710678, 0.70710678, 0. , 0. )

Array slicing:

>>> q = r1:3 >>> q.as_quat() array([ 0. , 0.70710678, 0. , 0.70710678], [ 0.57735027, 0.57735027, -0.57735027, 0. ])

val align_vectors : ?weights:[> `Ndarray ] Np.Obj.t -> ?return_sensitivity:bool -> a:[> `Ndarray ] Np.Obj.t -> b:[> `Ndarray ] Np.Obj.t -> [> tag ] Obj.t -> Py.Object.t * float * [ `ArrayLike | `Ndarray | `Object ] Np.Obj.t

Estimate a rotation to optimally align two sets of vectors.

Find a rotation between frames A and B which best aligns a set of vectors `a` and `b` observed in these frames. The following loss function is minimized to solve for the rotation matrix :math:`C`:

.. math::

L(C) = \frac

\sum_= 1^n w_i \lVert \mathbfa_i - C \mathbf_i \rVert^2 ,

where :math:`w_i`'s are the `weights` corresponding to each vector.

The rotation is estimated with Kabsch algorithm 1_.

Parameters ---------- a : array_like, shape (N, 3) Vector components observed in initial frame A. Each row of `a` denotes a vector. b : array_like, shape (N, 3) Vector components observed in another frame B. Each row of `b` denotes a vector. weights : array_like shape (N,), optional Weights describing the relative importance of the vector observations. If None (default), then all values in `weights` are assumed to be 1. return_sensitivity : bool, optional Whether to return the sensitivity matrix. See Notes for details. Default is False.

Returns ------- estimated_rotation : `Rotation` instance Best estimate of the rotation that transforms `b` to `a`. rmsd : float Root mean square distance (weighted) between the given set of vectors after alignment. It is equal to ``sqrt(2 * minimum_loss)``, where ``minimum_loss`` is the loss function evaluated for the found optimal rotation. sensitivity_matrix : ndarray, shape (3, 3) Sensitivity matrix of the estimated rotation estimate as explained in Notes. Returned only when `return_sensitivity` is True.

Notes ----- This method can also compute the sensitivity of the estimated rotation to small perturbations of the vector measurements. Specifically we consider the rotation estimate error as a small rotation vector of frame A. The sensitivity matrix is proportional to the covariance of this rotation vector assuming that the vectors in `a` was measured with errors significantly less than their lengths. To get the true covariance matrix, the returned sensitivity matrix must be multiplied by harmonic mean 3_ of variance in each observation. Note that `weights` are supposed to be inversely proportional to the observation variances to get consistent results. For example, if all vectors are measured with the same accuracy of 0.01 (`weights` must be all equal), then you should multiple the sensitivity matrix by 0.01**2 to get the covariance.

Refer to 2_ for more rigorous discussion of the covariance estimation.

References ---------- .. 1 https://en.wikipedia.org/wiki/Kabsch_algorithm .. 2 F. Landis Markley, 'Attitude determination using vector observations: a fast optimal matrix algorithm', Journal of Astronautical Sciences, Vol. 41, No.2, 1993, pp. 261-280. .. 3 https://en.wikipedia.org/wiki/Harmonic_mean

val apply : ?inverse:bool -> vectors:[> `Ndarray ] Np.Obj.t -> [> tag ] Obj.t -> [ `ArrayLike | `Ndarray | `Object ] Np.Obj.t

Apply this rotation to a set of vectors.

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 matricies, this application is the same as ``self.as_matrix().dot(vectors)``.

Parameters ---------- vectors : array_like, shape (3,) or (N, 3) Each `vectorsi` represents a vector in 3D space. A single vector can either be specified with shape `(3, )` or `(1, 3)`. The number of rotations and number of vectors given must follow standard numpy broadcasting rules: either one of them equals unity or they both equal each other. inverse : boolean, 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,) or (N, 3) Result of applying rotation on input vectors. Shape depends on the following cases:

  • If object contains a single rotation (as opposed to a stack with a single rotation) and a single vector is specified with shape ``(3,)``, then `rotated_vectors` has shape ``(3,)``.
  • In all other cases, `rotated_vectors` has shape ``(N, 3)``, where ``N`` is either the number of rotations or vectors.

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

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() array([ 2.22044605e-16, -1.00000000e+00, 0.00000000e+00], [ 1.00000000e+00, 2.22044605e-16, 0.00000000e+00], [ 0.00000000e+00, 0.00000000e+00, 1.00000000e+00]) >>> r.apply(vector) array(2.22044605e-16, 1.00000000e+00, 0.00000000e+00) >>> 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() array([ 0.70710678, -0.70710678, 0. ], [ 0.70710678, 0.70710678, 0. ], [ 0. , 0. , 1. ]) >>> r.apply(vectors) array([ 0.70710678, 0.70710678, 0. ], [-0.70710678, 2.12132034, 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() array([[ 7.07106781e-01, -7.07106781e-01, 0.00000000e+00], [ 7.07106781e-01, 7.07106781e-01, 0.00000000e+00], [ 0.00000000e+00, 0.00000000e+00, 1.00000000e+00]], [[ 1.00000000e+00, 0.00000000e+00, 0.00000000e+00], [ 0.00000000e+00, 2.22044605e-16, -1.00000000e+00], [ 0.00000000e+00, 1.00000000e+00, 2.22044605e-16]]) >>> r.apply(vector) array([-0.70710678, 2.12132034, 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.09026039, 1.11237244, -0.86860844]) >>> r.apply(vectors).shape (2, 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.09533535, -0.8365163 , 0.3169873 ])

val as_euler : ?degrees:bool -> seq:[ `S of string | `Length_3 of Py.Object.t ] -> [> tag ] Obj.t -> [ `ArrayLike | `Ndarray | `Object ] Np.Obj.t

Represent as Euler angles.

Any orientation can be expressed as a composition of 3 elementary rotations. Once the axis sequence has been chosen, Euler angles define the angle of rotation around each respective axis 1_.

The algorithm from 2_ has been used to calculate Euler angles for the rotation about a given sequence of axes.

Euler angles suffer from the problem of gimbal lock 3_, where the representation loses a degree of freedom and it is not possible to determine the first and third angles uniquely. In this case, a warning is raised, and the third angle is set to zero. Note however that the returned angles still represent the correct rotation.

Parameters ---------- seq : string, length 3 3 characters belonging to the set 'X', 'Y', 'Z' for intrinsic rotations, or 'x', 'y', 'z' for extrinsic rotations 1_. Adjacent axes cannot be the same. Extrinsic and intrinsic rotations cannot be mixed in one function call. degrees : boolean, optional Returned angles are in degrees if this flag is True, else they are in radians. Default is False.

Returns ------- angles : ndarray, shape (3,) or (N, 3) Shape depends on shape of inputs used to initialize object. The returned angles are in the range:

  • First angle belongs to -180, 180 degrees (both inclusive)
  • Third angle belongs to -180, 180 degrees (both inclusive)
  • Second angle belongs to:
  • -90, 90 degrees if all axes are different (like xyz)
  • 0, 180 degrees if first and third axes are the same (like zxz)

References ---------- .. 1 https://en.wikipedia.org/wiki/Euler_angles#Definition_by_intrinsic_rotations .. 2 Malcolm D. Shuster, F. Landis Markley, 'General formula for extraction the Euler angles', Journal of guidance, control, and dynamics, vol. 29.1, pp. 215-221. 2006 .. 3 https://en.wikipedia.org/wiki/Gimbal_lock#In_applied_mathematics

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

Represent a single rotation:

>>> r = R.from_rotvec(0, 0, np.pi/2) >>> r.as_euler('zxy', degrees=True) array(90., 0., 0.) >>> r.as_euler('zxy', degrees=True).shape (3,)

Represent a stack of single rotation:

>>> r = R.from_rotvec([0, 0, np.pi/2]) >>> r.as_euler('zxy', degrees=True) array([90., 0., 0.]) >>> r.as_euler('zxy', degrees=True).shape (1, 3)

Represent multiple rotations in a single object:

>>> r = R.from_rotvec( ... [0, 0, np.pi/2], ... [0, -np.pi/3, 0], ... [np.pi/4, 0, 0]) >>> r.as_euler('zxy', degrees=True) array([ 90., 0., 0.], [ 0., 0., -60.], [ 0., 45., 0.]) >>> r.as_euler('zxy', degrees=True).shape (3, 3)

val as_matrix : [> tag ] Obj.t -> [ `ArrayLike | `Ndarray | `Object ] Np.Obj.t

Represent as rotation matrix.

3D rotations can be represented using rotation matrices, which are 3 x 3 real orthogonal matrices with determinant equal to +1 1_.

Returns ------- matrix : ndarray, shape (3, 3) or (N, 3, 3) Shape depends on shape of inputs used for initialization.

References ---------- .. 1 https://en.wikipedia.org/wiki/Rotation_matrix#In_three_dimensions

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

Represent a single rotation:

>>> r = R.from_rotvec(0, 0, np.pi/2) >>> r.as_matrix() array([ 2.22044605e-16, -1.00000000e+00, 0.00000000e+00], [ 1.00000000e+00, 2.22044605e-16, 0.00000000e+00], [ 0.00000000e+00, 0.00000000e+00, 1.00000000e+00]) >>> r.as_matrix().shape (3, 3)

Represent a stack with a single rotation:

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

Represent multiple rotations:

>>> r = R.from_rotvec([np.pi/2, 0, 0], [0, 0, np.pi/2]) >>> r.as_matrix() array([[ 1.00000000e+00, 0.00000000e+00, 0.00000000e+00], [ 0.00000000e+00, 2.22044605e-16, -1.00000000e+00], [ 0.00000000e+00, 1.00000000e+00, 2.22044605e-16]], [[ 2.22044605e-16, -1.00000000e+00, 0.00000000e+00], [ 1.00000000e+00, 2.22044605e-16, 0.00000000e+00], [ 0.00000000e+00, 0.00000000e+00, 1.00000000e+00]]) >>> r.as_matrix().shape (2, 3, 3)

Notes ----- This function was called as_dcm before.

.. versionadded:: 1.4.0

val as_quat : [> tag ] Obj.t -> Py.Object.t

Represent as quaternions.

Rotations in 3 dimensions can be represented using unit norm quaternions 1_. The mapping from quaternions to rotations is two-to-one, i.e. quaternions ``q`` and ``-q``, where ``-q`` simply reverses the sign of each component, represent the same spatial rotation.

Returns ------- quat : `numpy.ndarray`, shape (4,) or (N, 4) Shape depends on shape of inputs used for initialization.

References ---------- .. 1 https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation

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

Represent a single rotation:

>>> r = R.from_matrix([0, -1, 0], ... [1, 0, 0], ... [0, 0, 1]) >>> r.as_quat() array(0. , 0. , 0.70710678, 0.70710678) >>> r.as_quat().shape (4,)

Represent a stack with a single rotation:

>>> r = R.from_quat([0, 0, 0, 1]) >>> r.as_quat().shape (1, 4)

Represent multiple rotations in a single object:

>>> r = R.from_rotvec([np.pi, 0, 0], [0, 0, np.pi/2]) >>> r.as_quat().shape (2, 4)

val as_rotvec : [> tag ] Obj.t -> [ `ArrayLike | `Ndarray | `Object ] Np.Obj.t

Represent as rotation vectors.

A rotation vector is a 3 dimensional vector which is co-directional to the axis of rotation and whose norm gives the angle of rotation (in radians) 1_.

Returns ------- rotvec : ndarray, shape (3,) or (N, 3) Shape depends on shape of inputs used for initialization.

References ---------- .. 1 https://en.wikipedia.org/wiki/Axis%E2%80%93angle_representation#Rotation_vector

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

Represent a single rotation:

>>> r = R.from_euler('z', 90, degrees=True) >>> r.as_rotvec() array(0. , 0. , 1.57079633) >>> r.as_rotvec().shape (3,)

Represent a stack with a single rotation:

>>> r = R.from_quat([0, 0, 1, 1]) >>> r.as_rotvec() array([0. , 0. , 1.57079633]) >>> r.as_rotvec().shape (1, 3)

Represent multiple rotations in a single object:

>>> r = R.from_quat([0, 0, 1, 1], [1, 1, 0, 1]) >>> r.as_rotvec() array([0. , 0. , 1.57079633], [1.35102172, 1.35102172, 0. ]) >>> r.as_rotvec().shape (2, 3)

val create_group : ?axis:int -> group:string -> [> tag ] Obj.t -> Py.Object.t

Create a 3D rotation group.

Parameters ---------- group : string The name of the group. Must be one of 'I', 'O', 'T', 'Dn', 'Cn', where `n` is a positive integer. The groups are:

* I: Icosahedral group * O: Octahedral group * T: Tetrahedral group * D: Dicyclic group * C: Cyclic group

axis : integer The cyclic rotation axis. Must be one of 'X', 'Y', 'Z' (or lowercase). Default is 'Z'. Ignored for groups 'I', 'O', and 'T'.

Returns ------- rotation : `Rotation` instance Object containing the elements of the rotation group.

Notes ----- This method generates rotation groups only. The full 3-dimensional point groups PointGroups_ also contain reflections.

References ---------- .. PointGroups `Point groups <https://en.wikipedia.org/wiki/Point_groups_in_three_dimensions>`_ on Wikipedia.

val from_dcm : ?kwds:(string * Py.Object.t) list -> Py.Object.t list -> [> tag ] Obj.t -> Py.Object.t

`from_dcm` is deprecated! from_dcm is renamed to from_matrix in scipy 1.4.0 and will be removed in scipy 1.6.0

val from_euler : ?degrees:bool -> seq:string -> angles:[ `F of float | `Ndarray of [> `Ndarray ] Np.Obj.t ] -> [> tag ] Obj.t -> Py.Object.t

Initialize from Euler angles.

Rotations in 3-D can be represented by a sequence of 3 rotations around a sequence of axes. In theory, any three axes spanning the 3-D Euclidean space are enough. In practice, the axes of rotation are chosen to be the basis vectors.

The three rotations can either be in a global frame of reference (extrinsic) or in a body centred frame of reference (intrinsic), which is attached to, and moves with, the object under rotation 1_.

Parameters ---------- seq : string Specifies sequence of axes for rotations. Up to 3 characters belonging to the set 'X', 'Y', 'Z' for intrinsic rotations, or 'x', 'y', 'z' for extrinsic rotations. Extrinsic and intrinsic rotations cannot be mixed in one function call. angles : float or array_like, shape (N,) or (N, 1 or 2 or 3) Euler angles specified in radians (`degrees` is False) or degrees (`degrees` is True). For a single character `seq`, `angles` can be:

  • a single value
  • array_like with shape (N,), where each `anglei` corresponds to a single rotation
  • array_like with shape (N, 1), where each `anglei, 0` corresponds to a single rotation

For 2- and 3-character wide `seq`, `angles` can be:

  • array_like with shape (W,) where `W` is the width of `seq`, which corresponds to a single rotation with `W` axes
  • array_like with shape (N, W) where each `anglei` corresponds to a sequence of Euler angles describing a single rotation

degrees : bool, optional If True, then the given angles are assumed to be in degrees. Default is False.

Returns ------- rotation : `Rotation` instance Object containing the rotation represented by the sequence of rotations around given axes with given angles.

References ---------- .. 1 https://en.wikipedia.org/wiki/Euler_angles#Definition_by_intrinsic_rotations

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

Initialize a single rotation along a single axis:

>>> r = R.from_euler('x', 90, degrees=True) >>> r.as_quat().shape (4,)

Initialize a single rotation with a given axis sequence:

>>> r = R.from_euler('zyx', 90, 45, 30, degrees=True) >>> r.as_quat().shape (4,)

Initialize a stack with a single rotation around a single axis:

>>> r = R.from_euler('x', 90, degrees=True) >>> r.as_quat().shape (1, 4)

Initialize a stack with a single rotation with an axis sequence:

>>> r = R.from_euler('zyx', [90, 45, 30], degrees=True) >>> r.as_quat().shape (1, 4)

Initialize multiple elementary rotations in one object:

>>> r = R.from_euler('x', 90, 45, 30, degrees=True) >>> r.as_quat().shape (3, 4)

Initialize multiple rotations in one object:

>>> r = R.from_euler('zyx', [90, 45, 30], [35, 45, 90], degrees=True) >>> r.as_quat().shape (2, 4)

val from_matrix : matrix:[> `Ndarray ] Np.Obj.t -> [> tag ] Obj.t -> Py.Object.t

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 ---------- matrix : array_like, shape (N, 3, 3) or (3, 3) A single matrix or a stack of matrices, where ``matrixi`` is the i-th matrix.

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

Notes ----- This function was called from_dcm before.

.. versionadded:: 1.4.0

val from_quat : ?normalized:Py.Object.t -> quat:[> `Ndarray ] Np.Obj.t -> [> tag ] Obj.t -> Py.Object.t

Initialize from quaternions.

3D rotations can be represented using unit-norm quaternions 1_.

Parameters ---------- quat : array_like, shape (N, 4) or (4,) Each row is a (possibly non-unit norm) quaternion in scalar-last (x, y, z, w) format. Each quaternion will be normalized to unit norm. normalized Deprecated argument. Has no effect, input `quat` is always normalized.

.. deprecated:: 1.4.0

Returns ------- rotation : `Rotation` instance Object containing the rotations represented by input quaternions.

References ---------- .. 1 https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation

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

Initialize a single rotation:

>>> r = R.from_quat(1, 0, 0, 0) >>> r.as_quat() array(1., 0., 0., 0.) >>> r.as_quat().shape (4,)

Initialize multiple rotations in a single object:

>>> r = R.from_quat( ... [1, 0, 0, 0], ... [0, 0, 0, 1] ... ) >>> r.as_quat() array([1., 0., 0., 0.], [0., 0., 0., 1.]) >>> r.as_quat().shape (2, 4)

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

>>> r = R.from_quat([0, 0, 0, 1]) >>> r.as_quat() array([0., 0., 0., 1.]) >>> r.as_quat().shape (1, 4)

Quaternions are normalized before initialization.

>>> r = R.from_quat(0, 0, 1, 1) >>> r.as_quat() array(0. , 0. , 0.70710678, 0.70710678)

val from_rotvec : rotvec:[> `Ndarray ] Np.Obj.t -> [> tag ] Obj.t -> Py.Object.t

Initialize from rotation vectors.

A rotation vector is a 3 dimensional vector which is co-directional to the axis of rotation and whose norm gives the angle of rotation (in radians) 1_.

Parameters ---------- rotvec : array_like, shape (N, 3) or (3,) A single vector or a stack of vectors, where `rot_veci` gives the ith rotation vector.

Returns ------- rotation : `Rotation` instance Object containing the rotations represented by input rotation vectors.

References ---------- .. 1 https://en.wikipedia.org/wiki/Axis%E2%80%93angle_representation#Rotation_vector

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

Initialize a single rotation:

>>> r = R.from_rotvec(np.pi/2 * np.array(0, 0, 1)) >>> r.as_rotvec() array(0. , 0. , 1.57079633) >>> r.as_rotvec().shape (3,)

Initialize multiple rotations in one object:

>>> r = R.from_rotvec( ... [0, 0, np.pi/2], ... [np.pi/2, 0, 0]) >>> r.as_rotvec() array([0. , 0. , 1.57079633], [1.57079633, 0. , 0. ]) >>> r.as_rotvec().shape (2, 3)

It is also possible to have a stack of a single rotaton:

>>> r = R.from_rotvec([0, 0, np.pi/2]) >>> r.as_rotvec().shape (1, 3)

val identity : ?num:int -> [> tag ] Obj.t -> Py.Object.t

Get identity rotation(s).

Composition with the identity rotation has no effect.

Parameters ---------- num : int or None, optional Number of identity rotations to generate. If None (default), then a single rotation is generated.

Returns ------- identity : Rotation object The identity rotation.

val inv : [> tag ] Obj.t -> Py.Object.t

Invert this rotation.

Composition of a rotation with its inverse results in an identity transformation.

Returns ------- inverse : `Rotation` instance Object containing inverse of the rotations in the current instance.

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

Inverting a single rotation:

>>> p = R.from_euler('z', 45, degrees=True) >>> q = p.inv() >>> q.as_euler('zyx', degrees=True) array(-45., 0., 0.)

Inverting multiple rotations:

>>> p = R.from_rotvec([0, 0, np.pi/3], [-np.pi/4, 0, 0]) >>> q = p.inv() >>> q.as_rotvec() array([-0. , -0. , -1.04719755], [ 0.78539816, -0. , -0. ])

val magnitude : [> tag ] Obj.t -> Py.Object.t

Get the magnitude(s) of the rotation(s).

Returns ------- magnitude : ndarray or float Angle(s) in radians, float if object contains a single rotation and ndarray if object contains multiple rotations.

Examples -------- >>> from scipy.spatial.transform import Rotation as R >>> r = R.from_quat(np.eye(4)) >>> r.magnitude() array(3.14159265, 3.14159265, 3.14159265, 0. )

Magnitude of a single rotation:

>>> r0.magnitude() 3.141592653589793

val match_vectors : ?kwds:(string * Py.Object.t) list -> Py.Object.t list -> [> tag ] Obj.t -> Py.Object.t

`match_vectors` is deprecated! match_vectors is deprecated in favor of align_vectors in scipy 1.4.0 and will be removed in scipy 1.6.0

Deprecated in favor of `align_vectors`.

val mean : ?weights:[> `Ndarray ] Np.Obj.t -> [> tag ] Obj.t -> Py.Object.t

Get the mean of the rotations.

Parameters ---------- weights : array_like shape (N,), optional Weights describing the relative importance of the rotations. If None (default), then all values in `weights` are assumed to be equal.

Returns ------- mean : `Rotation` instance Object containing the mean of the rotations in the current instance.

Notes ----- The mean used is the chordal L2 mean (also called the projected or induced arithmetic mean). If ``p`` is a set of rotations with mean ``m``, then ``m`` is the rotation which minimizes ``(weights:, None, None * (p.as_matrix() - m.as_matrix())**2).sum()``.

Examples -------- >>> from scipy.spatial.transform import Rotation as R >>> r = R.from_euler('zyx', [0, 0, 0], ... [1, 0, 0], ... [0, 1, 0], ... [0, 0, 1], degrees=True) >>> r.mean().as_euler('zyx', degrees=True) array(0.24945696, 0.25054542, 0.24945696)

val random : ?num:int -> ?random_state:[ `I of int | `RandomState of Py.Object.t ] -> [> tag ] Obj.t -> Py.Object.t

Generate uniformly distributed rotations.

Parameters ---------- num : int or None, optional Number of random rotations to generate. If None (default), then a single rotation is generated. random_state : int, RandomState instance or None, optional Accepts an integer as a seed for the random generator or a RandomState object. If None (default), uses global `numpy.random` random state.

Returns ------- random_rotation : `Rotation` instance Contains a single rotation if `num` is None. Otherwise contains a stack of `num` rotations.

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

Sample a single rotation:

>>> R.random(random_state=1234).as_euler('zxy', degrees=True) array(-110.5976185 , 55.32758512, 76.3289269 )

Sample a stack of rotations:

>>> R.random(5, random_state=1234).as_euler('zxy', degrees=True) array([-110.5976185 , 55.32758512, 76.3289269 ], [ -91.59132005, -14.3629884 , -93.91933182], [ 25.23835501, 45.02035145, -121.67867086], [ -51.51414184, -15.29022692, -172.46870023], [ -81.63376847, -27.39521579, 2.60408416])

val reduce : ?left:Py.Object.t -> ?right:Py.Object.t -> ?return_indices:bool -> [> tag ] Obj.t -> Py.Object.t

Reduce this rotation with the provided rotation groups.

Reduction of a rotation ``p`` is a transformation of the form ``q = l * p * r``, where ``l`` and ``r`` are chosen from `left` and `right` respectively, such that rotation ``q`` has the smallest magnitude.

If `left` and `right` are rotation groups representing symmetries of two objects rotated by ``p``, then ``q`` is the rotation of the smallest magnitude to align these objects considering their symmetries.

Parameters ---------- left : `Rotation` instance, optional Object containing the left rotation(s). Default value (None) corresponds to the identity rotation. right : `Rotation` instance, optional Object containing the right rotation(s). Default value (None) corresponds to the identity rotation. return_indices : bool, optional Whether to return the indices of the rotations from `left` and `right` used for reduction.

Returns ------- reduced : `Rotation` instance Object containing reduced rotations. left_best, right_best: integer ndarray Indices of elements from `left` and `right` used for reduction.

val to_string : t -> string

Print the object to a human-readable representation.

val show : t -> string

Print the object to a human-readable representation.

val pp : Format.formatter -> t -> unit

Pretty-print the object to a formatter.

OCaml

Innovation. Community. Security.