alga2 0.1.0

A modern abstract-algebra hierarchy for Rust — the successor to alga, powered by batch-impl
Documentation
//! Quaternions — the classic non-commutative division ring.
//!
//! `Quaternion<T>` is `w + x·i + y·j + z·k` over a real field `T`: a
//! division ring (every nonzero element has a multiplicative inverse) that
//! is **not** a field (the multiplication does not commute), a four-
//! dimensional vector space over `T`, and a normed algebra. The impls live
//! in `crate::impls::quaternion` (generated by batch-impl).

use core::fmt;

/// A quaternion `w + x·i + y·j + z·k` over a real field `T`.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Quaternion<T> {
    w: T,
    x: T,
    y: T,
    z: T,
}

impl<T> Quaternion<T> {
    /// Builds `w + x·i + y·j + z·k`.
    pub const fn new(w: T, x: T, y: T, z: T) -> Self {
        Self { w, x, y, z }
    }

    /// The scalar (real) part.
    pub const fn w(&self) -> &T {
        &self.w
    }

    /// The `i` coefficient.
    pub const fn x(&self) -> &T {
        &self.x
    }

    /// The `j` coefficient.
    pub const fn y(&self) -> &T {
        &self.y
    }

    /// The `k` coefficient.
    pub const fn z(&self) -> &T {
        &self.z
    }
}

impl<T: fmt::Display> fmt::Display for Quaternion<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} + {}i + {}j + {}k", self.w, self.x, self.y, self.z)
    }
}