Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
simplify div_add impl: use Div, Add
  • Loading branch information
PaulXiCao committed Oct 25, 2025
commit 49503f01f076b0612c528f8808da9be915b12658
13 changes: 5 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -830,9 +830,7 @@ trait DivAdd<Rhs = Self, Addend = Self> {
}

// (a + i b) / (c + i d) + (e + i f) == [(a + i b) * (c - i d)] / (c*c + d*d) + (e + i f)
// == [(a + i b) * (c - i d) + (c*c + d*d) * (e + i f)] / n for n=(c*c + d*d)
// == [(a*c + b*d + n*e) + i (-a*d + b*c + n*f)] / n
// == [{(a*c + b*d) / n + e} + i {(-a*d + b*c) / n + f}]
// == {(a*c + b*d) + i (-a*d + b*c)} / n + (e + i f) for n=(c*c + d*d)
impl<T: Clone + Num + MulAdd<Output = T> + Neg<Output = T>> DivAdd<Complex<T>> for Complex<T> {
type Output = Self;

Expand All @@ -841,12 +839,11 @@ impl<T: Clone + Num + MulAdd<Output = T> + Neg<Output = T>> DivAdd<Complex<T>> f
let n = other.norm_sqr();
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() * d.clone());
let im = a.mul_add(-d, b * c);

let re = a.clone().mul_add(c.clone(), b.clone() * d.clone()) / n.clone() + e;
let im = a.mul_add(-d, b * c) / n + f;

Self::new(re, im)
Self::new(re, im) / n + add
}
}
impl<T: Clone + Num + MulAdd<Output = T> + Neg<Output = T>> DivAdd<&Complex<T>> for &Complex<T> {
Expand Down