Prototype Quaternion dtype support for Numpy - see for maintained version
C Python
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
quaternion.h Cleanup and add additional ufunc implementations. Jul 16, 2011


This Python module adds a quaternion dtype to NumPy.

To build:

 $ python build

To install (may require administrator rights):

 # python install


 >>> import numpy as np
 >>> import quaternion
 >>> np.quaternion(1,0,0,0)
 quaternion(1, 0, 0, 0)
 >>> q1 = np.quaternion(1,2,3,4)
 >>> q2 = np.quaternion(5,6,7,8)
 >>> q1 * q2
 quaternion(-60, 12, 30, 24)
 >>> a = np.array([q1, q2])
 >>> a
 array([quaternion(1, 2, 3, 4), quaternion(5, 6, 7, 8)], dtype=quaternion)
 >>> exp(a)
 array([quaternion(1.69392, -0.78956, -1.18434, -1.57912),
        quaternion(138.909, -25.6861, -29.9671, -34.2481)], dtype=quaternion)

The following ufuncs are implemented:
 add, subtract, multiply, divide, log, exp, power, negative, conjugate,
 copysign, equal, not_equal, less, less_equal, isnan, isinf, isfinite, absolute

Quaternion components are stored as doubles.

Comparison operations follow the same lexicographic ordering as tuples.

The unary tests isnan and isinf return true if they would return true for any
individual component; isfinite returns true if it would return true for all

Real types may be cast to quaternions, giving quaternions with zero for all
three imaginary components. Complex types may also be cast to quaternions,
with their single imaginary component becoming the first imaginary component of
the quaternion. Quaternions may not be cast to real or complex types.

Written at the SciPy 2011 sprints, with help from Mark Weibe.

The basic structure of this package is copied from Mark's half-precision
floating point package: - without this
and Mark's help I would have had little chance of figuring out how to do this!