digits to display
0 3 6 9 12 all
postfix-syntax
quaternion calculator
exp notation
input
a
b
c
d
é
f
g
stack size real part himag part iimag part jimag part k

Version of Friday 16 October 2015.
Dave Barber's other pages.
Thanks to Bob Smith for review.

This is a simple postfix calculator for the quaternions over the real numbers. Implemented in JavaScript, it stores quaternions in a stack, keeping the seven most recent entries visible in the lower half of the display, one quaternion per row. Because of the stack configuration, there is no need to store information about pending operations. Further, parentheses and the equal button are omitted; instead are enter (or push in stack parlance) and pop buttons. This postfix syntax is also called reverse Polish notation, often abbreviated RPN.

Although a quaternion contains four real numbers, only the first of them (here named h) is interpreted as real; the three remaining (i, j and k) are coefficients of imaginary values respectively named i, j and k. The quaternions can be understood as an extension of the complex numbers with two extra imaginary parts. What makes quaternions distinctive is that multiplication is not commutative:

i × j = +k j × k = +i k × i = +j
j × i = −k k × j = −i i × k = −j
i2 = j2 = k2 = −1

See also the complex-number version of this calculator.


Input. To enter a number, type the parts of it into one or more of the boxes on the input line; a box is presumed zero if blank. To select an input box, click on it with the mouse; but without any click, input will default to the real box. Also, the tab key will cycle hijkother fields, where "other fields" depends on the browser; shift-tab goes the opposite direction.

       
        
        
     
     
input

The digit buttons have the obvious equivalents on the keyboard, as do the minus sign and decimal point. Also, the e key substitutes for the ent exp (enter exponent) button. If you use the mouse or the arrow keys to move the cursor within an input box, the web-page buttons may not recognize the change, and the next input character may appear at a surprising location.

Lacking keyboard equivalents are the bottom-row buttons to clear one input box, and to backspace one character.

The minimum format for a number is simply a string of digits, such as 2436. It can start with a minus sign, as −34. That is optionally followed by a point and more digits, for instance 12.54. Further, an exponent may be suffixed, and it begins with the letter e, has an optional minus sign, and then some digits. For example, 12.54e−77 means 12.54 times 10 to the −77 power. Here are some typical numbers:

23432e161722e-4543.21386.32e9232e-18
-43-29.683-897e-4-114.381-14.98e11-246785e-26

Once you have put your desired numbers into the input boxes, click enter, and your quaternion will appear in register a. The previous contents (if any) of register a will move to b, what was in b will go to c, etc. All four input boxes will be cleared.

If any of the four components of your input number was illegally formatted, register a will show what the calculator could salvage from it, with a result of NaN indicating complete failure. The following example shows a correct quaternion, with the imaginary part i defaulting to zero:

input

Two special buttons, pi enter and e enter, move pi ≈ 3.141593 and e ≈ 2.718282 directly to register a.

As a result of many enters or other operations, there may be more than seven numbers stored in the calculator, with the stack count box in the lower left telling how many. The maximum stack size is much greater than seven.

Many operations below perform the implicit enter. With these, if you have started entering a number into the input boxes, but not yet clicked the enter button, the number will be moved into register a before the operation is commenced, as though the enter button had been used. Any numbers that were already in the stack will be moved down to make room.


These controls pertain to the stack:

delete the entire stack (but not the input area)
delete the contents of a, and move other stack contents up to fill the gap
copy a to b, and move other stack contents down to make room
swap a and b swap other
registers similarly
swap b and c
stack size total number of quaternions in the stack; at most seven are visible

The dup button performs the implicit enter, placing two copies of the input number into the stack. Other controls:

choose how many digits will be shown in the results of calculations … digits to display
0 3 6 9 12 all
… but full precision (equivalent to about 15 decimal digits) is maintained internally
change one part of register a to zero exp notation when checked, always display results in exponential form
 
rearrange components of register a  
see below    

Let symbol g stand for √ ( i2 + j2 + k2 ), which is the magnitude of the aggregated imaginary parts. Then the ijk → i button (which performs the implicit enter) alters register a as follows:

Informally, the ijk → i button rotates the imaginary part of the quaternion so as to make it "look like" a complex number.

The mathematical operations below perform the implicit enter. Four operations replace the contents of stack register a with a new value. Twelve others consume registers a and b and leave the result in a, afterward moving the contents of c to b, d to c, etc. Two-argument operations that are not commutative come in at least two varieties; division gets four, and here uses the notation of reciprocation and multiplication to be precise about what is being done.

