Quaternions in Python

François Pinard pinard at iro.umontreal.ca
Fri Oct 5 20:47:43 EDT 2001


[David Feustel]

> Is there a Python module that does quaternion math?

Hi, David.  None that I know, but it might be not too difficult writing
one, given you do not ask for too heavy computations.  I mean: you could
generalize some formulas (below) using integers up to reals using converging
bounds, but this might require a lot of ccomputation.  While David Gillespie
was developing his nice `Calc' package, we corresponded a great deal on
a lot of topics, and he chose to include some of our discussions in the
documentation.  The quote below comes from the "Examples of Rewrite Rules" in
the Emacs Calc manual.  Maybe this could help you at getting started quickly?

If you do something about Python quaternions, please share your code! :-)

---------------------------------------------------------------------->
   The following rule set, contributed by Francois Pinard, implements
"quaternions", a generalization of the concept of complex numbers.
Quaternions have four components, and are here represented by function
calls `quat(W, [X, Y, Z])' with "real part" W and the three "imaginary"
parts collected into a vector.  Various arithmetical operations on
quaternions are supported.  To use these rules, either add them to
`EvalRules', or create a command based on `a r' for simplifying
quaternion formulas.  A convenient way to enter quaternions would be a
command defined by a keyboard macro containing: `' quat($$$$, [$$$, $$,
$]) <RET>'.

     [ quat(w, x, y, z) := quat(w, [x, y, z]),
       quat(w, [0, 0, 0]) := w,
       abs(quat(w, v)) := hypot(w, v),
       -quat(w, v) := quat(-w, -v),
       r + quat(w, v) := quat(r + w, v) :: real(r),
       r - quat(w, v) := quat(r - w, -v) :: real(r),
       quat(w1, v1) + quat(w2, v2) := quat(w1 + w2, v1 + v2),
       r * quat(w, v) := quat(r * w, r * v) :: real(r),
       plain(quat(w1, v1) * quat(w2, v2))
          := quat(w1 * w2 - v1 * v2, w1 * v2 + w2 * v1 + cross(v1, v2)),
       quat(w1, v1) / r := quat(w1 / r, v1 / r) :: real(r),
       z / quat(w, v) := z * quatinv(quat(w, v)),
       quatinv(quat(w, v)) := quat(w, -v) / (w^2 + v^2),
       quatsqr(quat(w, v)) := quat(w^2 - v^2, 2 * w * v),
       quat(w, v)^k := quatsqr(quat(w, v)^(k / 2))
                    :: integer(k) :: k > 0 :: k % 2 = 0,
       quat(w, v)^k := quatsqr(quat(w, v)^((k - 1) / 2)) * quat(w, v)
                    :: integer(k) :: k > 2,
       quat(w, v)^-k := quatinv(quat(w, v)^k) :: integer(k) :: k > 0 ]

   Quaternions, like matrices, have non-commutative multiplication.  In
other words, `q1 * q2 = q2 * q1' is not necessarily true if `q1' and
`q2' are `quat' forms.  The `quat*quat' rule above uses `plain' to
prevent Calc from rearranging the product.  It may also be wise to add
the line `[quat(), matrix]' to the `Decls' matrix, to ensure that
Calc's other algebraic operations will not rearrange a quaternion
product.  *Note Declarations::.

   These rules also accept a four-argument `quat' form, converting it
to the preferred form in the first rule.  If you would rather see
results in the four-argument form, just append the two items `phase(2),
quat(w, [x, y, z]) := quat(w, x, y, z)' to the end of the rule set.
(But remember that multi-phase rule sets don't work in `EvalRules'.)
----------------------------------------------------------------------<

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard




More information about the Python-list mailing list