simple_equation_inversion 1.0.1

A Rust library for solving linear equations and invertible function chains.
Documentation
use crate::element::Element;
use crate::element::element_kind::ElementKind;

impl PartialEq for Element {
    fn eq(&self, rhs: &Self) -> bool {
        if self.error != rhs.error {
            return false;
        }

        match (&self.kind, &rhs.kind) {
            (ElementKind::Known(v1), ElementKind::Known(v2)) => v1 == v2,
            (
                ElementKind::Variable {
                    factor: f1,
                    unknown: u1,
                    offset: o1,
                },
                ElementKind::Variable {
                    factor: f2,
                    unknown: u2,
                    offset: o2,
                },
            ) => f1 == f2 && o1 == o2 && u1.get_status() == u2.get_status(),
            (
                ElementKind::Transformed {
                    factor: f1,
                    transform: t1,
                    inner: i1,
                    offset: o1,
                },
                ElementKind::Transformed {
                    factor: f2,
                    transform: t2,
                    inner: i2,
                    offset: o2,
                },
            ) => f1 == f2 && o1 == o2 && t1.name() == t2.name() && i1 == i2,
            _ => false,
        }
    }
}

impl PartialEq<f64> for Element {
    fn eq(&self, rhs: &f64) -> bool {
        self == &Element::new_known(*rhs)
    }
}

impl PartialEq<Element> for f64 {
    fn eq(&self, rhs: &Element) -> bool {
        &Element::new_known(*self) == rhs
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[allow(unused_imports)]
    use alloc::format;
    use alloc::rc::Rc;
    #[allow(unused_imports)]
    use alloc::string::ToString;
    use core::cell::RefCell;

    #[test]
    fn test_partial_eq_knowns() {
        let a = Element::known(10.0);
        let b = Element::known(10.0);
        let c = Element::known(20.0);
        assert_eq!(a, b);
        assert_ne!(a, c);
        assert_eq!(a, 10.0);
        assert_eq!(10.0, a);
    }

    #[test]
    fn test_partial_eq_variables() {
        let rc1 = Rc::new(RefCell::new(0.0));
        let rc2 = Rc::new(RefCell::new(0.0));
        let x1 = Element::new_unknown(rc1);
        let x2 = Element::new_unknown(rc2);

        // Same coefficients and status
        assert_eq!(x1.clone() + 5.0, x2 + 5.0);
    }
}