Skip to content

Quaternion Class

The Quaternion class provides a compact, singularity-free representation of 3D rotations for spacecraft attitude determination and control.

Quaternion

Quaternion(w: float, x: float, y: float, z: float)

Represents a quaternion for 3D rotations.

Quaternions provide a compact, singularity-free representation of rotations. The quaternion is stored as [w, x, y, z] where w is the scalar part and [x, y, z] is the vector part.

Parameters:

Name Type Description Default
w float

Scalar component

required
x float

X component of vector part

required
y float

Y component of vector part

required
z float

Z component of vector part

required
Example
import brahe as bh
import numpy as np

# Create identity quaternion
q = bh.Quaternion(1.0, 0.0, 0.0, 0.0)
print(f"Norm: {q.norm()}")

# Create from array
q_vec = np.array([1.0, 0.0, 0.0, 0.0])
q2 = bh.Quaternion.from_vector(q_vec, scalar_first=True)

# Convert to rotation matrix
dcm = q.to_rotation_matrix()

# Quaternion multiplication
q3 = q * q2

# Normalize
q3.normalize()

Initialize instance.

__doc__ class-attribute

__doc__ = 'Represents a quaternion for 3D rotations.\n\nQuaternions provide a compact, singularity-free representation of rotations.\nThe quaternion is stored as [w, x, y, z] where w is the scalar part and\n[x, y, z] is the vector part.\n\nArgs:\n    w (float): Scalar component\n    x (float): X component of vector part\n    y (float): Y component of vector part\n    z (float): Z component of vector part\n\nExample:\n    ```python\n    import brahe as bh\n    import numpy as np\n\n    # Create identity quaternion\n    q = bh.Quaternion(1.0, 0.0, 0.0, 0.0)\n    print(f"Norm: {q.norm()}")\n\n    # Create from array\n    q_vec = np.array([1.0, 0.0, 0.0, 0.0])\n    q2 = bh.Quaternion.from_vector(q_vec, scalar_first=True)\n\n    # Convert to rotation matrix\n    dcm = q.to_rotation_matrix()\n\n    # Quaternion multiplication\n    q3 = q * q2\n\n    # Normalize\n    q3.normalize()\n    ```'

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to 'utf-8'. errors defaults to 'strict'.

__module__ class-attribute

__module__ = 'brahe._brahe'

str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.str() (if defined) or repr(object). encoding defaults to 'utf-8'. errors defaults to 'strict'.

data property

data: ndarray

Get the quaternion components as a numpy array [w, x, y, z].

Returns:

Type Description
ndarray

numpy.ndarray: 4-element array containing quaternion components

__add__ method descriptor

__add__(value)

Return self+value.

__eq__ method descriptor

__eq__(value)

Return self==value.

__ge__ method descriptor

__ge__(value)

Return self>=value.

__getitem__ method descriptor

__getitem__(key: str) -> Any

Return self[key].

__gt__ method descriptor

__gt__(value)

Return self>value.

__iadd__ method descriptor

__iadd__(value)

Return self+=value.

__imul__ method descriptor

__imul__(value)

Return self*=value.

__isub__ method descriptor

__isub__(value)

Return self-=value.

__le__ method descriptor

__le__(value)

Return self<=value.

__lt__ method descriptor

__lt__(value)

Return self<value.

__mul__ method descriptor

__mul__(value)

Return self*value.

__ne__ method descriptor

__ne__(value)

Return self!=value.

__new__ builtin

__new__(*args, **kwargs)

Create and return a new object. See help(type) for accurate signature.

__radd__ method descriptor

__radd__(value)

Return value+self.

__repr__ method descriptor

__repr__() -> str

Return repr(self).

__rmul__ method descriptor

__rmul__(value)

Return value*self.

__rsub__ method descriptor

__rsub__(value)

Return value-self.

__str__ method descriptor

__str__() -> str

Return str(self).

__sub__ method descriptor

__sub__(value)

Return self-value.

conjugate method descriptor

conjugate() -> Quaternion

Compute the conjugate of the quaternion.

Returns:

Name Type Description
Quaternion Quaternion

Conjugate quaternion with negated vector part

Example
import brahe as bh

q = bh.Quaternion(1.0, 0.0, 0.0, 0.0)
q_conj = q.conjugate()

from_euler_angle builtin

from_euler_angle(e: EulerAngle) -> Quaternion

Create a quaternion from an Euler angle representation.

Parameters:

Name Type Description Default
e EulerAngle

Euler angle representation

required

Returns:

Name Type Description
Quaternion Quaternion

Equivalent quaternion

Example
import brahe as bh

euler = bh.EulerAngle("XYZ", 0.1, 0.2, 0.3, bh.AngleFormat.RADIANS)
q = bh.Quaternion.from_euler_angle(euler)

from_euler_axis builtin

from_euler_axis(e: EulerAxis) -> Quaternion

Create a quaternion from an Euler axis representation.

Parameters:

