-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Extend floor (round, etc) to Complex numbers #42060
Comments
If there is a concensus among other languages that this is the right behavior, it seems reasonable to me. If so, it would be a good first PR. |
Wolfram Alpha has this definition as well, but it doesn't seem that clear cut, as Existing implementations seem to project onto gaussian integers (like in the OP here), but there seem to be at least some issues with that in regards to some identities. Python doesn't have a complex floor, as far as I can tell (does numpy?). Not sure what the right thing to do is 🤷♂️ |
There's also a list of interesting requirements for a
The naive implementation of just flooring components violates Fractionality, as noted in this codegolf (which also has a version with allegedly minimal rounding required). It should be noted that if we go the APL route, we can similarly define |
Great insights! In this case, I feel that it's better not to have a This is because people may port their codes from somewhere else, have it not raise an error, and not find functional bugs. For my application |
I don't know if the APL version is "more correct" - the identity |
If others want to play around with either version, here are both together (MIT, free to use, yada yada): naiveFloor(z::Complex) = floor(real(z)) + floor(imag(z))im
function aplFloor(z::Complex{T}) where T
r = real(z)
i = imag(z)
b = floor(r) + floor(i)im
# not sure whether this should be `rem` or `mod`..
x = mod(r, one(T))
y = mod(i, one(T))
if one(T) > x+y
return b
else
return b + (T(x ≥ y) + T(x < y)im)
end
end
If it works for reproducibility in your port, that's fine! Best to be aware of the caveats though. |
@Seelengrab Does the "magnitude" in the fractionality constraint need to be the "usual" magnitude, or could it be e.g. based on the infinity-norm? In this case, This definition extends nicely to other vector spaces. If one treats complex numbers as a vector space over reals, this is a natural extension. |
All references to magnitude in the sources I cited refer back to Gauss in the end, so it's the "usual" meaning, not any special norm. The difficulty is exactly that you can't really just pick a norm you like and say "this is the general
Keep in mind that the magnitude itself is irrelevant for I don't think choosing that definition for |
A priori I don't have a strong opinion over whether However, due to
I don't think it should be added now, unless breaking changes to |
In Julia 1.0 we have julia> round(1.1 + 2.9im, RoundDown)
1.0 + 2.0im
help?> round
...
Return the nearest integral value of the same type as the complex-valued `z` to `z`... |
That method can take two rounding modes though, not just one:
thereby sidestepping the issue of consistency. This issue is about |
Reopening since it was not clear cut why this should be implemented as a fall back. |
See JuliaLang#42060 for why this is not easy to define, and hence is (for now) better left to error, instead of working through a fallback.
I was recently porting a Matlab example to julia and I realized there's no direct equivalence.
I extended the floor function in my code snippet by doing:
I'm not sure if this would be a welcome addition, but it would surely make Julia code more similar to other well-used scientific software.
The text was updated successfully, but these errors were encountered: