Sitelet https://web.archive.org/web/20201112194922/https://github.com/JuliaLang/julia/issues/37376
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

mod(::Complex{<:Integer}, ::Integer) and div etc. #37376

Open
stevengj opened this issue Sep 3, 2020 · 5 comments · May be fixed by #37377
Open

mod(::Complex{<:Integer}, ::Integer) and div etc. #37376

stevengj opened this issue Sep 3, 2020 · 5 comments · May be fixed by #37377

Comments

@stevengj
Copy link
Member

@stevengj stevengj commented Sep 3, 2020 •

As discussed on discourse, this seems like the only sensible definition:

mod(z::Complex{<:Integer}, n::Integer) = Complex(mod(real(z), n), mod(imag(z), n))

and similarly for div, rem (and probably divrem).

Should be easy to create a patch (much simpler than #35374): just add the 1-line definitions analogous to the one above, docs, news, and a test.

@chakravala
Copy link
Contributor

@chakravala chakravala commented Sep 3, 2020 •

I disagree, I think it should be

mod(z::Complex, n) = Complex(mod(real(z), n), mod(imag(z), n))

Since this defines mod recursively, you can just let mod of a Real coefficient to handle the dispatch

@stevengj
Copy link
Member Author

@stevengj stevengj commented Sep 3, 2020 •

Yes, that's a reasonable further generalization, although I would be more conservative and restrict it to

mod(z::Complex, n::Real) = ...

If n is not real, then splitting the computation into real and imaginary parts makes less sense (especially since Complex(a,b) expects real arguments).

@chakravala
Copy link
Contributor

@chakravala chakravala commented Sep 3, 2020 •

Edit: sorry I was mistaken in my last post.

I don't quite see the argument for restricting the dispatch though, if it errors, then it errors, why restrict the dispatch?

And why is Complex(1+im,1-im) not allowed? Nested complex numbers are quite reasonable.

@stevengj
Copy link
Member Author

@stevengj stevengj commented Sep 3, 2020

The problem with dispatching to something that is unlikely to ever be implemented is that the error message is needlessly confusing, both to users and to future implementors — if we want to support mod(::Complex, ::Complex), we will add that method (ala #35374), opposed to implementing mod(::Real, ::Complex).

As for generalizing the Complex constructor, that's a better subject for a separate issue.

chakravala added a commit to chakravala/julia that referenced this issue Sep 3, 2020
@stevengj
Copy link
Member Author

@stevengj stevengj commented Sep 3, 2020

Also, if n is not real, the return value is not necessarily Complex. For example, if n is a quaternion then the return value would not be Complex.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

2 participants
You can’t perform that action at this time.