From 46d2036560113fba693bf7a487062cca34328130 Mon Sep 17 00:00:00 2001 From: Paul Date: Sat, 25 Oct 2025 14:31:37 +0200 Subject: [PATCH 1/5] use auxiliary variables as in documented formula --- src/lib.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 661b67b..90da77f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -793,9 +793,12 @@ impl> MulAdd> for 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(), e) - (b.clone() * d.clone()); // FIXME: use mulsub when available in rust + let im = a.mul_add(d, b.mul_add(c, f)); Complex::new(re, im) } } From d2481a0089d6de45abca437a48fe8dd4b0f69ef5 Mon Sep 17 00:00:00 2001 From: Paul Date: Sat, 25 Oct 2025 14:42:01 +0200 Subject: [PATCH 2/5] recommended impl from #148 --- src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 90da77f..65e4744 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -797,7 +797,9 @@ impl> MulAdd> for Complex { let (c, d) = (other.re, other.im); let (e, f) = (add.re, add.im); - let re = a.clone().mul_add(c.clone(), e) - (b.clone() * d.clone()); // FIXME: use mulsub when available in rust + 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) } From d984ef847263084b18f36087a4c954296d2443a5 Mon Sep 17 00:00:00 2001 From: Paul Date: Sat, 25 Oct 2025 14:42:31 +0200 Subject: [PATCH 3/5] update method documentation formula --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 65e4744..d7628e7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -787,7 +787,7 @@ impl Mul> for Complex { } } -// (a + i b) * (c + i d) + (e + i f) == ((a*c + e) - b*d) + i (a*d + (b*c + f)) +// (a + i b) * (c + i d) + (e + i f) == (a*c - (b*d - e)) + i (a*d + (b*c + f)) impl> MulAdd> for Complex { type Output = Complex; From 00ac3a20ac5ab11d7efb3f55eee2053b85a6a104 Mon Sep 17 00:00:00 2001 From: Paul Date: Sat, 25 Oct 2025 14:45:00 +0200 Subject: [PATCH 4/5] add trait bound --- src/lib.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d7628e7..261e95b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -788,7 +788,7 @@ impl Mul> 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 { +impl + Neg> MulAdd> for Complex { type Output = Complex; #[inline] @@ -804,7 +804,9 @@ impl> MulAdd> for Complex { 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] From bed1daac07ea9beeb11b923a669aa06d21dd0c9c Mon Sep 17 00:00:00 2001 From: Paul Date: Sat, 25 Oct 2025 14:45:46 +0200 Subject: [PATCH 5/5] move trait bounds into where clause --- src/lib.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 261e95b..094f738 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -788,7 +788,10 @@ impl Mul> for Complex { } // (a + i b) * (c + i d) + (e + i f) == (a*c - (b*d - e)) + i (a*d + (b*c + f)) -impl + Neg> MulAdd> for Complex { +impl MulAdd> for Complex +where + T: Clone + Num + MulAdd + Neg, +{ type Output = Complex; #[inline]