Quaternion Class
- class fastquat.quaternion.Quaternion(w: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray = 0, x: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray = 0, y: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray = 0, z: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray = 0, dtype: str | type[Any] | dtype | SupportsDType | None = None)[source]
Bases:
objectClass for manipulating quaternion tensors with JAX.
A quaternion is represented by [w, x, y, z] where w is the scalar part and (x, y, z) is the vector part.
- __init__(w: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray = 0, x: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray = 0, y: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray = 0, z: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray = 0, dtype: str | type[Any] | dtype | SupportsDType | None = None) None[source]
Initialize a tensor of quaternions.
- Parameters:
w – components of the quaternions.
x – components of the quaternions.
y – components of the quaternions.
z – components of the quaternions.
dtype – Data type of the quaternion components (inferred by default).
- classmethod from_array(array: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray) Self[source]
Create a Quaternion array from a numeric array of shape (…, 4).
- Parameters:
array – array of shape (…, 4) where the last dimension is [w, x, y, z]
- classmethod from_scalar_vector(scalar: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray, vector: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray) Self[source]
Create a quaternion from scalar and vector parts.
- Parameters:
scalar – Array of shape (…,) for the scalar part.
vector – Array of shape (…, 3) for the vector part.
- Returns:
Quaternion
- classmethod from_rotation_matrix(rot: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray) Self[source]
Create the quaternion associated to a rotation matrix.
- Parameters:
rot – Array of shape (…, 3, 3) representing the rotation matrix
- Returns:
The normalized Quaternion tensor representing the rotation matrix.
- classmethod zeros(shape: tuple[int, ...], dtype: str | type[Any] | dtype | SupportsDType | None = None) Self[source]
Create quaternions with all components set to 0.
- Parameters:
shape – Shape of the tensor (without the last dimension).
dtype – Data type of the quaternion components.
- Returns:
Quaternion with all components equal to 0.
- classmethod ones(shape: tuple[int, ...], dtype: str | type[Any] | dtype | SupportsDType | None = None) Self[source]
Create quaternions with scalar component set to 1 and vector components set to 0.
- Parameters:
shape – Shape of the tensor (without the last dimension).
dtype – Data type of the quaternion components.
- Returns:
Quaternions with w=1 and x=y=z=0.
- classmethod full(shape: tuple[int, ...], fill_value: float, dtype: str | type[Any] | dtype | SupportsDType | None = None) Self[source]
Create quaternions with scalar component set to a value and vector components set to 0.
- Parameters:
shape – Shape of the tensor (without the last dimension).
fill_value – Value to fill the scalar component with.
dtype – Data type of the quaternion components.
- Returns:
Quaternions with w=fill_value and x=y=z=0.
- classmethod random(key: PRNGKey, shape: tuple[int, ...] = (), dtype: str | type[Any] | dtype | SupportsDType | None = None) Self[source]
Generate normalized random quaternions.
- Parameters:
key – Key PRNG.
shape – Shape of the tensor (without the last dimension).
dtype – Data type of the quaternion components.
- Returns:
Normalized Quaternion.
- normalize() Self[source]
Normalize the quaternion.
Returns the normalized quaternion. If the quaternion has zero norm, returns the quaternion [NaN, NaN, NaN, NaN].
- to_rotation_matrix() Array[source]
Convert quaternion to rotation matrix.
- Returns:
Array of shape (…, 3, 3)
- rotate_vector(v: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray) Array[source]
Apply quaternion rotation to a vector.
- Parameters:
v – Array of shape (…, 3) representing vectors
- Returns:
Array of shape (…, 3) representing rotated vectors
- __pow__(exponent: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray) Self[source]
Quaternion exponentiation q^n.
For integer exponents, uses optimized special cases. For non-integer exponents, uses the general formula: q^n = exp(n * log(q))
- Parameters:
exponent – The exponent (scalar or array)
- Returns:
The quaternion raised to the given power
- log() Self[source]
Compute quaternion logarithm.
For a quaternion q = ‖q‖ * (cos(θ) + sin(θ)v), the logarithm is: log(q) = log(‖q‖) + θ * v
For the zero quaternion, returns (-inf, 0, 0, 0).
- Returns:
The logarithm of the quaternion
- exp() Self[source]
Compute quaternion exponential.
For a quaternion q = s + v, the exponential is: exp(q) = exp(s) * (cos(‖v‖) + sin(‖v‖) * v/‖v‖)
- Returns:
The exponential of the quaternion
- property ndim
Number of dimensions of the quaternion tensor (without the quaternion dimension).
- property size
Total number of quaternions.
- slerp(other: Self, t: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray) Self[source]
Spherical linear interpolation between two quaternions.
- Parameters:
other – Target quaternion to interpolate towards
t – Interpolation parameter in [0, 1]. t=0 returns self, t=1 returns other
- Returns:
Interpolated quaternion
The Quaternion class provides a comprehensive interface for quaternion operations
optimized for JAX. All methods are compatible with JAX transformations including JIT
compilation, automatic differentiation, and vectorization.
Constructor Methods
- Quaternion.__init__(w: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray = 0, x: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray = 0, y: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray = 0, z: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray = 0, dtype: str | type[Any] | dtype | SupportsDType | None = None) None[source]
Initialize a tensor of quaternions.
- Parameters:
w – components of the quaternions.
x – components of the quaternions.
y – components of the quaternions.
z – components of the quaternions.
dtype – Data type of the quaternion components (inferred by default).
- classmethod Quaternion.from_array(array: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray) Self[source]
Create a Quaternion array from a numeric array of shape (…, 4).
- Parameters:
array – array of shape (…, 4) where the last dimension is [w, x, y, z]
- classmethod Quaternion.from_scalar_vector(scalar: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray, vector: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray) Self[source]
Create a quaternion from scalar and vector parts.
- Parameters:
scalar – Array of shape (…,) for the scalar part.
vector – Array of shape (…, 3) for the vector part.
- Returns:
Quaternion
- classmethod Quaternion.from_rotation_matrix(rot: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray) Self[source]
Create the quaternion associated to a rotation matrix.
- Parameters:
rot – Array of shape (…, 3, 3) representing the rotation matrix
- Returns:
The normalized Quaternion tensor representing the rotation matrix.
- classmethod Quaternion.zeros(shape: tuple[int, ...], dtype: str | type[Any] | dtype | SupportsDType | None = None) Self[source]
Create quaternions with all components set to 0.
- Parameters:
shape – Shape of the tensor (without the last dimension).
dtype – Data type of the quaternion components.
- Returns:
Quaternion with all components equal to 0.
- classmethod Quaternion.ones(shape: tuple[int, ...], dtype: str | type[Any] | dtype | SupportsDType | None = None) Self[source]
Create quaternions with scalar component set to 1 and vector components set to 0.
- Parameters:
shape – Shape of the tensor (without the last dimension).
dtype – Data type of the quaternion components.
- Returns:
Quaternions with w=1 and x=y=z=0.
- classmethod Quaternion.full(shape: tuple[int, ...], fill_value: float, dtype: str | type[Any] | dtype | SupportsDType | None = None) Self[source]
Create quaternions with scalar component set to a value and vector components set to 0.
- Parameters:
shape – Shape of the tensor (without the last dimension).
fill_value – Value to fill the scalar component with.
dtype – Data type of the quaternion components.
- Returns:
Quaternions with w=fill_value and x=y=z=0.
- classmethod Quaternion.random(key: PRNGKey, shape: tuple[int, ...] = (), dtype: str | type[Any] | dtype | SupportsDType | None = None) Self[source]
Generate normalized random quaternions.
- Parameters:
key – Key PRNG.
shape – Shape of the tensor (without the last dimension).
dtype – Data type of the quaternion components.
- Returns:
Normalized Quaternion.
Properties
- Quaternion.w
- Quaternion.x
- Quaternion.y
- Quaternion.z
- Quaternion.vector
Vector part (…, 3)
- Quaternion.shape
Shape of the tensor.
- Quaternion.dtype
Data type.
Core Operations
Rotation Operations
Interpolation
- Quaternion.slerp(other: Self, t: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray) Self[source]
Spherical linear interpolation between two quaternions.
- Parameters:
other – Target quaternion to interpolate towards
t – Interpolation parameter in [0, 1]. t=0 returns self, t=1 returns other
- Returns:
Interpolated quaternion
Advanced Operations
- Quaternion.log() Self[source]
Compute quaternion logarithm.
For a quaternion q = ‖q‖ * (cos(θ) + sin(θ)v), the logarithm is: log(q) = log(‖q‖) + θ * v
For the zero quaternion, returns (-inf, 0, 0, 0).
- Returns:
The logarithm of the quaternion
- Quaternion.exp() Self[source]
Compute quaternion exponential.
For a quaternion q = s + v, the exponential is: exp(q) = exp(s) * (cos(‖v‖) + sin(‖v‖) * v/‖v‖)
- Returns:
The exponential of the quaternion
- Quaternion.__pow__(exponent: Array | ndarray | bool | number | bool | int | float | complex | TypedNdArray) Self[source]
Quaternion exponentiation q^n.
For integer exponents, uses optimized special cases. For non-integer exponents, uses the general formula: q^n = exp(n * log(q))
- Parameters:
exponent – The exponent (scalar or array)
- Returns:
The quaternion raised to the given power
Array Operations
Device and Memory
- Quaternion.device
- Quaternion.nbytes
Number of bytes in the tensor.
- Quaternion.itemsize
Size of one quaternion element in bytes.
- Quaternion.size
Total number of quaternions.
- Quaternion.ndim
Number of dimensions of the quaternion tensor (without the quaternion dimension).