Quaternions in Python

Kevin Rodgers krodgers at ryanaero.com
Mon Oct 8 16:22:14 EDT 2001


Carl Banks <idot at vt.edu> wrote in message news:<tq6np9.fg.ln at 10.0.0.1>...
> Paul Rubin <phr-n2001 at nightsong.com> wrote:
> > Erik Max Francis <max at alcyone.com> writes:
> >> > I think there's a simple way to represent quaternions as 3x3 matrices.
> >> > So you could probably just store them that way and use NumPy for the
> >> > arithmetic.
> >> 
> >> ... which rather defeats the purpose for using quaternions in the first
> >> place.
> > 
> > Nah.  I mean, if efficiency was a big issue, you wouldn't be using an
> > interpreted language in the first place.  What's left, if you have a
> > good matrix package, is something like if your language had complex
> > numbers but didn't have reals.  You can still represent the reals as
> > complexes.  For that matter, lots of languages have had reals but no
> > integers.  It hasn't been that bad a problem.
> 
> You severely underestimate the difficulty of doing this.  While any
> quarternion can be represented as a 3x3 matrix, not every 3x3 matrix
> is a quarternion.  And keeping your matrix in "quarternion form" when
> you do arithmetic is not trivial.
> 
> To wit, a quarternion has 4 components.  A 3x3 matrix has 9
> components.  Therefore, when doing quarternion arithmetic using a 3x3
> matrix, you must be sure the resulting matrix satisfies 5 constraints.
> And, IIRC, the constraints are not simple.
> 
> (Contrast this to using a complex as a real.  Here, only one very
> simple constraint, namely Im(z)==0, must be maintained.)


All of you are missing some aspect of the situation:

(1) The problem is how to represent an arbitrary 3-axis coordinate
rotation in a mathematically robust way.

(2) Quaternions and direction cosine matrices (the 3x3 matrix referred
to above) are two equivalent ways of doing this.

(3) Euler angles are NOT mathematically robust.  Euler angles are a
set of three rotations to get from coordinate system A to coordinate
system B.  The rotations are done in a specified order. The problem is
that, no matter what the order in which you do the three rotations,
there is always a value for one angle of rotation that yields infinite
values of the other two angles.  For example, aircraft Euler angles
are normally represented as yaw, pitch, and roll, in that order. 
Given that order of rotation, it is pretty straightforward to show
that when the pitch angle is straight up or straight down, the values
of yaw and roll are undefined.  In a physical gimbal system, this is
known as gimbal lock.

(4) For physical gimbals, gimbal lock is resolved by adding a fourth
gimbal.  For mathematical techniques, both the four-element quaternion
and the nine-element direction cosine matrix resolve the gimbal lock
phenomenon.

(5) A rotation quaternion can always be converted into an equivalent
direction cosine matrix, and vice versa.  Note that not all 3x3
matrices are direction cosine matrices, however.

(6) The number of constraints on the 3x3 matrix are less than
described.  A direction cosine matrix has only six independent
parameters, not nine.  One column of the matrix can be always be
recovered by the cross product of the other two.  There is an
orthogonality constraint between the two independent columns, however,
and this is somewhat more complex than the quaternion normalization
constraint (which is just that the root-sum-square of the quaternion
components be unity).

(7) Lots of application areas use both quaternions and direction
cosine matrices. For inertial navigation applications, quaternions are
often used in the numerical integration of the differential equations
of motion, then immediately converted to direction cosine form.  This
is because there are a lot of operations that are computationally more
efficient to perform using the direction cosine form of the
transformation.

(8) Use whatever combination of quaternions and direction cosine
matrices your problem needs.  For lots of practical problems, you can
even get by with Euler angles!

Kevin Rodgers
"This one goes up to eleven."  -- Nigel Tufnel



More information about the Python-list mailing list