Name Type Description Default
e EulerAxis

Euler axis representation

required

Returns:

Name Type Description
Quaternion Quaternion

Equivalent quaternion

Example
import brahe as bh
import numpy as np

axis = np.array([0.0, 0.0, 1.0])
ea = bh.EulerAxis(axis, 1.5708, bh.AngleFormat.RADIANS)
q = bh.Quaternion.from_euler_axis(ea)

from_quaternion builtin

from_quaternion(q: Quaternion) -> Quaternion

Create a quaternion from another quaternion (copy constructor).

Parameters:

Name Type Description Default
q Quaternion

Source quaternion

required

Returns:

Name Type Description
Quaternion Quaternion

New quaternion instance

Example
import brahe as bh

q1 = bh.Quaternion(1.0, 0.0, 0.0, 0.0)
q2 = bh.Quaternion.from_quaternion(q1)

from_rotation_matrix builtin

from_rotation_matrix(r: RotationMatrix) -> Quaternion

Create a quaternion from a rotation matrix.

Parameters:

Name Type Description Default
r RotationMatrix

Rotation matrix

required

Returns:

Name Type Description
Quaternion Quaternion

Equivalent quaternion

Example
import brahe as bh
import numpy as np

mat = np.eye(3)
rm = bh.RotationMatrix.from_matrix(mat)
q = bh.Quaternion.from_rotation_matrix(rm)

from_vector builtin

from_vector(v: ndarray, scalar_first: bool) -> Quaternion

Create a quaternion from a numpy array.

Parameters:

Name Type Description Default
v ndarray

4-element array containing quaternion components

required
scalar_first bool

If True, array is [w, x, y, z], else [x, y, z, w]

required

Returns:

Name Type Description
Quaternion Quaternion

New quaternion instance

Example
import brahe as bh
import numpy as np

v = np.array([1.0, 0.0, 0.0, 0.0])
q = bh.Quaternion.from_vector(v, scalar_first=True)

inverse method descriptor

inverse() -> Quaternion

Compute the inverse of the quaternion.

Returns:

Name Type Description
Quaternion Quaternion

Inverse quaternion

Example
import brahe as bh

q = bh.Quaternion(1.0, 0.0, 0.0, 0.0)
q_inv = q.inverse()

norm method descriptor

norm() -> float

Calculate the norm (magnitude) of the quaternion.

Returns:

Name Type Description
float float

Euclidean norm of the quaternion

Example
import brahe as bh

q = bh.Quaternion(1.0, 0.0, 0.0, 0.0)
norm = q.norm()

normalize method descriptor

normalize() -> Any

Normalize the quaternion in-place to unit length.

Example
import brahe as bh

q = bh.Quaternion(2.0, 0.0, 0.0, 0.0)
q.normalize()

slerp method descriptor

slerp(other: Quaternion, t: float) -> Quaternion

Perform spherical linear interpolation (SLERP) between two quaternions.

Parameters:

Name Type Description Default
other Quaternion

Target quaternion

required
t float

Interpolation parameter in [0, 1]

required

Returns:

Name Type Description
Quaternion Quaternion

Interpolated quaternion

Example
import brahe as bh

q1 = bh.Quaternion(1.0, 0.0, 0.0, 0.0)
q2 = bh.Quaternion(0.707, 0.707, 0.0, 0.0)
q_mid = q1.slerp(q2, 0.5)

to_euler_angle method descriptor

to_euler_angle(order: str) -> EulerAngle

Convert to Euler angle representation.

Parameters:

Name Type Description Default
order str

Rotation sequence (e.g., "XYZ", "ZYX")

required

Returns:

Name Type Description
EulerAngle EulerAngle

Equivalent Euler angles

Example
import brahe as bh

q = bh.Quaternion(1.0, 0.0, 0.0, 0.0)
euler = q.to_euler_angle("XYZ")

to_euler_axis method descriptor

to_euler_axis() -> EulerAxis

Convert to Euler axis representation.

Returns:

Name Type Description
EulerAxis EulerAxis

Equivalent Euler axis

to_quaternion method descriptor

to_quaternion() -> Quaternion

Convert to quaternion representation (returns self).

Returns:

Name Type Description
Quaternion Quaternion

This quaternion

to_rotation_matrix method descriptor

to_rotation_matrix() -> RotationMatrix

Convert to rotation matrix representation.

Returns:

Name Type Description
RotationMatrix RotationMatrix

Equivalent rotation matrix

to_vector method descriptor

to_vector(scalar_first: bool) -> ndarray

Convert quaternion to a numpy array.

Parameters:

Name Type Description Default
scalar_first bool

If True, returns [w, x, y, z], else [x, y, z, w]

required

Returns:

Type Description
ndarray

numpy.ndarray: 4-element array containing quaternion components

Example
import brahe as bh

q = bh.Quaternion(1.0, 0.0, 0.0, 0.0)
v = q.to_vector(scalar_first=True)