Skip to main content

UnitQuaternion

Struct UnitQuaternion 

Source
pub struct UnitQuaternion { /* private fields */ }
Expand description

Unit quaternion representing an element of SU(2)

Quaternion q = w + xi + yj + zk where w² + x² + y² + z² = 1

§Geometric Interpretation

Every unit quaternion represents a rotation in 3D space:

  • Rotation by angle θ around axis n̂ = (nx, ny, nz): q = cos(θ/2) + sin(θ/2)(nx·i + ny·j + nz·k)

§⚠️ Important: Double Cover Property

q and -q represent the SAME rotation but DIFFERENT quantum states:

  • Both rotate 3D vectors identically (v → q·v·q⁻¹ = (-q)·v·(-q)⁻¹)
  • But q ≠ -q as SU(2) group elements
  • Consequence: Fermions change sign under 2π rotation (ψ → -ψ)

This is NOT a numerical issue or degeneracy - it’s fundamental topology!

§Relation to SU(2) Matrix

q = w + xi + yj + zk corresponds to SU(2) matrix:

U = [[ w + ix,  -y + iz],
     [ y + iz,   w - ix]]

Note: -q gives matrix -U, which acts identically on vectors but represents a different element of SU(2) (relevant for spinors/fermions).

Implementations§

Source§

impl UnitQuaternion

Source

pub fn w(&self) -> f64

Get the real (scalar) component.

Source

pub fn x(&self) -> f64

Get the imaginary i component.

Source

pub fn y(&self) -> f64

Get the imaginary j component.

Source

pub fn z(&self) -> f64

Get the imaginary k component.

Source

pub fn components(&self) -> (f64, f64, f64, f64)

Get all components as a tuple (w, x, y, z).

Source

pub fn to_array(&self) -> [f64; 4]

Get all components as an array [w, x, y, z].

Source§

impl UnitQuaternion

Source

pub fn identity() -> Self

Identity element: q = 1

Source

pub fn new(w: f64, x: f64, y: f64, z: f64) -> Self

Create from components (automatically normalizes to unit quaternion)

Returns identity if input norm < NORM_EPSILON (degenerate case).

Source

pub fn from_axis_angle(axis: [f64; 3], angle: f64) -> Self

Create from axis-angle representation

§Arguments
  • axis - Rotation axis (nx, ny, nz), will be normalized
  • angle - Rotation angle in radians
§Returns

Unit quaternion q = cos(θ/2) + sin(θ/2)(nx·i + ny·j + nz·k)

Source

pub fn to_axis_angle(&self) -> ([f64; 3], f64)

Extract axis and angle from quaternion

§Returns

(axis, angle) where axis is normalized and angle ∈ [0, 2π]

Source

pub fn rotation_x(theta: f64) -> Self

Rotation around X-axis by angle θ

Source

pub fn rotation_y(theta: f64) -> Self

Rotation around Y-axis by angle θ

Source

pub fn rotation_z(theta: f64) -> Self

Rotation around Z-axis by angle θ

Source

pub fn conjugate(&self) -> Self

Quaternion conjugate: q* = w - xi - yj - zk

For unit quaternions: q* = q⁻¹ (inverse)

Source

pub fn inverse(&self) -> Self

Quaternion inverse: q⁻¹

For unit quaternions: q⁻¹ = q*

Source

pub fn norm_squared(&self) -> f64

Norm squared: |q|² = w² + x² + y² + z²

Should always be 1 for unit quaternions

Source

pub fn norm(&self) -> f64

Norm: |q| = sqrt(w² + x² + y² + z²)

Should always be 1 for unit quaternions

Source

pub fn normalize(&self) -> Self

Renormalize to unit quaternion (fix numerical drift)

Source

pub fn distance_to_identity(&self) -> f64

Distance to identity (geodesic distance on S³)

d(q, 1) = 2·arccos(|w|)

This is the rotation angle in [0, π]

Source

pub fn distance_to(&self, other: &Self) -> f64

Geodesic distance to another quaternion

d(q₁, q₂) = arccos(|q₁·q₂|) where · is quaternion dot product

Source

pub fn slerp(&self, other: &Self, t: f64) -> Self

Spherical linear interpolation (SLERP)

Interpolate between self and other with parameter t ∈ [0,1] Returns shortest path on S³

Source

pub fn exp(v: [f64; 3]) -> Self

Exponential map from Lie algebra 𝔰𝔲(2) to group SU(2)

Given a vector v = (v₁, v₂, v₃) ∈ ℝ³ (Lie algebra element), compute exp(v) as a unit quaternion.

Formula: exp(v) = cos(|v|/2) + sin(|v|/2)·(v/|v|)

Source

pub fn log(&self) -> [f64; 3]

Logarithm map from group SU(2) to Lie algebra 𝔰𝔲(2)

Returns vector v ∈ ℝ³ such that exp(v) = q

Formula: log(q) = (θ/sin(θ/2))·(x, y, z) where θ = 2·arccos(w)

Source

pub fn to_matrix(&self) -> [[Complex64; 2]; 2]

Convert to SU(2) matrix representation (for compatibility)

Returns 2×2 complex matrix: U = [[ w + ix, -y + iz], [ y + iz, w - ix]]

Source

pub fn from_matrix(matrix: [[Complex64; 2]; 2]) -> Self

Create from SU(2) matrix

Expects matrix [[α, -β*], [β, α*]] with |α|² + |β|² = 1

Source

pub fn rotate_vector(&self, v: [f64; 3]) -> [f64; 3]

Act on a 3D vector by conjugation: v’ = qvq*

This is the rotation action of SU(2) on ℝ³ Identifies v = (x,y,z) with quaternion xi + yj + zk

Uses the direct formula (Rodrigues) for efficiency: v’ = v + 2w(n×v) + 2(n×(n×v)) where n = (x,y,z)

Source

pub fn verify_unit(&self, tolerance: f64) -> bool

Verify this is approximately a unit quaternion

Source

pub fn to_rotation_matrix(&self) -> [[f64; 3]; 3]

Construct the 3×3 rotation matrix corresponding to this unit quaternion.

Returns the SO(3) matrix R such that R·v = rotate_vector(v) for all v ∈ ℝ³.

§Lean Correspondence

This is the Rust counterpart of toRotationMatrix in OrthogonalGroups.lean. The Lean proofs establish:

  • toRotationMatrix_orthogonal_axiom: R(q)ᵀ·R(q) = I
  • toRotationMatrix_det_axiom: det(R(q)) = 1
  • toRotationMatrix_mul_axiom: R(q₁·q₂) = R(q₁)·R(q₂)
  • toRotationMatrix_neg: R(-q) = R(q)

Trait Implementations§

Source§

impl Clone for UnitQuaternion

Source§

fn clone(&self) -> UnitQuaternion

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 UnitQuaternion

Source§

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

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

impl Mul for UnitQuaternion

Source§

type Output = UnitQuaternion

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl MulAssign for UnitQuaternion

Source§

fn mul_assign(&mut self, rhs: Self)

Performs the *= operation. Read more
Source§

impl PartialEq for UnitQuaternion

Source§

fn eq(&self, other: &UnitQuaternion) -> 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 Copy for UnitQuaternion

Source§

impl StructuralPartialEq for UnitQuaternion

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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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, 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T, Right> ClosedMul<Right> for T
where T: Mul<Right, Output = T> + MulAssign<Right>,

Source§

impl<T, Right> ClosedMulAssign<Right> for T
where T: ClosedMul<Right> + MulAssign<Right>,

Source§

impl<T> Scalar for T
where T: 'static + Clone + PartialEq + Debug,