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

Robert Love rblove_lists at comcast.net
Thu Jul 28 22:48:29 EDT 2011


On Jul 28, 2011, at 7:42 AM, Martin Ling wrote:

> On Wed, Jul 27, 2011 at 10:29:08PM -0500, Robert Love wrote:
>> 
>> To use quaternions I find I often need conversion to/from matrices and
>> to/from Euler angles.  Will you add that functionality?
> 
> Yes, I intend to. Note that these conversions are already available in
> the standalone (non-dtype) implementation in imusim.maths.quaternions:
> 
> http://www.imusim.org/docs/api/imusim.maths.quaternions.Quaternion-class.html#setFromEuler
> http://www.imusim.org/docs/api/imusim.maths.quaternions.Quaternion-class.html#toEuler
> http://www.imusim.org/docs/api/imusim.maths.quaternions.Quaternion-class.html#setFromMatrix
> http://www.imusim.org/docs/api/imusim.maths.quaternions.Quaternion-class.html#toMatrix
> 
> I should do a new release though - the Euler methods there only support
> ZYX and ZXY order conversions, my development version supports any order.
> 
>> Will you handle the left versor and right versor versions?
> 
> I don't know what this means. Please enlighten me and I'll be happy to
> try! I thought a 'right versor' was a unit quaternion representing an
> angle of 90 degrees (as in 'right angle') - I don't see what a 'left'
> one would be.
> 

Quaternions have a "handedness" or a sign convention.  The recently departed Space Shuttle used a Left versor convention while most things, including Space Station, use the right versor convention, in their flight software.  Made for frequent confusion.

Let me see if I can illustrate by showing the functions I use for converting a matrix to a quaternion.


def Quaternion_Of(m):
    """
    Returns a quaternion in the right versor sense.
    """

    q = N.zeros(4,float)
    
    q[0] = 0.5*sqrt(1.0 + m[0,0] + m[1,1] + m[2,2])

    q04_inv = 1.0/(4.0*q[0])
    q[1] = (m[1,2] - m[2,1])*q04_inv
    q[2] = (m[2,0] - m[0,2])*q04_inv
    q[3] = (m[0,1] - m[1,0])*q04_inv

    return q



def Quaternion_Of(m):
    """
    Returns a quaternion in the left versor sense.
    """

    q = N.zeros(4,float)
    
    q[0] = 0.5*sqrt(1.0 + m[0,0] + m[1,1] + m[2,2])

    q04_inv = 1.0/(4.0*q[0])
    q[1] = (m[2,1] - m[1,2])*q04_inv
    q[2] = (m[0,2] - m[2,0])*q04_inv
    q[3] = (m[1,0] - m[0,1])*q04_inv

    return q


Or transforming a vector using the different conventions.


def Transform(q,v):
    """
    Returns the vector part of q*vq which transforms v from one
    coordinate system to another.  Right Versor
    """
    u = Q.Vector_Part(q)
    return 2.0*(q[0]*N.cross(v,u) +
                N.dot(v,u)*u +
                (q[0]*q[0] - 0.5)*v)


def Transform(q,v):
    """
    Returns the vector part of q*vq which transforms v from one
    coordinate system to another.  Left Versor
    """
    u = Q.Vector_Part(q)
    return 2.0*(q[0]*N.cross(u,v) +
                N.dot(u,v)*u +
                (q[0]*q[0] - 0.5)*v)








More information about the NumPy-Discussion mailing list