diff --git a/src/lib.rs b/src/lib.rs index 661b67b..094f738 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -787,19 +787,29 @@ impl Mul> for Complex { } } -// (a + i b) * (c + i d) + (e + i f) == ((a*c + e) - b*d) + i (a*d + (b*c + f)) -impl> MulAdd> for Complex { +// (a + i b) * (c + i d) + (e + i f) == (a*c - (b*d - e)) + i (a*d + (b*c + f)) +impl MulAdd> for Complex +where + T: Clone + Num + MulAdd + Neg, +{ type Output = Complex; #[inline] fn mul_add(self, other: Complex, add: Complex) -> Complex { - let re = self.re.clone().mul_add(other.re.clone(), add.re) - - (self.im.clone() * other.im.clone()); // FIXME: use mulsub when available in rust - let im = self.re.mul_add(other.im, self.im.mul_add(other.re, add.im)); + let (a, b) = (self.re, self.im); + let (c, d) = (other.re, other.im); + let (e, f) = (add.re, add.im); + + let re = a + .clone() + .mul_add(c.clone(), -b.clone().mul_add(d.clone(), -e)); // FIXME: use mulsub when available in rust + let im = a.mul_add(d, b.mul_add(c, f)); Complex::new(re, im) } } -impl<'a, 'b, T: Clone + Num + MulAdd> MulAdd<&'b Complex> for &'a Complex { +impl<'a, 'b, T: Clone + Num + MulAdd + Neg> MulAdd<&'b Complex> + for &'a Complex +{ type Output = Complex; #[inline]