# Quaternions in Python

Carl Banks idot at vt.edu
Mon Oct 8 23:40:04 CEST 2001

```Kevin Rodgers <krodgers at ryanaero.com> wrote:
>
> 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.

Not necessarily.  The original question was whether there is a
quarternion arithmetic package in Python.  He did not say he wanted to
use the quarternions to represent rotations, and although I cannot
name any off hand, I am sure there are other uses for quarternions.

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

Equivalent is a pretty strong term.  I would say that these are
alternate ways of achieving some result, not equivalent ways.

[SNIP]

> (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.

Incorrect.  Only a normalized quarternion (absolute value = 1) can be
converted to a direction cosine matrix.  In general, a quarternion
need not be normalized.

> (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).

If there are constraints between the "other two" columns, then the
components of those columns are not all independent parameters.

> (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.

This is correct.  Why do you think quarternions are often used for
numerical integration, but direction cosines very rarely?  It's
because quarternions are much more suited to numerical integration
than is the cosine matrix.

IOW, if you want to "simulate" a quarternion in Python using a 3x3
real matrix with NumPy, you must maintain all of the constraints you
would have had to have maintained if you had used a matrix in the
first place.  The quarternion might have never existed.

> (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!

Agreed, which is why it is desirable to have a quarternion module in
Python.  (Euler angle module can stay in Perl.)  The complex 2x2
matrix gimmick looks pretty good: perhaps NumPy will include a
front-end for it?

--
CARL BANKS

```