alga2 0.1.0

A modern abstract-algebra hierarchy for Rust — the successor to alga, powered by batch-impl
Documentation
//! Complex numbers — an in-crate, zero-dependency type.
//!
//! `Complex<T>` is a ring when `T` is a ring and a field when `T` is a
//! field; the tower impls live in `crate::impls::complex` (generated by
//! batch-impl). The alternative — a feature-gated bridge to `num-complex` —
//! is deferred: keeping the core zero-dependency outweighs re-exporting a
//! well-known type here.

use core::fmt;

/// A complex number `re + im·i` over a scalar type `T`.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Complex<T> {
    re: T,
    im: T,
}

impl<T> Complex<T> {
    /// Builds `re + im·i`.
    pub const fn new(re: T, im: T) -> Self {
        Self { re, im }
    }

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

    /// The imaginary part.
    pub const fn im(&self) -> &T {
        &self.im
    }
}

impl<T: fmt::Display> fmt::Display for Complex<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} + {}i", self.re, self.im)
    }
}