The calculator provides the same precision, usually about 15 decimal digits, as the underlying JavaScript operations.

two inputs, one output divide divide two inputs, one output
divide divide
multiply multiply
find anticommutative part find anticommutative part
find commutative part find reciprocal one input, one output
two inputs, one output subtract subtract two inputs, one output
add find absolute value one input, one output
one input, one output negate conjugate

The product a × b is the sum of a commutative part a ⊗′ b and an anticommutative part a ⊗″ b. Specifically:

a×b = a ⊗′ b + a ⊗″ b
b×a = a ⊗′ ba ⊗″ b

where:

a⊗′b = (a × b + b × a) ÷ 2 = + b ⊗′ a
a⊗″b = (a × bb × a) ÷ 2 = − b ⊗″ a

Mnemonic: The symbol for the anticommutative part has two primes because it needs two calculator buttons for the two different sequences of inputs. On the other hand, the commutative version has only one prime because it needs only one button.

Operator ⊗″ resembles the commutator as well as the standard cross product. Meanwhile, operator ⊗′ suggests the anticommutator.


The next operations are all one-input-one-output, replacing the contents of register a with something new. The implicit enter is performed. Angles are reckoned in radians, not degrees.

The definitions are based on those for complex numbers given in chapter four of Abramowitz and Stegun. In this adaptation, the output of each function is defined so that it multiplicatively commutes with the input; for instance sin (q) × q will equal q × sin (q).

circular sine principal value of inverse sine circular
cosine principal value of inverse cosine
tangent principal value of inverse tangent
hyperbolic sine principal value of inverse sine hyperbolic
cosine principal value of inverse cosine
tangent principal value of inverse tangent
exponential principal value of natural logarithm
principal value of cube root principal value of square root

The button legends read inv… instead of arc… (as on many calculators) because for the hyperbolic functions, there is no arc to be had: the function takes an area as input. The same principle applies to the circular functions, but with them the length of a pertinent arc happens to be numerically equal to the area serving as input, making the frequently-written arc sin not definitively incorrect.

Some of the functions above are not defined for certain quaternion inputs (see branch cuts). Again, g stands for √ ( i2 + j2 + k2 ).

  not defined when
g = 0 and abs (h) > 1
g = 0 and abs (h) > 1
g > 1 and h = 0
g > 1 and h = 0
g = 0 and h < 1
g = 0 and abs (h) > 1
g = 0 and h = 0

As a design decision, the calculator does not give an answer when the logarithm or square root of a negative real number is requested. In these cases there are not only infinitely many solutions, but they further form a dense uncountable set, and no clear criterion is apparent for selecting one of them as the principal value.


Comments.

Two separate multiplication buttons were provided to remind the user that multiplication is not commutative. For consistency, subtraction then required two buttons, and for thoroughness division got four.

Powers and roots are not supported because of the wealth of multiple values arising from not only the logarithm but also the choice of sequence in multiplication:

Conversions to and from Euler angles were not included because there are no clear standards for (1) determining in what sequence the rotations around the respective axes (x, y, z) are to be performed; and (2) determing whether each rotation will be clockwise or counterclockwise.

Following the tradition of RPN calculators, most notably those of Hewlett-Packard, the enter button is wider than the others. However, stack manipulation on this calculator does differ somewhat from HP's versions of RPN. For instance, there is no t register whose contents are replicated when the pop (HP calls it drop) button is pushed.

The postfix-with-stack design means that an enhanced version of this calculator could introduce operations with any number of inputs and any number of outputs — such flexibility is fully exploited in languages like Adobe PostScript. With an infix-and-parentheses syntax by contrast, it is awkward for an operation to have more than two inputs or one output.

Keyboards of pocket calculators are, by reason of size, limited to about fifty buttons. As a result, many functions are accessed by way of shift buttons (often called f, g, 2nd, or inv). On the other hand a web page, when viewed on a full-size computer screen, has plenty of room, so no shift buttons are used here. If this calculator were optimized for small-screen handheld devices, shift buttons might become necessary, and fewer than seven registers would likely be displayed.

The entire source code is housed in this one file. Users may copy, modify, and republish as desired.


Author's candid, possibly offensive, opinion:

Commutative multiplication is for wimps.