[Numpy-discussion] Quaternion dtype for NumPy - initial implementation available

Anne Archibald aarchiba at physics.mcgill.ca
Sat Jul 16 20:16:44 EDT 2011


What a useful package! Apart from helping all the people who know they
need quaternions, this package removes one major family of use cases
for vectorized small-matrix operations, namely, 3D rotations.
Quaternions are the canonical way to represent orientation and
rotation in three dimensions, and their multiplication gives (with
some fiddling) composition of rotations. The next interesting question
is, how well does scipy.interpolate deal with them? For really good
rotational paths I seem to recall you want specialized splines, but
simply interpolating in the quaternion domain is not a bad quick and
dirty approach.

Anne (now awaiting octonions, though I've never heard of a practical
use for them)

On 16 July 2011 10:50, Martin Ling <martin-numpy at earth.li> wrote:
> Hi all,
>
> I have just pushed a package to GitHub which adds a quaternion dtype to
> NumPy: https://github.com/martinling/numpy_quaternion
>
> Some backstory: on Wednesday I gave a talk at SciPy 2011 about an
> inertial sensing simulation package I have been working on
> (http://www.imusim.org/). One component I suggested might be reusable
> from that code was the quaternion math implementation, written in
> Cython. One of its features is a wrapper class for Nx4 NumPy arrays that
> supports efficient operations using arrays of quaternion values.
>
> Travis Oliphant suggested that a quaternion dtype would be a better
> solution, and got me talking to Mark Weibe about this. With Mark's help
> I completed this initial version at yesterday's sprint session.
>
> Incidentally, how to do something like this isn't well documented and I
> would have had little hope without both Mark's in-person help and his
> previous code (for adding a half-precision float dtype) to refer to. I
> don't know what the consensus is about whether people writing custom
> dtypes is a desirable thing, but if it is then the process needs to be
> made a lot easier. That said, the fact this is doable without patching
> the numpy core at all is really, really nice.
>
> Example usage:
>
>  >>> 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. The package could be extended
> to support e.g. qfloat, qdouble, qlongdouble
>
> Comparison operations follow the same lexicographic ordering as tuples.
>
> The unary tests isnan, isinf and isfinite return true if they would
> return true for any individual component.
>
> 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.
>
> Comments very welcome. This is my first attempt at NumPy hacking :-)
>
>
> Martin
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>



More information about the NumPy-Discussion mailing list