Skip to main content

High-performance quaternions with JAX support

Project description

FastQuat - High-Performance Quaternions with JAX

PyPI version Python versions Tests

FastQuat provides optimized quaternion operations with full JAX compatibility, featuring:

  • 🚀 Hardware-accelerated computations (CPU/GPU/TPU)
  • 🔄 Automatic differentiation support
  • 🧩 Seamless integration with JAX transformations (jit, grad, vmap)
  • 📦 Efficient storage using interleaved memory layout

Installation

pip install fastquat

This will install FastQuat with CPU support. For GPU support, you may need to install JAX with CUDA support:

pip install "jax[cuda12]" fastquat

Quick Start

import jax.numpy as jnp
from fastquat import Quaternion

# Create quaternions
q1 = Quaternion.ones()  # Identity quaternion
q2 = Quaternion(0.7071, 0.7071, 0.0, 0.0)  # 90° rotation around x-axis

# Quaternion operations
q3 = q1 * q2  # Multiplication
q_inv = 1 / q1           # Inverse, or q1 ** -1
q_norm = q1.normalize()  # Normalization

# Rotate vectors
vector = jnp.array([1.0, 0.0, 0.0])
rotated = q2.rotate_vector(vector)

# Spherical interpolation (SLERP)
interpolated = q1.slerp(q2, t=0.5)  # Halfway between q1 and q2

Features

Core Operations

  • Quaternion arithmetic: Addition, multiplication, conjugation, inverse, power, exponentiation, logarithm
  • Normalization: Efficient unit quaternion computation
  • Conversion: To/from rotation matrices, Euler angles
  • Vector rotation: Direct vector transformation

Advanced Interpolation

  • SLERP (Spherical Linear Interpolation): Smooth rotation interpolation
    • Automatically handles shortest path selection
    • Numerically stable for close quaternions
    • Supports batched operations and array-valued parameters

JAX Integration

  • JIT compilation: Compile quaternion operations for maximum performance
  • Automatic differentiation: Compute gradients through quaternion operations
  • Vectorization: Process batches of quaternions efficiently
  • Device support: Run on CPU, GPU, or TPU

Performance

FastQuat is optimized for high-performance computing:

  • Memory-efficient interleaved storage
  • SIMD-optimized operations on supported hardware
  • Zero-copy integration with JAX arrays
  • Minimal Python overhead through JIT compilation

Examples

Basic Usage

import jax
import jax.numpy as jnp
from fastquat import Quaternion

# Create random quaternions
key = jax.random.PRNGKey(42)
q_batch = Quaternion.random(key, shape=(1000,))

# JIT-compiled batch operations
@jax.jit
def batch_rotate(quaternions, vectors):
    return quaternions.rotate_vector(vectors)

vectors = jax.random.normal(key, (1000, 3))
rotated_batch = batch_rotate(q_batch, vectors)

SLERP

# Smooth rotation interpolation
q_start = Quaternion.ones()
q_end = Quaternion.from_rotation_matrix(rotation_matrix)

# Generate smooth interpolation
t_values = jnp.linspace(0, 1, 100)
interpolated_rotations = q_start.slerp(q_end, t_values)

# Apply to object vertices for smooth animation
animated_vertices = interpolated_rotations.rotate_vector(object_vertices)

Documentation

Full documentation is available at fastquat.readthedocs.io

Contributing

Contributions are welcome! Please see our development guide for details.

License

MIT License - see LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastquat-0.5.1.tar.gz (169.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastquat-0.5.1-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file fastquat-0.5.1.tar.gz.

File metadata

  • Download URL: fastquat-0.5.1.tar.gz
  • Upload date:
  • Size: 169.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for fastquat-0.5.1.tar.gz
Algorithm Hash digest
SHA256 df581f6c0f6c2c3b07402798d566e9b615602d007987aeeb3494a6e95fcac96d
MD5 b7454e6ac23767bdf84fcecd14621f20
BLAKE2b-256 c9662ecaee367a4df7ffb86788ce7b7e0cbc839f11fc981eed7ad036f2d4d14a

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastquat-0.5.1.tar.gz:

Publisher: release.yml on CMBSciPol/fastquat

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastquat-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: fastquat-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for fastquat-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c24891422c6f57db3e4c37b702921921502fbe2d1d31619424c2e7a7dc894932
MD5 6839b74f09330fe7725e461d7eac77fa
BLAKE2b-256 492c32600cd8cca27216886e9aeb9be97b925c0eae558914500131a232198779

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastquat-0.5.1-py3-none-any.whl:

Publisher: release.yml on CMBSciPol/fastquat

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page