use core::ops::Div;
use crate::element::Element;
use crate::element::element_kind::ElementKind;
use crate::error::Error;
use crate::log_compute_step;
impl Div for Element {
type Output = Self;
fn div(self, rhs: Self) -> Self {
let mut error = self.error.clone().and(rhs.error.clone());
match rhs.kind {
ElementKind::Variable { unknown, .. } => {
unknown.set_status(Err(Error::UnknownInDenominator));
if let Some(u) = self.unknown_ref() {
u.set_status(Err(Error::UnknownInDenominator));
}
error = error.and(Err(Error::UnknownInDenominator));
Self {
kind: ElementKind::Known(0.0),
error,
}
}
ElementKind::Transformed { inner, .. } => {
if let Some(u) = inner.unknown_ref().or_else(|| self.unknown_ref()) {
u.set_status(Err(Error::UnknownInDenominator));
}
error = error.and(Err(Error::UnknownInDenominator));
Self {
kind: ElementKind::Known(0.0),
error,
}
}
ElementKind::Known(k) => {
if k == 0.0 {
if let Some(u) = self.unknown_ref() {
u.set_status(Err(Error::DivisionByZero));
}
error = error.and(Err(Error::DivisionByZero));
return Self {
kind: ElementKind::Known(0.0),
error,
};
}
let kind = match self.kind.clone() {
ElementKind::Known(v) => ElementKind::Known(v / k),
ElementKind::Variable {
factor,
unknown,
offset,
} => ElementKind::Variable {
factor: factor / k,
unknown,
offset: offset / k,
},
ElementKind::Transformed {
factor,
transform,
inner,
offset,
} => ElementKind::Transformed {
factor: factor / k,
transform,
inner,
offset: offset / k,
},
};
let res = Self { kind, error };
log_compute_step!("Step: ({self}) / ({rhs}) => ({res})");
res
}
}
}
}
impl Div<&Element> for &Element {
type Output = Element;
fn div(self, rhs: &Element) -> Element {
self.clone() / rhs.clone()
}
}
impl Div<Element> for &Element {
type Output = Element;
fn div(self, rhs: Element) -> Element {
self.clone() / rhs
}
}
impl Div<&Element> for Element {
type Output = Element;
fn div(self, rhs: &Element) -> Element {
self / rhs.clone()
}
}
impl Div<f64> for Element {
type Output = Self;
fn div(self, rhs: f64) -> Self {
self / Element::new_known(rhs)
}
}
impl Div<f64> for &Element {
type Output = Element;
fn div(self, rhs: f64) -> Element {
self.clone() / rhs
}
}
impl Div<Element> for f64 {
type Output = Element;
fn div(self, rhs: Element) -> Self::Output {
Element::new_known(self) / rhs
}
}
impl Div<&Element> for f64 {
type Output = Element;
fn div(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_div_knowns() {
let a = Element::known(12.0);
let b = Element::known(4.0);
assert_eq!((a / b).value(), Some(3.0));
}
#[test]
fn test_div_variable_by_scalar() {
let rc = Rc::new(RefCell::new(0.0));
let x = Element::new_unknown(rc);
let expr = (x + 3.0) / 4.0;
assert_eq!(format!("{expr}"), "0.25x + 0.75");
}
#[test]
fn test_div_by_zero_error() {
let rc = Rc::new(RefCell::new(0.0));
let x = Element::new_unknown(rc);
let res = (x + 3.0) / 0.0;
assert_eq!(res.error(), Err(Error::DivisionByZero));
}
#[test]
fn test_div_by_variable_triggers_unknown_in_denominator() {
let rc = Rc::new(RefCell::new(0.0));
let x = Element::new_unknown(rc);
let res = 10.0 / (x + 1.0);
assert_eq!(res.error(), Err(Error::UnknownInDenominator));
}
}