Skip to content

feat: Add complex numbers #37

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

sigmasternchen
Copy link

First draft for complex number (#33), please take a look.

Changes from the discussion on the issue:

  • I added a public reciprocal function since I needed it internally anyway.
  • The power function only takes positive integer exponents, because I used De Moivre’s theorem. Accordingly weighted_product also only supports integer weights. If anyone knows a more generic algorithm, please let me know.

Documentation is also still missing, and I didn't do any changes to the main maths module yet.

Please let me know what you think.

I apologize in advance for not being very responsive - I'm currently on vacation and probably won't have a lot of time to work on this.

- Sigma

@NicklasXYZ NicklasXYZ marked this pull request as ready for review February 11, 2025 19:46
@NicklasXYZ NicklasXYZ self-requested a review as a code owner February 11, 2025 19:46
@NicklasXYZ
Copy link
Member

Overall I think it looks pretty good!

I made some improvements to the weighted_x functions in the main math module based on Hayleigh’s feedback, and I think we could align the complex number implementation in the same way to incorporate that feedback as well (e.g. see the math library thread on the Gleam Discord server under “sharing”).

I think it would be great if we could generalize the weighted_product, power, nth_root, and other related functions, to accept real exponents by implementing the logarithm for complex numbers, such that we can compute z^a = exp(a * log(z)), where z is a complex number, a is a real number, and log(z) = log(|z|) + i * arg(z).

For more info see e.g. https://en.wikipedia.org/wiki/Principal_value and/or https://docs.python.org/3/library/cmath.html#power-and-logarithmic-functions.

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 this pull request may close these issues.

2 participants