simple_equation_inversion 1.0.1

A Rust library for solving linear equations and invertible function chains.
Documentation
use core::ops::Mul;

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

impl Mul for Element {
    type Output = Self;

    fn mul(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: factor * k,
                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: factor * k,
                transform,
                inner,
                offset: offset * k,
            },

            // Multiplying two elements that both contain unknowns is non-linear
            (var @ (ElementKind::Variable { .. } | ElementKind::Transformed { .. }), _) => {
                if let Some(u) = self.unknown_ref().or_else(|| rhs.unknown_ref()) {
                    u.set_status(Err(Error::SquareForbidden));
                }
                let _ = var;
                let res = Self {
                    kind: ElementKind::Known(0.0),
                    error: Err(Error::SquareForbidden),
                };
                log_compute_step!("Step: ({self}) * ({rhs}) => ({res})");
                return res;
            }
        };

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

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

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

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

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

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

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

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

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

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

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

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

    fn mul(self, rhs: Element) -> Self::Output {
        rhs * self
    }
}

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

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

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

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

        // (2x + 3) * 4 = 8x + 12
        let expr = (2.0 * x + 3.0) * 4.0;
        assert_eq!(format!("{expr}"), "8x + 12");
    }

    #[test]
    fn test_mul_variables_triggers_square_forbidden() {
        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 res = x1 * x2;
        assert_eq!(res.error(), Err(Error::SquareForbidden));
    }
}