Quaternion¶
A quaternion is a number that can be expressed in the form a + bi + cj + dk
, where a
, b
, c
, and d
are real numbers, and i
, j
, and k
are imaginary units that satisfy the following equations:
The set of quaternions is denoted by \(\mathbb{H}\). The quaternion is a non-commutative number system, which means that the order of multiplication matters just like matricies.
Quaternion has a real part and three imaginary parts. Sometimes the imaginary parts are represented as a vector.
Conjugate & Norm¶
The norm of a quaternion is defined as the square of the self with conjugated self. The conjugate of a quaternion is the same as the original number, but the sign of the imaginary part is flipped.
Addition & Subtraction¶
Quaternion addition & subtraction are defined as follows:
Multiplication¶
Quaternion addition & subtraction are the same as complex numbers. Quaternion multiplication is defined as follows:
In matrix form, the multiplication can be represented as follows:
Inverse & Division¶
The inverse of a quaternion is defined as the conjugate of the quaternion divided by the norm of the quaternion.
Quaternion Class¶
// initializer, 1+2i+3j+4k
Quaternion<float> q1{1.f, 2.f, 3.f, 4.f}; // Re, i, j, k
Quaternion<float> q2{{1.f, 2.f, 3.f, 4.f}};
Quaternion<float> q3{1.f, Vectorf<3>{{2.f, 3.f, 4.f}}}; // Re, Im(Vec3)
// type alias
Quaternionf q4 = q1;
// cast to other object (4 x 1)
Vectorf<4> vec41 = q1.cast2Vector();
Matrixf<4,1> mat41 = q1.cast2Matrix();
q1 = vec41.cast2Quaternion();
// accessor
float re = q1.Re();
Vectorf<3> im = q1.Im();
q1.Re() = re;
q1.Im() = im;
float w = q1.w();
float x = q1.x();
float y = q1.y();
float z = q1.z();
q1.W() = w;
q1.X() = x;
q1.Y() = y;
q1.Z() = z;
// conjugate, norm
Quaternionf q5 = q1.conjugated();
float norm = q1.norm();
// inverse
Quaternionf q6 = q1.inversed();
// operators
Quaternionf q7 = q1 + q2;
Quaternionf q8 = q1 - q2;
Quaternionf q9 = q1 * q2; // quaternion multiplication
Quaternionf q10 = q1 / q2; // quaternion division
// result type casting
Vectorf<4> vec41 = SquareMatrixf<4>{} * q1; // matrix multiplication
// to multiplication matrix
SquareMatrixf<4> mat4 = q1.toMulMatrix();
// | w, -V^T |
// | v, wI + [V]_x |