Skip to main content

Quaternion

Struct Quaternion 

Source
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: f64

Scalar component

§vector: Cartesian<3>

Vector component

Implementations§

Source§

impl Quaternion

Source

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);
Source

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);
Source

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());
Source

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.

Source

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

Source§

type Epsilon = <f64 as AbsDiffEq>::Epsilon

Used for specifying relative comparisons.
Source§

fn default_epsilon() -> Self::Epsilon

The default tolerance to use when testing values that are close together. Read more
Source§

fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool

A test for equality that uses the absolute difference to compute the approximimate equality of two numbers.
Source§

fn abs_diff_ne(&self, other: &Rhs, epsilon: Self::Epsilon) -> bool

The inverse of AbsDiffEq::abs_diff_eq.
Source§

impl Add for Quaternion

Source§

type Output = Quaternion

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self

Performs the + operation. Read more
Source§

impl AddAssign for Quaternion

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl Clone for Quaternion

Source§

fn clone(&self) -> Quaternion

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Quaternion

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Quaternion

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Quaternion

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Format a Quaternion as [{s}, [{v[0]}, {v[1]}, {v[2]}]].

Source§

impl Div<f64> for Quaternion

Source§

type Output = Quaternion

The resulting type after applying the / operator.
Source§

fn div(self, rhs: f64) -> Self

Performs the / operation. Read more
Source§

impl DivAssign<f64> for Quaternion

Source§

fn div_assign(&mut self, rhs: f64)

Performs the /= operation. Read more
Source§

impl From<[f64; 4]> for Quaternion

Source§

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

Source§

type Output = Quaternion

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: f64) -> Self

Performs the * operation. Read more
Source§

impl Mul for Quaternion

Source§

type Output = Quaternion

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Quaternion) -> Self

Performs the * operation. Read more
Source§

impl MulAssign<f64> for Quaternion

Source§

fn mul_assign(&mut self, rhs: f64)

Performs the *= operation. Read more
Source§

impl MulAssign for Quaternion

Source§

fn mul_assign(&mut self, rhs: Quaternion)

Performs the *= operation. Read more
Source§

impl PartialEq for Quaternion

Source§

fn eq(&self, other: &Quaternion) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl RelativeEq for Quaternion

Source§

fn default_max_relative() -> Self::Epsilon

The default relative tolerance for testing values that are far-apart. Read more
Source§

fn relative_eq( &self, other: &Self, epsilon: Self::Epsilon, max_relative: Self::Epsilon, ) -> bool

A test for equality that uses a relative comparison if the values are far apart.
Source§

fn relative_ne( &self, other: &Rhs, epsilon: Self::Epsilon, max_relative: Self::Epsilon, ) -> bool

The inverse of RelativeEq::relative_eq.
Source§

impl Serialize for Quaternion

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Sub for Quaternion

Source§

type Output = Quaternion

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self

Performs the - operation. Read more
Source§

impl SubAssign for Quaternion

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
Source§

impl Copy for Quaternion

Source§

impl StructuralPartialEq for Quaternion

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,