From aeef49e9ef770fdb30ff465eeeec172a3607be76 Mon Sep 17 00:00:00 2001 From: bakk Date: Thu, 20 May 2021 19:50:59 +0200 Subject: [PATCH] Division with complex numbers --- kalk/src/kalk_num/mod.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/kalk/src/kalk_num/mod.rs b/kalk/src/kalk_num/mod.rs index 75e59b0..d9e5423 100644 --- a/kalk/src/kalk_num/mod.rs +++ b/kalk/src/kalk_num/mod.rs @@ -292,8 +292,23 @@ impl KalkNum { } pub(crate) fn div(self, context: &mut crate::interpreter::Context, rhs: KalkNum) -> KalkNum { - let right = calculate_unit(context, &self, rhs.clone()).unwrap_or(rhs); - KalkNum::new(self.value / right.value, &right.unit) + let right = calculate_unit(context, &self, rhs.clone()).unwrap_or(rhs.clone()); + + // Avoid unecessary calculations + if self.imaginary_value == 0f64 && right.imaginary_value == 0f64 { + KalkNum::new(self.value / right.value, &right.unit) + } else { + // Multiply both the numerator and denominator + // with the conjugate of the denominator, and divide. + let conjugate = rhs.get_conjugate(); + let numerator = self.clone().mul(context, conjugate.clone()); + let denominator = rhs.clone().mul(context, conjugate); + KalkNum::new_with_imaginary( + numerator.value / denominator.value.clone(), + &right.unit, + numerator.imaginary_value / denominator.value, + ) + } } pub(crate) fn rem(self, context: &mut crate::interpreter::Context, rhs: KalkNum) -> KalkNum { @@ -301,6 +316,14 @@ impl KalkNum { KalkNum::new(self.value % right.value, &right.unit) } + pub fn get_conjugate(&self) -> KalkNum { + KalkNum::new_with_imaginary( + self.value.clone(), + &self.unit, + self.imaginary_value.clone() * (-1f64), + ) + } + /// Get an estimate of what the number is, eg. 3.141592 => π pub fn estimate(&self) -> Option { let fract = self.value.clone().fract().abs();