narupatools.physics.rotations

Methods for dealing with rotations and transformations.

Functions

quaternion_as_rotation_matrix

Calculate the rotation matrix from a quaternion \(q = x i + y j + z k + w\).

quaternion_conjugate

Calculate the conjugate of a quaternion \(q = x i + y j + z k + w\).

quaternion_inverse

Calculate the inverse of a quaternion \(q = x i + y j + z k + w\).

rotate_around_center_of_mass

Rotate a set of particles about their center of mass.

rotate_around_origin

Rotate a set of points about the origin (0, 0, 0).

rotate_around_point

Rotate a set of points about an arbitrary point \(c\).

narupatools.physics.rotations.quaternion_as_rotation_matrix(quaternion: Union[numpy.ndarray[Any, numpy.dtype[numpy.float64]], Sequence[float]], /)numpy.ndarray[Any, numpy.dtype[numpy.float64]]

Calculate the rotation matrix from a quaternion \(q = x i + y j + z k + w\).

This uses the formula:

\[\begin{split}R = \begin{bmatrix} 1 - 2(y^2 + z^2) & 2 (x y - z w) & 2 (x z + y w) \\ 2 (x y + z w) & 1 - 2(x^2 + z^2) & 2 (y z - x w) \\ 2(x z - y w) & 2 (y z + x w) & 1 - 2 (x^2 + y^2) \end{bmatrix}\end{split}\]

where \(q\) is assumed to be a unit quaternion.

Parameters

quaternion – Quaternion \(q\) to represent as a rotation matrix

Returns

Rotation matrix \(R\) as a (3, 3) NumPy array.

narupatools.physics.rotations.quaternion_conjugate(quaternion: Union[numpy.ndarray[Any, numpy.dtype[numpy.float64]], Sequence[float]], /)numpy.ndarray[Any, numpy.dtype[numpy.float64]]

Calculate the conjugate of a quaternion \(q = x i + y j + z k + w\).

This is given by:

\[q^* = - x i - y j - z k + w\]
Parameters

quaternion – Quaternion \(q\) to conjugate.

Returns

Conjugate \(q^*\) of the quaternion \(q\).

narupatools.physics.rotations.quaternion_inverse(quaternion: Union[numpy.ndarray[Any, numpy.dtype[numpy.float64]], Sequence[float]], /)numpy.ndarray[Any, numpy.dtype[numpy.float64]]

Calculate the inverse of a quaternion \(q = x i + y j + z k + w\).

This is given by:

\[q^{-1} = \frac{q^*}{|q|^2}\]

where \(q^*\) is the conjugate of the quaternion \(q\) and |q| is the magnitude of the quaternion \(q\).

Parameters

quaternion – Quaternion \(q\) to invert.

Returns

Inverse \(q^{-1}\) of the quaternion \(q\).

narupatools.physics.rotations.rotate_around_center_of_mass(*, masses: Union[numpy.ndarray[Any, numpy.dtype[numpy.float64]], Sequence[float]], positions: Union[numpy.ndarray[Any, numpy.dtype[numpy.float64]], Sequence[numpy.ndarray[Any, numpy.dtype[numpy.float64]]], Sequence[Sequence[float]]], rotation: Union[numpy.ndarray[Any, numpy.dtype[numpy.float64]], Sequence[float]])numpy.ndarray[Any, numpy.dtype[numpy.float64]]

Rotate a set of particles about their center of mass.

Parameters
  • masses – List of masses \(m_i\) of each particle.

  • positions – List of positions \(r_i\) of each particle.

  • rotation – Rotation to perform, either as a 3x3 rotation matrix \(R\) or a quaternion \(q = x i + y j + z k + w\) as an array (x, y, z, w).

Returns

Set of positions that have been rotated around their center of mass.

narupatools.physics.rotations.rotate_around_origin(*, positions: Union[numpy.ndarray[Any, numpy.dtype[numpy.float64]], Sequence[numpy.ndarray[Any, numpy.dtype[numpy.float64]]], Sequence[Sequence[float]]], rotation: Union[numpy.ndarray[Any, numpy.dtype[numpy.float64]], Sequence[float]])numpy.ndarray[Any, numpy.dtype[numpy.float64]]

Rotate a set of points about the origin (0, 0, 0).

Parameters
  • positions – List of positions \(r_i\).

  • rotation – Rotation to perform, either as a 3x3 rotation matrix \(R\) or a quaternion \(q = x i + y j + z k + w\) as an array (x, y, z, w).

Returns

Set of positions that have been rotated around the origin (0, 0, 0).

narupatools.physics.rotations.rotate_around_point(*, positions: Union[numpy.ndarray[Any, numpy.dtype[numpy.float64]], Sequence[numpy.ndarray[Any, numpy.dtype[numpy.float64]]], Sequence[Sequence[float]]], rotation: Union[numpy.ndarray[Any, numpy.dtype[numpy.float64]], Sequence[float]], origin: Union[numpy.ndarray[Any, numpy.dtype[numpy.float64]], Sequence[float]])numpy.ndarray[Any, numpy.dtype[numpy.float64]]

Rotate a set of points about an arbitrary point \(c\).

Parameters
  • origin – Point \(c\) about which to rotate the positions.

  • positions – List of positions \(r_i\).

  • rotation – Rotation to perform, either as a 3x3 rotation matrix \(R\) or a quaternion \(q = x i + y j + z k + w\) as an array (x, y, z, w).

Returns

Set of positions that have been rotated around the origin \(c\).