[an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] Skip to the content of the web site.

Quaternions

The C++ classes Quaternion<double>, Quaternion<double>, and Quaternion<float> represent floating-point quaternions of the form z = a + ib + jc + kd. The symbols i, j, and k follow the multiplication rules i2 = j2 = k2 = ijk = -1. Consequently, multiplication is not commutative.

Throughout this document, the variable z is used to represent *this object. The template variable T represents the field of the coefficients.

Index



Constructors

There are two constructors

     Quaternion( T a = 0 );
     Quaternion( T a, T b, T c, T d );

which create the quaternion a + 0i + 0j + 0k and a + ib + jc + kd, respectively.



Constants

There are five static constants defined in each class:

ZERO0
ONE 1
I i
J j
K k

The constants may be accessed through the array UNITS[4] = {ONE, I, J, K}.



Real-Valued Functions

Each of the real-valued member functions has the prototype T f() const; and has a corresponding procedural function T f( const Quaternion<T> );.

real imag_i imag_j imag_k csgn
abs norm abs_imag norm_imag arg

Descriptions of each of the functions follow:

real
Return the real component ℜ(z) = a.
imag_i
Return the imaginary component ℑi(z) = b.
imag_j
Return the imaginary component ℑj(z) = c.
imag_k
Return the imaginary component ℑk(z) = d.
csgn
Return 0 if z = 0, 1 if a ≥ +0, and -1 if a ≤ -0.
abs
Return |z| = (a2 + b2 + c2 + d2)½ unless either component is infinity, in which case, it always returns ∞.
norm
Return |z|2 = a2 + b2 + c2 + d2 unless any component is infinity, in which case, it always returns ∞.
abs_imag
Return |ℑ(z)| = |ib + jc + kd|.
norm_imag
Return |ℑ(z)|2 = b2 + c2 + d2.
arg
Return the argument of the quaternion arg(z) = atan2( |ℑ(z)|, ℜ(z) ).

The function T operator [](int n) const returns the coefficient of the unit UNITS[n] for n = 0, 1, 2, and 3.



Quaternion-Valued Functions

Each of these quaternion-valued member functions has the prototype Quaternion<T> f() const; and has a corresponding procedural function Quaternion<T> f( const Quaternion<T> );.

imag conj signum

Descriptions of each of the functions follow:

imag
Return the quaternion 0 + ib + jc + kd.
cong
Return the quaternion aib + jc + kd.
signum
Return the quaternion z/|z| given z ≠ 0. If z = 0, then z is returned.


Squares and Inverses

Each of the square, square root, and inverse member functions has the prototype Quaternion<T> f() const; and has a corresponding procedural function Quaternion<T> f( const Quaternion<T> );.

sqr sqrt inverse

Descriptions of each of the functions follow:

sqr
Calculate z2.
sqrt
Calculate the square root of z (that is, z½).
inverse
Calculate the inverse of z (that is, z-1).


Rotations

The member function Quaternion<T> rotate( const Quaternion<T> w ) const; and its associated procedural function Quaternion<T> rotate( const Quaternion<T> z, const Quaternion<T> w ) calculates wzw*. If w has unit length and z is an imaginary quaternion, then this member function returns vector z rotated 2 arg(w) radians around the line defined by ℑ(w).



Powers

The power function is overloaded with two prototypes:

Quaternion<T> pow( T ) Quaternion<T> pow( Quaternion<T> )
These calculate z raised to the power of the given argument. There are corresponding procedural functions Quaternion<T> pow( const Quaternion<T>, T ); and Quaternion<T> pow( const Quaternion<T>, const Quaternion<T> );.



Exponential and Logarithmic Functions

Each of the exponential and logarithmic member functions has the prototype Quaternion<T> f() const; and has a corresponding procedural function Quaternion<T> f( const Quaternion<T> );.

exp log log10

Descriptions of each of the functions follow:

exp
Calculate ez.
log
Calculate the natural logarithm ln(z).
log10
Calculate the base-10 logarithm log10(z).


Trigonometric and Hyperbolic Functions (and their Inverses)

Each of the trigonometric, hyperbolic, inverse trigonometric, and inverse hyperbolic member functions has the prototype Quaternion<T> f() const; and has a corresponding procedural function Quaternion<T> f( const Quaternion<T> );.

 sin   cos   tan   sec   csc   cot 
 sinh  cosh  tanh  sech  csch  coth
asin  acos  atan  asec  acsc  acot 
asinh acosh atanh asech acsch acoth

For example, the sin function calculates the result of zz3/3! + z5/5! − z7/7! + z9/9! − z11/11! + ⋅⋅⋅ by using the formula

sin(z) = sin(a) cosh(|ℑ(z)|) + signum( ℑ(z) ) cos(a) sinh(|ℑ(z)|)


