Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Pow for Complex #18

Closed
clarfonthey opened this issue Feb 27, 2018 · 7 comments · Fixed by #56
Closed

Implement Pow for Complex #18

clarfonthey opened this issue Feb 27, 2018 · 7 comments · Fixed by #56

Comments

@clarfonthey
Copy link
Contributor

See rust-num/num-traits#38 for details on this trait.

@shingtaklam1324
Copy link
Contributor

shingtaklam1324 commented Mar 7, 2018

There is the issue of raising a Complex<T> to a power of Complex<T>, as it is equal to

a^(b+ic) = (a ^ b)[cos(c ln a) + i sin(c ln a)]

If a is a complex number, then ln ahas multiple answers, and there is not one definite answer. Instead, there is an infinite set of possible answers.

@clarfonthey
Copy link
Contributor Author

@shingtaklam1324 right, but returning the principal solution is usually what people want. It's like how asin(1)/π is really (8n + 1)/4 but any practical implementation will return 1/4 as you can get any other solution by adding 2n.

Complex numbers work similarly; solutions can be obtained by repeatedly multiplying a constant complex number, which I don't recall the equation for off the top of my head. The point is that the principal solution is 99% of the time what you want.

I mean, in theory you could return an iterator over the solutions because they're countable, but I don't know if that's appropriate outside of niche cases.

@cuviper
Copy link
Member

cuviper commented Mar 7, 2018

FWIW, we do already have powc, and rust-num/num#137 (comment) referenced this derivation.

@shingtaklam1324
Copy link
Contributor

shingtaklam1324 commented Mar 8, 2018

@clarcharr I think I may have over complicated this slightly, in that case, it should just be a simple refactoring or having a wrapper function for powc to

impl Pow<Complex<T>> for Complex<T>
  where T: Float + Clone

and powf to

impl Pow<T> for Complex<T>
  where T: Float + Clone

@cuviper
Copy link
Member

cuviper commented Mar 8, 2018

Yes, I would just wrap powc and powf. It might be nice to add powi too.

@clarfonthey
Copy link
Contributor Author

Floats currently support ints and floats as exponents so I'd do all three for Complex.

@benruijl
Copy link

powi would be very welcome! I get some rounding errors when using powf:

        println!(
            "test: {:?}",
            Complex::new(0., -2. * f64::PI()).powf(1.)
        );

Yields: Complex { re: 0.00000000000000038473413874435795, im: -6.283185307179586 }

bors bot added a commit that referenced this issue Jun 6, 2019
56: Add Complex::powi and assorted Pow impls r=cuviper a=cuviper

Closes #18.

Co-authored-by: Josh Stone <cuviper@gmail.com>
@bors bors bot closed this as completed in #56 Jun 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants