dair_pll.quaternion

Quaternion-based \(SO(3)\) operations.

class dair_pll.quaternion.DataType

Static type for both supported types of quaternion/vector representations: Tensor and ndarray.

alias of TypeVar(‘DataType’, ~torch.Tensor, ~numpy.ndarray)

class dair_pll.quaternion.TensorCallable(*args, **kwargs)[source]

Bases: Protocol

Static type for callable mapping from list of Tensors to Tensor.

class dair_pll.quaternion.NdarrayCallable(*args, **kwargs)[source]

Bases: Protocol

Static type for callable mapping from list of ndarrays to ndarray.

dair_pll.quaternion.operation_selector(tensor_operation, ndarray_operation, *args)[source]

Helper function which selects between Pytorch and Numpy implementations of a quaternion operation.

Parameters:
Return type:

~DataType

Returns:

Operation’s return value, same type as arguments.

dair_pll.quaternion.inverse_torch(q)[source]

Tensor implementation of inverse().

Return type:

Tensor

dair_pll.quaternion.inverse_np(q)[source]

ndarray implementation of inverse().

Return type:

ndarray

dair_pll.quaternion.inverse(q)[source]

Quaternion inverse function.

For input quaternion \(q = [q_w, q_{xyz}]\), returns the inverse quaternion \(q^{-1} = [-q_w, q_{xyz}]\).

Parameters:

q (~DataType) – (*, 4) quaternion batch to invert.

Return type:

~DataType

Returns:

(*, 4) inverse of q.

dair_pll.quaternion.multiply_torch(q, r)[source]

Tensor implementation of multiply().

Return type:

Tensor

dair_pll.quaternion.multiply_np(q, r)[source]

ndarray implementation of multiply().

dair_pll.quaternion.multiply(q, r)[source]

Quaternion multiplication.

Given 2 quaternions \(q = [q_w, q_{xyz}]\) and \(r = [r_w, r_{xyz}]\), performs the quaternion multiplication via the formula

\[\begin{split}q \times r = \begin{bmatrix} q_w r_w - q_{xyz} \cdot r_{xyz} \\ q_w r_{xyz} + r_w q_{xyz} + q_{xyz} \times r_{xyz} \end{bmatrix}\end{split}\]

This formula was taken from the following address: https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation#Quaternion-derived_rotation_matrix

Parameters:
  • q (~DataType) – (*, 4) left quaternion factor .

  • r (~DataType) – (*, 4) right quaternion factor.

Return type:

~DataType

Returns:

(*, 4) Product quaternion q * r.

dair_pll.quaternion.rotate_torch(q, p)[source]

Tensor implementation of rotate().

Return type:

Tensor

dair_pll.quaternion.rotate_np(q, p)[source]

ndarray implementation of rotate().

Return type:

ndarray

dair_pll.quaternion.rotate(q, p)[source]

Quaternion rotation.

Given a quaternion \(q = [q_w, q_{xyz}]\) and vector \(p\), produces the \(q\)-rotated vector \(p'\) via the formula

\[p' = (q_{xyz} \cdot p) q_{xyz} + 2 q_w (q_{xyz} \times p) + q_w^2 p + q_{xyz} \times (q_{xyz} \times p)\]

This formula was taken from the following address: https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation#Quaternion-derived_rotation_matrix

Parameters:
Return type:

~DataType

Returns:

(*, 3) rotated vector batch.

dair_pll.quaternion.sinc(x)[source]

Elementwise \(\mathrm{sinc}\) function.

Given a tensor \(x\), applies the elementwise-mapping

\[\begin{split}x \to \begin{cases}1 & x =0 \\ \frac{\sin(x)}{x} & x \neq 0\end{cases}.\end{split}\]
Parameters:

x (Tensor) – (*,) \(\mathrm{sinc}\) input values.

Return type:

Tensor

Returns:

(*,) \(\mathrm{sinc}\) function evaluated at x.

dair_pll.quaternion.log(q)[source]

Transforms quaternion into logarithmic coordinates.

Given a quaternion

\[q = [\cos(\theta/2), \hat u \sin(\theta/2)] = [q_w, q_{xyz}],\]

returns the corresponding logarithmic coordinates (rotation vector) \(r = \theta\hat u\).

This computation is evaluated via the pertations

\[\begin{split}\begin{align} \theta(q) &= 2\mathrm{atan2}(||q_{xyz}||_2, q_w), \\ r &= \begin{cases} \frac{\theta(q)}{\sin(\theta(q)/2)} q_{xyz} & \sin( \theta(q)/2) \neq 0,\\ 0 & \sin(\theta(q)/2) = 0.\end{cases} \end{align}\end{split}\]

This function inverts exp().

Parameters:

q (Tensor) – (*, 4) quaternion batch.

Return type:

Tensor

Returns:

(*, 3) rotation vector batch \(r\).

dair_pll.quaternion.exp(r)[source]

Transforms logarithmic coordinates into quaternion.

Given logarithmic coordinates representation (rotation vector) \(r = \theta\hat u\), returns the corresponding quaternion

\[q = [\cos(\theta/2), \hat u \sin(\theta/2)] = [q_w, q_{xyz}].\]

This computation is evaluated via the operations

\[\begin{split}\begin{align} \theta(r) &= ||r||_2, \\ q &= \begin{bmatrix}\cos(\theta(r)/2) \\ \frac{1}{2} r \mathrm{sinc}(\theta(r)/2) \end{bmatrix}. \end{align}\end{split}\]

This function inverts log().

Parameters:

r (Tensor) – (*, 3) rotation vector batch.

Return type:

Tensor

Returns:

(*, 4) quaternion batch \(q\).