pub struct Quaternion {
pub scalar: f64,
pub vector: Cartesian<3>,
}Expand description
Extended complex number.
A quaternion has a real value and three complex values, represented by scalar and 3-vector respectively:
\mathbf{q} = (s, \vec{v})Looking for the quaternion representation of 3D rotations? See Versor.
§Constructing quaternions
Create a quaternion with an array of coordinates ([scalar, vector_0, vector_1, vector_2]).
use hoomd_vector::Quaternion;
let q = Quaternion::from([1.0, 2.0, 3.0, 4.0]);
assert_eq!(q.scalar, 1.0);
assert_eq!(q.vector, [2.0, 3.0, 4.0].into());§Quaternion properties
Compute a quaternion’s norm:
use hoomd_vector::Quaternion;
let q = Quaternion::from([3.0, 0.0, 4.0, 0.0]);
let norm = q.norm();
assert_eq!(norm, 5.0);Form the conjugate:
use hoomd_vector::Quaternion;
let q = Quaternion::from([1.0, 2.0, 3.0, 4.0]);
let q_star = q.conjugate();
assert_eq!(q_star, [1.0, -2.0, -3.0, -4.0].into());§Operating on quaternions
All operation examples use the following two quaternions:
use hoomd_vector::Quaternion;
let a = Quaternion::from([1.0, -2.0, 6.0, -4.0]);
let b = Quaternion::from([-2.0, 6.0, 4.0, 1.0]);Addition:
let c = a + b;
assert_eq!(c, [-1.0, 4.0, 10.0, -3.0].into());a += b;
assert_eq!(a, [-1.0, 4.0, 10.0, -3.0].into());Subtraction:
let c = a - b;
assert_eq!(c, [3.0, -8.0, 2.0, -5.0].into());a -= b;
assert_eq!(a, [3.0, -8.0, 2.0, -5.0].into());Multiplication by a scalar:
let c = a * 2.0;
assert_eq!(c, [2.0, -4.0, 12.0, -8.0].into());a *= 2.0;
assert_eq!(a, [2.0, -4.0, 12.0, -8.0].into());Division by a scalar:
let c = a / 2.0;
assert_eq!(c, [0.5, -1.0, 3.0, -2.0].into());a /= 2.0;
assert_eq!(a, [0.5, -1.0, 3.0, -2.0].into());Quaternion multiplication:
let c = a * b;
assert_eq!(c, [-10.0, 32.0, -30.0, -35.0].into());a *= b;
assert_eq!(a, [-10.0, 32.0, -30.0, -35.0].into());Fields§
§scalar: f64Scalar component
vector: Cartesian<3>Vector component
Implementations§
Source§impl Quaternion
impl Quaternion
Sourcepub fn norm_squared(&self) -> f64
pub fn norm_squared(&self) -> f64
The norm of the quaternion, squared.
|\mathbf{q}|^2§Example
use hoomd_vector::Quaternion;
let q = Quaternion::from([3.0, 0.0, 4.0, 0.0]);
let norm_squared = q.norm_squared();
assert_eq!(norm_squared, 25.0);Sourcepub fn norm(&self) -> f64
pub fn norm(&self) -> f64
The norm of the quaternion.
|\mathbf{q}|§Example
use hoomd_vector::Quaternion;
let q = Quaternion::from([3.0, 0.0, 4.0, 0.0]);
let norm = q.norm();
assert_eq!(norm, 5.0);Sourcepub fn conjugate(self) -> Self
pub fn conjugate(self) -> Self
Construct the conjugate of this quaternion.
\mathbf{q}^* = (s, -\vec{v})§Example
use hoomd_vector::Quaternion;
let q = Quaternion::from([1.0, 2.0, 3.0, 4.0]);
let q_star = q.conjugate();
assert_eq!(q_star, [1.0, -2.0, -3.0, -4.0].into());Sourcepub fn to_versor(self) -> Result<Versor, Error>
pub fn to_versor(self) -> Result<Versor, Error>
Create a Versor by normalizing the given quaternion.
\mathbf{v} = \frac{\mathbf{q}}{|\mathbf{q}|}§Example
use hoomd_vector::{Quaternion, Versor};
let q = Quaternion::from([3.0, 0.0, 0.0, 4.0]);
let v = q.to_versor()?;
assert_eq!(*v.get(), [3.0 / 5.0, 0.0, 0.0, 4.0 / 5.0].into());§Errors
Error::InvalidQuaternionMagnitude when self is the 0 quaternion.
Sourcepub fn to_versor_unchecked(self) -> Versor
pub fn to_versor_unchecked(self) -> Versor
Create a Versor by normalizing the given quaternion.
\mathbf{v} = \frac{\mathbf{q}}{|\mathbf{q}|}§Example
use hoomd_vector::{Quaternion, Versor};
let q = Quaternion::from([3.0, 0.0, 0.0, 4.0]);
let v = q.to_versor_unchecked();
assert_eq!(*v.get(), [3.0 / 5.0, 0.0, 0.0, 4.0 / 5.0].into());§Panics
Divide by 0 when self is the 0 quaternion.
Trait Implementations§
Source§impl AbsDiffEq for Quaternion
impl AbsDiffEq for Quaternion
Source§fn default_epsilon() -> Self::Epsilon
fn default_epsilon() -> Self::Epsilon
Source§fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool
fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool
Source§fn abs_diff_ne(&self, other: &Rhs, epsilon: Self::Epsilon) -> bool
fn abs_diff_ne(&self, other: &Rhs, epsilon: Self::Epsilon) -> bool
AbsDiffEq::abs_diff_eq.Source§impl Add for Quaternion
impl Add for Quaternion
Source§impl AddAssign for Quaternion
impl AddAssign for Quaternion
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read moreSource§impl Clone for Quaternion
impl Clone for Quaternion
Source§fn clone(&self) -> Quaternion
fn clone(&self) -> Quaternion
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Quaternion
impl Debug for Quaternion
Source§impl<'de> Deserialize<'de> for Quaternion
impl<'de> Deserialize<'de> for Quaternion
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl Display for Quaternion
impl Display for Quaternion
Source§impl Div<f64> for Quaternion
impl Div<f64> for Quaternion
Source§impl DivAssign<f64> for Quaternion
impl DivAssign<f64> for Quaternion
Source§fn div_assign(&mut self, rhs: f64)
fn div_assign(&mut self, rhs: f64)
/= operation. Read moreSource§impl From<[f64; 4]> for Quaternion
impl From<[f64; 4]> for Quaternion
Source§fn from(value: [f64; 4]) -> Self
fn from(value: [f64; 4]) -> Self
Construct a Quaternion from 4 values.
The first value is the real part. The 2nd through 4th are the complex vector part:
[scalar, vector_0, vector_1, vector_2].
§Example
use hoomd_vector::Quaternion;
let q = Quaternion::from([1.0, 2.0, 3.0, 4.0]);
assert_eq!(q.scalar, 1.0);
assert_eq!(q.vector, [2.0, 3.0, 4.0].into());Source§impl Mul<f64> for Quaternion
impl Mul<f64> for Quaternion
Source§impl Mul for Quaternion
impl Mul for Quaternion
Source§type Output = Quaternion
type Output = Quaternion
* operator.Source§fn mul(self, rhs: Quaternion) -> Self
fn mul(self, rhs: Quaternion) -> Self
* operation. Read moreSource§impl MulAssign<f64> for Quaternion
impl MulAssign<f64> for Quaternion
Source§fn mul_assign(&mut self, rhs: f64)
fn mul_assign(&mut self, rhs: f64)
*= operation. Read moreSource§impl MulAssign for Quaternion
impl MulAssign for Quaternion
Source§fn mul_assign(&mut self, rhs: Quaternion)
fn mul_assign(&mut self, rhs: Quaternion)
*= operation. Read moreSource§impl PartialEq for Quaternion
impl PartialEq for Quaternion
Source§impl RelativeEq for Quaternion
impl RelativeEq for Quaternion
Source§fn default_max_relative() -> Self::Epsilon
fn default_max_relative() -> Self::Epsilon
Source§fn relative_eq(
&self,
other: &Self,
epsilon: Self::Epsilon,
max_relative: Self::Epsilon,
) -> bool
fn relative_eq( &self, other: &Self, epsilon: Self::Epsilon, max_relative: Self::Epsilon, ) -> bool
Source§fn relative_ne(
&self,
other: &Rhs,
epsilon: Self::Epsilon,
max_relative: Self::Epsilon,
) -> bool
fn relative_ne( &self, other: &Rhs, epsilon: Self::Epsilon, max_relative: Self::Epsilon, ) -> bool
RelativeEq::relative_eq.Source§impl Serialize for Quaternion
impl Serialize for Quaternion
Source§impl Sub for Quaternion
impl Sub for Quaternion
Source§impl SubAssign for Quaternion
impl SubAssign for Quaternion
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-= operation. Read more