# Bug in Mathematica 9 (Quaternion)

The code:

<< Quaternions;
Exp[Quaternion[1, 0, 0, 0]]


produces an error:

During evaluation of In:= Power::infy: Infinite expression 1/0 encountered. >>

During evaluation of In:= Power::infy: Infinite expression 1/0 encountered. >>

During evaluation of In:= Power::infy: Infinite expression 1/0 encountered. >>

During evaluation of In:= General::stop: Further output of Power::infy will be suppressed during this calculation. >>

During evaluation of In:= $RecursionLimit::reclim: Recursion depth of 1024 exceeded. >> During evaluation of In:=$RecursionLimit::reclim: Recursion depth of 1024 exceeded. >>

During evaluation of In:= $RecursionLimit::reclim: Recursion depth of 1024 exceeded. >> During evaluation of In:= General::stop: Further output of$RecursionLimit::reclim will be suppressed during this calculation. >>

During evaluation of In:= \$IterationLimit::itlim: Iteration limit of 4096 exceeded. >>

During evaluation of In:= Infinity::indet: Indeterminate expression 0 ComplexInfinity encountered. >>


I expect it to output E instead.

Quaternion exponent formula is

In:= ScalarQ[x_] = True;
Exp[Quaternion[a, b, c, d]]

Out= Quaternion[E^a Cos[Sqrt[b^2 + c^2 + d^2]], (
b E^a Sin[Sqrt[b^2 + c^2 + d^2]])/Sqrt[b^2 + c^2 + d^2], (
c E^a Sin[Sqrt[b^2 + c^2 + d^2]])/Sqrt[b^2 + c^2 + d^2], (
d E^a Sin[Sqrt[b^2 + c^2 + d^2]])/Sqrt[b^2 + c^2 + d^2]]


but quaternion cannot substitute directly in case AbsIJK[x] == 0.

Is there a fix in Mathematica 10? Is there any reason for it to cause error?

• It still present in 10.x. – user18792 Apr 28 '16 at 7:27
• Version 5.2 (the version before the add-on packages were completely overhauled) does not have a problem evaluating Exp[Quaternion[1, 0, 0, 0]]. I suspect something done during the transition to version 6 broke it. – J. M. will be back soon May 6 '16 at 0:04

## An attempt to fix

Go to Mathematica installation path, find the Quaternion.m package file. In line 359, you should find the original definition:

Quaternion /:
Sign[a:Quaternion[__?ScalarQ]]:= a / Abs[a]


change the definition to this:

Quaternion /:
Sign[a:Quaternion[__?ScalarQ]]:= If[Abs[a]==0, 0, a / Abs[a]]


## My investigations

First I used TracePrint on Mathematica 10.4.1, and found something like Quaternion[0, 0, 0, 0]/Abs[Quaternion[0, 0, 0, 0]] appears before the error messages, and Sign function was simply defined as a/Abs[a], that's why I changed that function.

J.M. mentioned that in 5.2 this can return a result. I tested it and also found it true. But when I see the trace print, I found AdjustedSignIJK runs into a case where it commented: (* This seems very wrong. *), and returns I for sign. However in the following calculations, a 0 from other place multiples with this I, so accidentally it becomes correct. In 6.0, the package was modified. From line 387 to 390:

(* fix bug 66775 -- charlesp *)

SignIJK[Quaternion[a_?ScalarQ, b_?ScalarQ, c_?ScalarQ, d_?ScalarQ]] :=
Sign[Quaternion[0, b, c, d]]


Looks like an attempt to fix this bug(at least it doesn't run into that case anymore), that's where it made the program calls Sign. Since Sign did not consider case 0`, the new bug happened.

This is only my shallow observation. I am not sure if this can break anything else.