This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)
 Documentation / Mathematica / Add-ons / Standard Packages / Algebra  /


This package implements Hamilton's quaternion algebra. Quaternions have the form where , , , and are real numbers. The symbols , , and are multiplied according to the rules . Quaternions are an extension of the complex numbers, and work much the same except that their multiplication is not commutative. For instance, .
Because of the similarities between quaternions and complex numbers, this package imitates Mathematica's treatment of complex numbers in many ways. To provide a clear distinction between quaternions and complex numbers, all quaternions should be entered using the form Quaternion[

a,b,c,d] where , , , and are real numbers. Only limited support is offered to the symbolic form a


Defining, testing, and transforming quaternions.

  • This loads the package.
  • In[1]:= <<Algebra`Quaternions`

  • Quaternion objects will not automatically simplify to Complex or Real numbers.
  • In[2]:= 3 + 2 I + Quaternion[2,0,-6,0] -3 * Quaternion[1,3,-2,0]


  • FromQuaternion transforms this Quaternion object to an object with head Complex.
  • In[3]:= FromQuaternion[%]


    The primary purpose of this package is to define arithmetic for quaternions. It adds rules to Plus, Minus, Times, Divide, and, most importantly, NonCommutativeMultiply. It is only legal to use Times when multiplying two nonquaternions or a quaternion by a (i.e., real) number. When multiplying two quaternions, one must use NonCommutativeMultiply


  • Addition is done component by component.
  • In[4]:= Quaternion[1,2,3,4] + Quaternion[2,3,4,5]


  • Be sure to use ** rather than * when multiplying quaternions.
  • In[5]:= Quaternion[2,0,-6,3] ** Quaternion[1,3,-2,2]


  • This multiplication is noncommutative.
  • In[6]:= Quaternion[1,3,-2,2] ** Quaternion[2,0,-6,3]


  • Be careful with Divide since 's internal rules quickly turn it into Times

  • In[7]:= Quaternion[1,3,-2,0] / Quaternion[1,0,0,0]


    Although quaternions are whole algebraic objects, just as with complex numbers it is sometimes useful to look at their component parts. These objects form a vector space over the real numbers, with their standard basis being . One may use standard Mathematica techniques for extracting the individual components. However, there are other vector-type parameters such as length and direction which one wants to look at as well.
    The projection of a quaternion onto space, the nonreal part of the quaternion, is called the pure quaternion part

    . This plays a role similar to the pure imaginary part of a complex number.

    Component functions of quaternions.

  • In the conjugate of a quaternion, all the signs of the nonreal components are reversed.
  • In[8]:= q = Conjugate[Quaternion[4,-3,1,-2]]


  • The sign of a quaternion is defined in the same way as the sign of a complex number. It is the "direction" of the quaternion.
  • In[9]:= Sign[q]


  • This returns a quaternion with norm 1 and real part 0.
  • In[10]:= AdjustedSignIJK[q]


  • This gives the standard Euclidean length.
  • In[11]:= Abs[q]


  • A quaternion with a zero I component will still have a nonzero pure quaternion part.
  • In[12]:= AbsIJK[Quaternion[1,0,2,3]]


    For a complex number , is defined by . The package defines in a similar way, using the pure quaternion part of instead of the pure imaginary part of a complex number. Indeed, it makes analogous definitions for the following elementary functions: Exp, Log, Cos, Sin, Tan, Sec, Csc, Cot, ArcCos, ArcSin, ArcTan, ArcSec, ArcCsc, ArcCot, Cosh, Sinh, Tanh, Sech, Csch, Coth, ArcCosh, ArcSinh, ArcTanh, ArcSech, ArcCsch, and ArcCoth


  • The exponential of a quaternion can be quite complicated.
  • In[13]:= Exp[Quaternion[2,3,1,6]]


  • Just as with complex numbers, it is important to beware of branch cuts.
  • In[14]:= Sin[Cos[Quaternion[.3,.1,.5,.5]]]


  • A four-dimensional analog of deMoivre's theorem is used for calculating powers of quaternions.
  • In[15]:= Quaternion[1,2,0,1]^2.5


    The functions so far have been intended to work with quaternions whose components are arbitrary real numbers. Just as the integers and Gaussian integers are interesting subsets of the reals and complexes, there is a special subset of the quaternions called the quaternion integers. This subset is a little broader than one might expect. It includes not only those quaternions that have all integer components, but also those quaternions that have all components being odd multiples of . In this subset there are 24 quaternions that have multiplicative inverses. These are the of the algebra. They correspond roughly to

    in the Gaussian integers.

    Integer quaternion functions.

  • Round for quaternions returns a Quaternion in which either all components are integers, or all components are odd multiples of 1/2.
  • In[16]:= Round[Quaternion[1/2,3,4,5/2]]


  • A quaternion is even if its norm is even.
  • In[17]:= EvenQ[Quaternion[2,3,4,5]]


  • A quaternion integer has components that are either all integers or all halves of odd integers.
  • In[18]:= IntegerQuaternionQ[Quaternion[3/2,1/2, -1/2,5/2]]


    Given a quaternion and a unit quaternion , then and are, respectively, right and left of . It is useful to choose an arbitrary associate and call it the

    associate. This package chooses the associate with the largest real component.

    The associates of an integer quaternion.

  • This is the primary left associate of the quaternion.
  • In[19]:= PrimaryLeftAssociate[Quaternion[1,4,5,3]]


  • The primary right associate is often very similar.
  • In[20]:= PrimaryRightAssociate[Quaternion[1,4,5,3]]


    Quaternion multiplication is noncommutative, so there are two greatest common denominators, one for the left side and one for the right. Since this function depends on the value returned by PrimaryAssociate, the RightGCD and LeftGCD are not unique.

    Some integer division functions.

  • The largest quaternion that divides both of these is Quaternion[1,0,0,0].
  • In[21]:= LeftGCD[Quaternion[1,3,4,1], Quaternion[3,4,1,2]]


  • The LeftGCD and the RightGCD can be quite different.
  • In[22]:= RightGCD[Quaternion[1,3,4,1], Quaternion[3,4,1,2]]


  • Just as with complex numbers, the quaternion Mod works recursively.
  • In[23]:= Mod[Quaternion[-3,4,1,2],3]


  • The LeftGCD and the RightGCD can be quite different.
  • In[24]:= Mod[Quaternion[1,3,4,1], Quaternion[3,4,1,2]]


    PrimeQ has the option GaussianIntegers->True that checks to see if a number is prime with respect to the Gaussian integers. This package extends PrimeQ further to check if a number is prime with respect to the quaternions.
    Lagrange proved that every integer can be expressed as a sum of squares of at most four integers. Therefore, given an integer , there is a quaternion with integer components such that q

    **Conjugate[q]==n. So no integer is prime with respect to the quaternions. In fact, a quaternion integer is prime if and only if its norm is prime in the usual sense.

    An extension of PrimeQ.

  • 19 is a prime with respect to the Gaussian integers.
  • In[25]:= PrimeQ[19, GaussianIntegers->True]


  • It is not a prime with respect to the quaternions. It can be factored into Quaternion[1,4,1,1] and Quaternion[1,-4,-1,-1].
  • In[26]:= PrimeQ[19, Quaternions->True]


  • Quaternion[1,3,0,1] is a prime quaternion.
  • In[27]:= PrimeQ[Quaternion[1,3,0,1], Quaternions->True]