Skip to main content

A module for using quaternions in Python.

Project description

Documentation Status

Class and mathematical functions for quaternion numbers.

Installation

Python

This is a Python 3 module. If you don’t have Python installed, get the latest version here.

The Quaternions module

Install with pip:

pip install quaternions-for-python

If you want to build from source, you can clone the repository with the following terminal command:

git clone https://github.com/zachartrand/Quaternions.git

How to use

Using the quaternions module

The quaternions module is designed to be imported to use quaternion numbers just like complex numbers in Python. The rest of this file assumes you import the class like this:

>>> from quaternions import Quaternion

To create a quaternion, simply type

>>> Quaternion(a, b, c, d)

where a, b, c, and d correspond to a quaternion of the form a + bi + cj + dk. For example, creating the quaternion 1 - 2i - 3j + 4k looks like this in the Python interpreter:

>>> q1 = Quaternion(1, -2, -3, 4)
>>> q1
Quaternion(1.0, -2.0, -3.0, 4.0)
>>> print(q1)
(1 - 2i - 3j + 4k)

Quaternions have mathematical functionality built in. Adding or multipling two quaternions together uses the same syntax as ints and floats:

>>> q1, q2 = Quaternion(1, -2, -3, 4), Quaternion(1, 4, -3, -2)
>>> print(q1)
(1 - 2i - 3j + 4k)
>>> print(q2)
(1 + 4i - 3j - 2k)
>>> print(q1+q2)
(2 + 2i - 6j + 2k)
>>> print(q1-q2)
(-6i + 0j + 6k)
>>> print(q2-q1)
(6i + 0j - 6k)
>>> print(q1*q2)
(8 + 20i + 6j + 20k)
>>> print(q2*q1)
(8 - 16i - 18j - 16k)
>>> print(q1/q2)
(-0.19999999999999996 - 0.8i - 0.4j - 0.4k)
>>> print(1/q2 * q1)
(-0.19999999999999996 + 0.4i + 0.4j + 0.8k)
>>> print(q2/q1)
(-0.19999999999999996 + 0.8i + 0.4j + 0.4k)

Check the documentation for other useful methods of the Quaternion class.

Using the qmath module

The qmath module contains some functions that are compatible with quaternions, similarly to how the cmath module works. These include the exponential function, the natural logarithm, and the square root function. It also includes a function, rotate3d(), that takes an iterable of coordinates and rotates them a given angle around a given axis (the z-axis by default). Here is an example rotating the point (1, 0, 0) around the z-axis:

>>> from quaternions import qmath
>>>
>>> p = (1, 0, 0)
>>>
>>> p = qmath.rotate3d(p, 90); print(p)
(0.0, 1.0, 0.0)
>>> p = qmath.rotate3d(p, 90); print(p)
(-1.0, 0.0, 0.0)
>>> p = qmath.rotate3d(p, 90); print(p)
(0.0, -1.0, 0.0)
>>> p = qmath.rotate3d(p, 90); print(p)
(1.0, 0.0, 0.0)

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

quaternions-for-python-1.1.3.tar.gz (16.4 kB view details)

Uploaded Source

Built Distribution

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

quaternions_for_python-1.1.3-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

Details for the file quaternions-for-python-1.1.3.tar.gz.

File metadata

  • Download URL: quaternions-for-python-1.1.3.tar.gz
  • Upload date:
  • Size: 16.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for quaternions-for-python-1.1.3.tar.gz
Algorithm Hash digest
SHA256 1e8cb5ab27023b44c4c3c0aefa18cd3a6ebcbd024990de09c48d76e6c58bbfd6
MD5 6fc2ca317745f305a91c65aeff3acc3d
BLAKE2b-256 31ae49561861bda1ecb61d7c8ca29844393040104605a69afb321e779b07d17a

See more details on using hashes here.

File details

Details for the file quaternions_for_python-1.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for quaternions_for_python-1.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 5b6296feb1387a39f22fd6a8ffe855fa65fdf3ee1c9fe887c119b56fd81028b1
MD5 0938ab25f04e03fe965d20bc77813125
BLAKE2b-256 7646d94d24b3e9be02ecd966350260aa5ceb9eff79a93a96f7f1325e99b99e69

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