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

Documentation on Slice, LinearIndices, etc. with custom array indexing #36830

Open
damiendr opened this issue Jul 28, 2020 · 1 comment
Open
Labels
domain:arrays [a, r, r, a, y, s] domain:docs This change adds or pertains to documentation

Comments

@damiendr
Copy link
Contributor

It would be nice to have some documentation on the use of Slice, LinearIndices and CartesianIndices for non-1-based indexing.

For instance I just found out in #28503 that the following works:

julia> li = LinearIndices((Base.Slice(1:128), Base.Slice(0:127)))
julia> li[1,0]
1

Until then I had unsuccessfully tried the following:

julia> li = LinearIndices((1:128, 0:127))
julia> li = LinearIndices((1:128, ZeroRange(128)))

which both give

julia> li[1,0]
ERROR: BoundsError: attempt to access 128×128 LinearIndices{2,Tuple{UnitRange{Int64},UnitRange{Int64}}} at index [1, 0]

But I can't find any official docs on Slice or why it works differently from ZeroRange and UnitRange, or an explanation of the rationale for the implicit 1-based conversions.

Example of things I would have liked to find in the docs:

  • what design choices require that LinearIndices((0:9,0:9)) == LinearIndices((1:10, 1:10))?

  • if I can do:

julia> CartesianIndices((Base.Slice(0:9), Base.Slice(0:9)))[0,0]
CartesianIndex(0, 0)
julia> CartesianIndices((0:9, 0:9))[1,1]
CartesianIndex(0, 0)

is there a built-in way to construct an expression SomeIndices such that:

julia> SomeIndices((0:9, 0:9))[0,0]
CartesianIndex(1,1)

other than allocating a complete OffsetArray with the indices?

Maybe this belongs to the dev/offset arrays docs?

@mbauman mbauman added domain:arrays [a, r, r, a, y, s] domain:docs This change adds or pertains to documentation labels Jul 28, 2020
@mbauman
Copy link
Sponsor Member

mbauman commented Jul 28, 2020

Yes, this can and should be improved. Quick comment right off the bat: you want to use Base.IdentityUnitRange instead of Base.Slice. I'll try to add more later, but some details are in discourse:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:arrays [a, r, r, a, y, s] domain:docs This change adds or pertains to documentation
Projects
None yet
Development

No branches or pull requests

2 participants