Special Functions

Bessel functions of the first kind, Jn(z) are implemented for integer values of n. The prototype of the member function is Quaternion<T> bessel_J( int ) const; and there is the corresponding procedural function Quaternion<T> bessel_J( int, const Quaternion<T>) const;.

Because the coefficients of the Taylor series for Bessel functions of the first kind are real, this function is well defined even for the non-commutative quaternions.



Integer-Value Functions

Each of the integer-valued member functions has the prototype Quaternion<T> f() const; and has a corresponding procedural function Quaternion<T> f( const Quaternion<T> );.

floor ceil

In both cases, the floor and ceiling, respectively, are calculated for each component.



Horner's Rule

The polynomial v0zn − 1 + v1zn − 2 + v2zn − 3 + ⋅⋅⋅ vn − 3z2 + vn − 2z + vn − 1 may be calculated efficiently using Horner's rule. The array v of n entries may be of type T *. The lack of commutativity dictates that the polynomial is not well defined from quaternionic coefficients, hence the coefficients are restricted to real values.

     Quaternion<T> horner( T * v, unsigned int n );

The Newton polynomial with offsets:       v0(z − cn − 1)(z − cn − 2)⋅⋅⋅(z − c3)(z − c2)(z − c1) +
      v1(z − cn − 1)(z − cn − 2)⋅⋅⋅(z − c3)(z − c2) +
      v2(z − cn − 1)(z − cn − 2)⋅⋅⋅(z − c3) + ⋅⋅⋅ +
  vn − 3(z − cn − 1)(z − cn − 2) +
  vn − 2(z − cn − 1) +
  vn − 1 may also be calculated efficiently using Horner's rule. The arrays v and c of n entries must be of type T *.

     Quaternion<T> horner( T * v, T * c, unsigned int n );

Similarly, due to commutativity, the coefficients and the offsets must be restricted to real values.

Corresponding to each of this functions is a procedural function which takes the variable z as a first argument.



Binary Arithmetic Operators

All binary arithmetic operators operator ⋅ have the following prototypes:

operator ⋅ ( const Quaternion<T> &, const Quaternion<T> & ), operator ⋅ ( T, const Quaternion<T> & ), and operator ⋅ ( const Quaternion<T> &, T ). The standard operations are:

+ - * /

Note that z/w is defined as zw-1.



Unary Arithmetic Operators

The unary arithmetic operator operator - has the prototype operator - ( const Quaternion<T> & ) and returns the negative of the quaternion.



Assignment Operators

The assignment operators operator ⋅ have the prototypes operator ⋅ ( const Quaternion<T> & ) and operator ⋅ ( T ) and appropriately modifies and returns this quaternion. The operations are:

= += -= *= /=


Auto Increment and Auto Decrement Operators

The auto increment and auto decrement operators work on the real part of the quaternion.



Binary Boolean Operators

All binary Boolean operators have the following prototypes:

operator ⋅ ( const Quaternion<T> &, const Quaternion<T> & ), operator ⋅ ( T, const Quaternion<T> & ), and operator ⋅ ( const Quaternion<T> &, T ). The standard operations are:

== !=

Descriptions of each of the functions follow:

operator ==
Returns true if all components return true under ==.
operator !=
Returns true if any components return true under !=.


Query Functions

Each of the following member functions has the prototype bool f() const; and returns a value based on the components of this quaternion.

is_imaginary is_inf is_nan is_neg_inf
is_pos_inf is_real is_real_inf is_zero

Descriptions of each of the functions follow:

is_imaginary
Returns true if this quaternion has a zero real component (of the form 0 + ib + jc + kd) and false otherwise.
is_inf
Returns true if any component of this is one of either +∞ or -∞ and false otherwise.
is_nan
Returns true if any component of this is NaN and false otherwise.
is_neg_inf
Returns true if this quaternion is -∞ + 0i + 0j + 0k and false otherwise.
is_pos_inf
Returns true if this quaternion is ∞ + 0i + 0j + 0k and false otherwise.
is_real
Returns true if this quaternion has a zero imaginary component (of the form a + 0i + 0j + 0k) and false otherwise.
is_real_inf
Returns true if this quaternion is either +∞ + 0i + 0j + 0k or -∞ + 0i + 0j + 0k and false otherwise.
is_zero
Returns true if this quaternion is 0 + 0i + 0j + 0k and false otherwise.


Static Factory Functions

Each of the following static factory functions has the prototype static Quaternion<T> f() const; and returns a random quaternion according to the following definitions:

random
r1 + ir2 + jr3 + kr4
random_real
r1 + 0i + 0j + 0k
random_imag
0 + ir1 + jr3 + kr4

where rk is a random real value.



Stream Operators

The stream operators have been overloaded to print and read quaternions.