numpy-quaternion extends NumPy by adding a quaternion dtype, enabling efficient array operations and mathematical functions for handling quaternions. This library is crucial for applications involving 3D rotations, such as animation, robotics, and aerospace. It is actively maintained with frequent releases, with the current version being 2024.0.13.
pip install numpy-quaternionVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to create a quaternion, represent a 3D point as a pure-imaginary quaternion, and apply a rotation using quaternion multiplication.
Prefer quaternion-based rotation representations (e.g., axis-angle, rotation matrices) and operations where possible, and avoid converting to/from Euler angles unless absolutely necessary.
Ensure that operations that might implicitly convert the dtype are handled carefully. If a copy is needed, explicitly re-cast the result to the quaternion dtype or use library-specific copying mechanisms if available.
For optimal performance and precision in vector rotations, consider using dedicated functions like `rotate_vectors` instead of manual multiplication sequences.
Verify that your `numpy-quaternion` version explicitly supports NumPy 2.x. If you encounter issues, upgrade `numpy-quaternion` to its latest version, which likely includes patches for NumPy 2.x compatibility, or pin your NumPy version to 1.x.
Ensure the package is installed correctly (`python -m pip install numpy-quaternion` or `conda install -c conda-forge quaternion`) and then import it using `import quaternion` (lowercase and singular).
Upgrade `numpy-quaternion` to its latest version (`python -m pip install --upgrade numpy-quaternion`). If the error persists in user code, replace `np.object` with Python's built-in `object` type or `np.object_`.
Access the components of the quaternion (e.g., `q.w`, `q.x`, `q.y`, `q.z`) and perform the operation on the float components, or use a quaternion-specific function if available within the `quaternion` module that handles such operations (e.g., `quaternion.arg(q)` for the angle/argument of a quaternion).
Call these constructors directly from the `quaternion` module, for example: `import quaternion` and then `q = quaternion.from_axis_angle(axis, angle)`.