Skip to main content

Quaternion operations in pure PyTorch

Project description

Quaternions in PyTorch

QuaTorch

Release cov Tests Docs

QuaTorch is a lightweight python package providing Quaternion, a torch.Tensor subclass that represents a Quaternion. It implements common special operations for quaternions such as multiplication, conjugation, inversion, normalization, log, exp, etc. It also supports conversion to/from rotation matrix and axis-angle representation. Convenient utilities are provided together, such as spherical linear interpolation (slerp) and 3D vector rotation.

Highlights

  • Quaternion type: quatorch.Quaternion (subclass of torch.Tensor).
  • Element-wise and algebraic ops implemented: +, -, * (quaternion product and scalar mul), abs (norm), conjugate, inverse, normalize, to_rotation_matrix, and more.
  • Utilities: from_rotation_matrix, from_axis_angle, to_axis_angle, rotate_vector, slerp, log, exp, and pow.
  • Compatible with torch.compile without graph breaks.

Installation

This project targets Python 3.10+ and requires PyTorch. Install via pip (recommended):

pip install quatorch

Or install editable/development mode:

git clone 
cd QuaTorch
pip install -e .

Quick start

Basic usage examples using PyTorch tensors and Quaternion:

import torch
from quatorch import Quaternion

# Create a quaternion from four scalars (W, X, Y, Z)
q = Quaternion(1.0, 0.0, 0.0, 0.0)

# Or from a tensor of shape (..., 4)
q2 = Quaternion(torch.tensor([0.9239, 0.3827, 0.0, 0.0]))  # 45° around X

# Normalize
q2 = q2.normalize()

# Quaternion multiplication (rotation composition)
q3 = q * q2

# Rotate a vector
v = torch.tensor([1.0, 0.0, 0.0])
v_rot = q2.rotate_vector(v)

# Convert to rotation matrix
R = q2.to_rotation_matrix()

# Slerp between quaternions
t = 0.5
q_mid = q.slerp(q2, t)

API notes

  • Order definition:

    • The quaternion $q=w + x\mathbf{i} + y\mathbf{j} + z\mathbf{k}$ is represented by an ordered tuple $(w, x, y, z)$ and this is the expected order for a quaternion in the whole library (i.e., watch out for XYZW-ordered incoming data).
  • Construction:

    • Quaternion(data: torch.Tensor) where data has .shape[-1] == 4. An arbitrary leading shape is supported in all operations.
    • Quaternion(w, x, y, z) accepts scalars or tensors broadcastable to the same shape.
  • Interoperability:

    • The class implements several torch.* functions via a small dispatcher so many PyTorch APIs behave sensibly with Quaternion objects.

Running tests

This repository includes unit tests using pytest under test/unit_tests.

From the project root, run:

uv run --with=.[cu128] pytest 

or

uv run --with=.[cpu] pytest 

Contributing

Contributions are welcome! In particular:

  • Bug reports and feature requests
  • Optimizing performance
  • Helping improving documentation

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

quatorch-0.3.0.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

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

quatorch-0.3.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file quatorch-0.3.0.tar.gz.

File metadata

  • Download URL: quatorch-0.3.0.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for quatorch-0.3.0.tar.gz
Algorithm Hash digest
SHA256 598846b45489b4ba602b972f96c41e4efcce170f68dec73d162692925acf6316
MD5 a0b826766a91d08b86331313b960f517
BLAKE2b-256 a0d5cb87a9d832aab2373a5ff2c7c9d8e096699e40dea3e4ff24769379afee61

See more details on using hashes here.

File details

Details for the file quatorch-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: quatorch-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 7.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for quatorch-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ff3f7d4297bf2be7cb41b22582d25874af2705d5850f1912777b9e6fd81eed57
MD5 e3ffaa6f32413c358a8cac78a9872705
BLAKE2b-256 ed5826ee7e8a6eb085a99b0fe5cca0689988f9487371f5ee56aed5f51ac0167f

See more details on using hashes here.

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