Skip to content

Commit 8235bd3

Browse files
committed
Simplify powc formula
1 parent f1903ec commit 8235bd3

File tree

1 file changed

+10
-20
lines changed

1 file changed

+10
-20
lines changed

src/lib.rs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -361,26 +361,8 @@ impl<T: Float> Complex<T> {
361361
/// Raises `self` to a complex power.
362362
#[inline]
363363
pub fn powc(self, exp: Self) -> Self {
364-
// formula: x^y = (a + i b)^(c + i d)
365-
// = (ρ e^(i θ))^c (ρ e^(i θ))^(i d)
366-
// where ρ=|x| and θ=arg(x)
367-
// = ρ^c e^(−d θ) e^(i c θ) ρ^(i d)
368-
// = p^c e^(−d θ) (cos(c θ)
369-
// + i sin(c θ)) (cos(d ln(ρ)) + i sin(d ln(ρ)))
370-
// = p^c e^(−d θ) (
371-
// cos(c θ) cos(d ln(ρ)) − sin(c θ) sin(d ln(ρ))
372-
// + i(cos(c θ) sin(d ln(ρ)) + sin(c θ) cos(d ln(ρ))))
373-
// = p^c e^(−d θ) (cos(c θ + d ln(ρ)) + i sin(c θ + d ln(ρ)))
374-
// = from_polar(p^c e^(−d θ), c θ + d ln(ρ))
375-
let (r, theta) = self.to_polar();
376-
377-
if r.is_zero() {
378-
return Self::new(r, r);
379-
}
380-
Self::from_polar(
381-
r.powf(exp.re) * (-exp.im * theta).exp(),
382-
exp.re * theta + exp.im * r.ln(),
383-
)
364+
// formula: x^y = exp(y * ln(x))
365+
(exp * self.ln()).exp()
384366
}
385367

386368
/// Raises a floating point number to the complex power `self`.
@@ -1719,6 +1701,9 @@ pub(crate) mod test {
17191701

17201702
#[cfg(any(feature = "std", feature = "libm"))]
17211703
pub(crate) mod float {
1704+
1705+
use core::f64::INFINITY;
1706+
17221707
use super::*;
17231708
use num_traits::{Float, Pow};
17241709

@@ -1914,6 +1899,11 @@ pub(crate) mod test {
19141899
));
19151900
let z = Complex::new(0.0, 0.0);
19161901
assert!(close(z.powc(b), z));
1902+
assert!(z.powc(Complex64::new(0., 0.)).is_nan());
1903+
assert!(z.powc(Complex64::new(0., INFINITY)).is_nan());
1904+
assert!(z.powc(Complex64::new(10., INFINITY)).is_nan());
1905+
assert!(z.powc(Complex64::new(INFINITY, INFINITY)).is_nan());
1906+
assert!(close(z.powc(Complex64::new(INFINITY, 0.)), z));
19171907
}
19181908

19191909
#[test]

0 commit comments

Comments
 (0)