simple_equation_inversion 1.0.1

A Rust library for solving linear equations and invertible function chains.
Documentation
use alloc::rc::Rc;
use core::ops::{Add, Sub};

use crate::element::Element;
use crate::element::element_kind::ElementKind;
use crate::error::Error;
use crate::log_compute_step;

impl Add for Element {
    type Output = Self;

    fn add(self, rhs: Self) -> Self {
        let error = self.error.clone().and(rhs.error.clone());
        if error.is_err() {
            let res = Self {
                kind: ElementKind::Known(0.0),
                error,
            };
            log_compute_step!("Step: ({self}) + ({rhs}) => ({res})");
            return res;
        }

        let kind = match (self.kind.clone(), rhs.kind.clone()) {
            (ElementKind::Known(a), ElementKind::Known(b)) => ElementKind::Known(a + b),

            (
                ElementKind::Known(k),
                ElementKind::Variable {
                    factor,
                    unknown,
                    offset,
                },
            )
            | (
                ElementKind::Variable {
                    factor,
                    unknown,
                    offset,
                },
                ElementKind::Known(k),
            ) => ElementKind::Variable {
                factor,
                unknown,
                offset: offset + k,
            },

            (
                ElementKind::Known(k),
                ElementKind::Transformed {
                    factor,
                    transform,
                    inner,
                    offset,
                },
            )
            | (
                ElementKind::Transformed {
                    factor,
                    transform,
                    inner,
                    offset,
                },
                ElementKind::Known(k),
            ) => ElementKind::Transformed {
                factor,
                transform,
                inner,
                offset: offset + k,
            },

            (
                ElementKind::Variable {
                    factor: f1,
                    unknown: u1,
                    offset: o1,
                },
                ElementKind::Variable {
                    factor: f2,
                    unknown: u2,
                    offset: o2,
                },
            ) => {
                if !Rc::ptr_eq(&u1.unknown, &u2.unknown) {
                    u1.set_status(Err(Error::SeveralUnknown));
                    u2.set_status(Err(Error::SeveralUnknown));
                    let res = Self {
                        kind: ElementKind::Known(0.0),
                        error: Err(Error::SeveralUnknown),
                    };
                    return res;
                }
                ElementKind::Variable {
                    factor: f1 + f2,
                    unknown: u1,
                    offset: o1 + o2,
                }
            }

            (
                ElementKind::Transformed {
                    factor: f1,
                    transform: t1,
                    inner: i1,
                    offset: o1,
                },
                ElementKind::Transformed {
                    factor: f2,
                    transform: t2,
                    inner: i2,
                    offset: o2,
                },
            ) => {
                if t1.name() == t2.name() && *i1 == *i2 {
                    ElementKind::Transformed {
                        factor: f1 + f2,
                        transform: t1,
                        inner: i1,
                        offset: o1 + o2,
                    }
                } else {
                    let res = Self {
                        kind: ElementKind::Known(0.0),
                        error: Err(Error::SeveralUnknown),
                    };
                    return res;
                }
            }

            _ => {
                // Incompatible linear combination (e.g. Variable + Transformed)
                let res = Self {
                    kind: ElementKind::Known(0.0),
                    error: Err(Error::SeveralUnknown),
                };
                return res;
            }
        };

        let res = Self { kind, error };
        log_compute_step!("Step: ({self}) + ({rhs}) => ({res})");
        res
    }
}

impl Add<&Element> for &Element {
    type Output = Element;

    fn add(self, rhs: &Element) -> Element {
        self.clone() + rhs.clone()
    }
}

impl Add<Element> for &Element {
    type Output = Element;

    fn add(self, rhs: Element) -> Element {
        self.clone() + rhs
    }
}

impl Add<&Element> for Element {
    type Output = Element;

    fn add(self, rhs: &Element) -> Element {
        self + rhs.clone()
    }
}

impl Add<f64> for Element {
    type Output = Self;

    fn add(self, rhs: f64) -> Self {
        self + Element::new_known(rhs)
    }
}

impl Add<f64> for &Element {
    type Output = Element;

    fn add(self, rhs: f64) -> Element {
        self.clone() + rhs
    }
}

impl Add<Element> for f64 {
    type Output = Element;

    fn add(self, rhs: Element) -> Self::Output {
        rhs + self
    }
}

impl Add<&Element> for f64 {
    type Output = Element;

    fn add(self, rhs: &Element) -> Self::Output {
        rhs.clone() + self
    }
}

impl Sub for Element {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self {
        self + (-rhs)
    }
}

impl Sub<&Element> for &Element {
    type Output = Element;

    fn sub(self, rhs: &Element) -> Element {
        self.clone() - rhs.clone()
    }
}

impl Sub<Element> for &Element {
    type Output = Element;

    fn sub(self, rhs: Element) -> Element {
        self.clone() - rhs
    }
}

impl Sub<&Element> for Element {
    type Output = Element;

    fn sub(self, rhs: &Element) -> Element {
        self - rhs.clone()
    }
}

impl Sub<f64> for Element {
    type Output = Self;

    fn sub(self, rhs: f64) -> Self {
        self - Element::new_known(rhs)
    }
}

impl Sub<f64> for &Element {
    type Output = Element;

    fn sub(self, rhs: f64) -> Element {
        self.clone() - rhs
    }
}

impl Sub<Element> for f64 {
    type Output = Element;

    fn sub(self, rhs: Element) -> Self::Output {
        Element::new_known(self) - rhs
    }
}

impl Sub<&Element> for f64 {
    type Output = Element;

    fn sub(self, rhs: &Element) -> Self::Output {
        Element::new_known(self) - rhs.clone()
    }
}

#[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_add_knowns() {
        let a = Element::known(3.0);
        let b = Element::known(4.0);
        assert_eq!((a + b).value(), Some(7.0));
    }

    #[test]
    fn test_add_variable_and_constant() {
        let rc = Rc::new(RefCell::new(0.0));
        let x = Element::new_unknown(rc);

        let sum1 = x.clone() + 4.0;
        assert_eq!(format!("{sum1}"), "x + 4");

        let sum2 = 4.0 + x;
        assert_eq!(format!("{sum2}"), "x + 4");
    }

    #[test]
    fn test_add_variables_same_unknown() {
        let rc = Rc::new(RefCell::new(0.0));
        let x1 = Element::new_unknown(rc.clone());
        let x2 = Element::new_unknown(rc);

        // (x + 3) + (2x + 4) = 3x + 7
        let e1 = x1 + 3.0;
        let e2 = 2.0 * x2 + 4.0;
        let sum = e1 + e2;
        assert_eq!(format!("{sum}"), "3x + 7");
    }

    #[test]
    fn test_add_variables_distinct_unknowns_error() {
        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);

        let sum = x1 + x2;
        assert_eq!(sum.error(), Err(Error::SeveralUnknown));
    }

    #[test]
    fn test_sub_elements() {
        let rc = Rc::new(RefCell::new(0.0));
        let x = Element::new_unknown(rc);

        // (x + 3) - 4 = x - 1
        let sub1 = (x.clone() + 3.0) - 4.0;
        assert_eq!(format!("{sub1}"), "x - 1");

        // 4 - (x + 3) = -x + 1
        let sub2 = 4.0 - (x + 3.0);
        assert_eq!(format!("{sub2}"), "-x + 1");
    }
}