-
-
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
Inconsistent ranges of CartesianIndex
#43336
Comments
The default step-size for ranges is one, even if start and end agree. In your example the step-size in the second dimension should be one, not zero. |
See also the discussions in #41959 and #41960 There are two valid interpretations of
|
@stev47 Can you please not just close an issue where I already mention that this error is due to the assumption that the cartesian product of each dimension "just works" for the expansion over a nonzero step? The discourse discussion you're linking is also not relevant here - @johnnychen94 Yes, I've read & participated in both of those discussions. I don't think they apply here. My complaint is not with In short, I think this should only error if the step is |
Maybe let me expand on the motivation here - creating a cartesian product is not my main goal. I'm mainly interested in getting steps like |
@Seelengrab I see your point now. We should revive the discussion #15218 first; once |
I'm not sure we have to solve the general case mentioned in that issue for this to be possible - simply ignoring the dimensions with |
Yes, that's a shortcut but I think it's essentially no different to the general 1-d range case. I understand your point that # something like this :)
# https://github.com/JuliaLang/julia/blob/ca11c2fc24a64a083c0b06a4abcdfb41c0c4f2cb/base/multidimensional.jl#L317-L318
function Base.:(:)(I::CartesianIndex{N}, S::CartesianIndex{N}, J::CartesianIndex{N}) where N
R = map(Tuple(I), Tuple(S), Tuple(J)) do i, s, j
if iszero(s) && i == j
i:one(i):i
else
i:s:j
end
end
CartesianIndices(R)
end |
@Seelengrab sorry for being hasty but no harm was done to you. |
No worries! Just felt very dismissive, as I've explicitly mentioned that I'm concerned with nonzero steps. Maybe I could have phrased that more clearly in the initial post.
That is orthogonal to this issue though, as |
I see, then it is indeed more viable. CartesianIndex(a):CartesianIndex(s):CartesianIndex(b) ==
CartesianIndex.(Iterators.product(map((a, s, b) -> a:s:b, a, s, b)...)) Not sure how valuable that consistency is. |
I'd have expected these two to be equivalent, but the second one errors:
Unintuitively, the "correct" way to write this would be
ci(1,1):ci(1,x):ci(3,1)
for any valuex
other than0
:I think this is a consequence of how the cartesian product is formed right now - maybe dimensions where the step is zero can be just skipped during product expansion, provided both
start
andstop
of the to-be-constructed range have the same coordinate in that point? Since no dimensions are lost, it should still be type stable.The text was updated successfully, but these errors were encountered: