reefer 0.3.0

Optimizing proc-macro for geometric algebra
Documentation
use std::collections::BTreeMap;

use abstalg::Domain;

use crate::clifford::{BasisScalar, Frame};

/// Sparse multivector representation keyed by basis blades.
#[derive(Debug, Clone)]
pub struct Multivector<B: Ord + PartialOrd + Eq + PartialEq, S: Domain> {
    /// Sparse mapping from basis blades to their scalar coefficients.
    pub vectors: BTreeMap<B, S::Elem>,
}

impl<B: Ord, S: Domain<Elem: Ord>> Ord for Multivector<B, S> {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.vectors.keys().cmp(other.vectors.keys())
    }
}
impl<B: Ord, S: Domain<Elem: PartialOrd>> PartialOrd for Multivector<B, S> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.vectors.keys().partial_cmp(other.vectors.keys())
    }
}
impl<B: Ord, S: Domain<Elem: Eq>> Eq for Multivector<B, S> {}
impl<B: Ord, S: Domain<Elem: PartialEq>> PartialEq for Multivector<B, S> {
    fn eq(&self, other: &Self) -> bool {
        self.vectors.iter().eq(other.vectors.iter())
    }
}

impl<B: Frame, S: BasisScalar> Multivector<B, S> {
    /// Construct a multivector from an iterator of `(blade, coefficient)` pairs.
    pub fn from_terms<I>(terms: I) -> Self
    where
        I: IntoIterator<Item = (B, S::Elem)>,
    {
        Self {
            vectors: terms.into_iter().collect(),
        }
    }
    /// Expose the internal sparse map of blades to coefficients.
    #[allow(dead_code)]
    pub fn terms(&self) -> &BTreeMap<B, S::Elem> {
        &self.vectors
    }
}