basic_quaternion
namespace nin {
template <std::floating_point T>
struct basic_quaternion;
using quaternion = basic_quaternion<double>;
}
A Hamilton quaternion q = w + xi⃗ + yj⃗ + zk⃗ with full arithmetic support.
All functions are constexpr and noexcept.
| This page documents the bare mathematical quaternion type. For rotation and orientation types, see basic_rotation and basic_orientation_qty. |
The type alias quaternion is provided for basic_quaternion<double>.
Member functions
(Constructor)
(Constructor)
|
(1) |
|
(2) |
|
(3) |
-
(1) Default constructor. All components are zero.
-
(2) Explicit converting constructor from a different floating-point type.
-
(3) Component constructor.
import ninbot;
int main() {
nin::quaternion q1; // (w, x, y, z) = (0, 0, 0, 0)
}
conjugate
conjugates the quaternion in place
conjugate
conjugates the quaternion in place
|
(1) |
Negates the imaginary components (x, y, z).
Returns a reference to *this to allow chaining.
import ninbot;
int main() {
nin::quaternion q1c = nin::quaternion{1, 2, -3, -4}.conjugate(); // q1c = (1, -2, 3, 4)
}
normalize
normalises the quaternion in place
normalize
normalises the quaternion in place
|
(1) |
Divides every component by norm(*this).
Returns a reference to *this to allow chaining.
import ninbot;
int main() {
nin::quaternion q1{1, 1, 1, 1};
q1.normalize(); // q1 = (0.5, 0.5, 0.5, 0.5)
}
invert
inverts the quaternion in place
invert
inverts the quaternion in place
|
(1) |
Conjugates and divides by the squared norm.
Returns a reference to *this to allow chaining.
import ninbot;
int main() {
nin::quaternion q1 {0, 1, 0, 0};
q1.invert(); // q1 = (0, -1, 0, 0)
}
operator+, operator- (unary)
unary plus and negation
operator+, operator- (unary)
unary plus and negation
|
(1) |
|
(2) |
-
(1) Returns a copy of the quaternion.
-
(2) Returns the negation of the quaternion (all components negated).
import ninbot;
int main() {
nin::quaternion q0 = {1, 2, 3, 4};
nin::quaternion q1 = +q0; // q1 = (1, 2, 3, 4)
nin::quaternion q2 = -q0; // q2 = (-1, -2, -3, -4)
}
operator+=, operator-=, operator*=, operator/=
compound assignment with another quaternion
operator+=, operator-=, operator*=, operator/=
compound assignment with another quaternion
|
(1) |
|
(2) |
|
(3) |
|
(4) |
-
(4) The result is equivalent to
*this *= inv(q_rhs).
import ninbot;
int main() {
nin::quaternion q1 = {1, 2, 3, 4};
nin::quaternion q2 = {2, 5, 8, 11};
q1 += q2; // q1 = (3, 7, 11, 15)
q2 *= q2; // q2 = (-206, 20, 32, 44)
}
operator+=, operator-=, operator*=, operator/= (scalar)
compound assignment with a scalar
operator+=, operator-=, operator*=, operator/= (scalar)
compound assignment with a scalar
|
(1) |
|
(2) |
|
(3) |
|
(4) |
-
(1) (2) Addition and subtraction for the real part only (component
w). -
(3) (4) Multiplication and division apply to every component of the quaternion.
import ninbot;
int main() {
nin::quaternion q = {1, 2, 3, 4};
nin::quaternion q1 = q + 5; // q1 = (6, 2, 3, 4)
nin::quaternion q2 = q * 5; // q2 = (5, 10, 15, 20)
}
Non-member functions
norm, norm2
norm and squared norm
norm, norm2
norm and squared norm
|
(1) |
|
(2) |
import ninbot;
int main() {
nin::quaternion q = {1, 1, 1, 1};
double value1 = norm(q); // value1 = 2
double value2 = norm2(q); // value2 = 4
}
conj, inv
non-member conjugate and inverse
conj, inv
non-member conjugate and inverse
|
(1) |
|
(2) |
import ninbot;
int main() {
nin::quaternion q = {1, 1, 0, 0};
nin::quaternion qi = inv(q);
nin::quaternion qc = conj(q);
}
operator+, operator-, operator*, operator/
Mixed quaternion-scalar binary arithmetic
operator+, operator-, operator*, operator/
Mixed quaternion-scalar binary arithmetic
|
(1) |
|
(2) |
|
(3) |
|
(4) |
-
(1) (2) (3) At least one of the parameters must be a quaternion. The other can be either a quaternion or a scalar.
-
(4)
lhsmust be a quaternion.rhscan be a quaternion, in which case the result is equivalent tolhs * inv(rhs), or a scalar.
import ninbot;
int main() {
nin::quaternion q1 = {1, 2, -1, -2};
nin::quaternion q2 = {0, 1, 1, 1};
nin::quaternion q3 = q1 - q2 + 10; // q3 = (11, 1, -2, -3)
nin::quaternion q4 = q1 * q2 * conj(q1) / norm2(q1); // q4 = (0, -1, -1.4, 0.2)
}
real, imag
real and imaginary projections
real, imag
real and imaginary projections
|
(1) |
|
(2) |
|
(3) |
-
(1)-(2) Returns the scalar part
w. -
(3) Returns the vector part (x, y, z).
import ninbot;
int main() {
nin::quaternion q1;
real(q1) = 1;
auto [x, y, z] = imag(q1);
}
std::formatter<>
STL overload for the std::format and std::print families.
std::formatter<>
STL overload for the std::format and std::print families.
|
(1) |
Adds a specialization of std::formatter that formats the
four quaternion components in the order w, x, y, z.
The <format-spec> syntax is
[<float-spec>] [ "|" <separator_w> [ "|" <separator_xyz>] ]
with the following rules:
-
<float-spec>— anything before the first|is forwarded verbatim to the STL formatter for floating points and applied to each of the four components. -
Parentheses — when the format-spec contains no separator specifiers, the output is wrapped in parentheses.
-
Separators — the text after the first
|specifies the separator placed between every component. If a second|is present, the text of the separators betweenwandxbecomes<separator_w>and the text of the separators of the vector components becomes<separator_xyz>. -
Escaping — inside the separator portion,
\\|produces a literal|and\\\\produces a literal backslash. -
Defaults — when no
|is given,<separator_w>defaults to"; "and<separator_xyz>to", ".
import ninbot;
#include <print>
int main() {
nin::quaternion q = {1, 2, 3, 4};
std::print("{}", q); // "(1; 2, 3, 4)"
std::print("{:.2f}", q); // "(1.00; 2.00, 3.00, 4.00)"
std::print("{:|, }", q); // "1, 2, 3, 4"
std::print("({:|\\| | })", q); // "(1| 2 3 4)"
}