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

Martin Ling martin-numpy at earth.li
Sun Jul 31 09:21:38 EDT 2011


Hi Hans,

Sorry, that is actually what I implemented, I just documented it
iincorrectly. I have just pushed an update to the README. Thanks for
pointing this out!


Martin

On Sun, Jul 31, 2011 at 07:57:58AM +0200, Hans Meine wrote:
> 
> Hi Martin,
> 
> I think it would be more useful if isfinite returned true if *all* elements were finite.  (Opposite of isnan and isinf.)
> 
> HTH,
>   Hans
> 
> PS: did not check the complex dtype, hopefully that one's no different. 
> 
> (The above has been typed using a small on-screen keyboard, which may account for any typos,  briefness or bad formatting.)
> 
> Am 16.07.2011 um 16:50 schrieb Martin Ling <martin-numpy at earth.li>:
> 
> > 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