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
sub2ind bounds checking #8746
Comments
Yes, it definitely should check. Are you up for fixing it, or do you need someone else to do it? If you want to try it yourself but are quite new to julia, |
I'm up for fixing it, but won't get around to it until Thursday, if that's ok. |
It's gone this long without... |
Is this related to #4044? |
Not in any sense that I'm aware of---that's an issue about bounds-checking upon construction. I suppose the closest analog would be to check the first argument of
|
So I've looked into this and I'm not sure now that we want to do this. I've run a simple test and it just comes back much slower if I check the bounds. I feel like this is important because people (like myself) usually stick this in some inner loop and run it many times. If we don't bounds-check, the documentation definitely needs to be updated. An alternative might be to allow for optional bounds checking. What does anyone else think should be done? |
I suspect that the slowness was the reason for the lack of bounds checks in the first place. |
Seems like we have another usecase for #7799 |
@dressel, what if you use the |
Consider the following implementation: function getindex(a::SomeArray, i::Integer, j::Integer)
# ... check whether i and j are in valid ranges ...
a[sub2ind(size(a), i, j)]
end It actually performs bound-checking for multiple times:
We should definitely draw a line as to when/where we should do bounds checking, and how this works with |
I'm pretty ok with saying that sub2ind is just a utility function that does a particularly useful linear transformation of indices without doing any bounds checking. You still get bounds checks when you index into the actual array, so you can't index out of bounds entirely, you can just access an unexpected – but valid – element of the array. |
OTOH silently returning a nonsensical value for |
Something like below? check_bounds(siz, i, j) = (1 <= i <= siz[1] && 1 <= j <= siz[2]) || throw(BoundsError())
_sub2ind(siz, i, j) = i + siz[1] * (j-1)
@inline function sub2ind(siz, i, j)
check_bounds(siz, i, j)
_sub2ind(siz, i, j)
end People can call |
Yes, that's a manual way to do it, but perhaps cleaner would be to hook this into |
Isn't this closed in #25113? |
Indeed! |
I think sub2ind needs to do some bounds-checking. If I try something like:
Julia spits out 21. I was expecting it to throw an error. If you try the same thing in Matlab, it will do something like:
I haven't been able to find anything about this in the developer's or user's groups, but please let me know if there is some reasoning for this somewhere. I feel it should throw an error. If not, maybe it should be noted in the standard library documentation.
The text was updated successfully, but these errors were encountered: