-
Notifications
You must be signed in to change notification settings - Fork 41
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
support CartesianIndex offset #132
Comments
One may convert it to a OffsetArray(rand(4, 4), Tuple(CartesianIndex(-1, -1))) or extract the underlying OffsetArray(rand(4, 4), CartesianIndex(-1, -1).I) I wonder if this calls for a specialized constructor? |
There are two behaviors wrt this as alternatives:
When I opened I was thinking of the first semantic, but now I kind of prefer the second as it brings new functionalities. (and it reasons well to me) Thoughts? |
I like this idea, it removes the extra step required to compute the offset corresponding to a specific origin. I just wonder if this might be non-intuitive? Eg if broadcasting is required, it's been suggested that one should use julia> OffsetVector(ones(2), Tuple(CartesianIndex(1)) .+ 0) |> axes
(2:3,) whereas the latter would have axes julia> OffsetVector(ones(2), CartesianIndex(1)) |> axes
(1:2,) Then again, a |
Yes, this is a bit non-intuitive (or more accurately speaking, ambiguous), especially for those who are not familiar with the purpose of CartesianIndex. The logic behind reasons well to me.
If we go with option 1, we'd still need such a helper, probably in other names. I don't have a strong opinion here, ping @timholy for a triage |
Interesting questions. I'd say that having CartesianIndex behaving differently from tuples would be a bit too confusing. But we could add a |
Sorry, finger slipped on the mouse pad and hit the wrong button. 😛 |
I guess this confusion is avoided by using a different type that behaves analogous to a Taking this one step further, I wonder if it might make sense to have a special constructor where all the dimensions have the same origin. This might make the transition from zero-based indexing easier. Something like this might work: julia> struct NBasedIndexing
n :: Int
end;
julia> function OffsetArrays.OffsetArray(arr::AbstractArray{<:Any,N}, indsorigin::NBasedIndexing) where {N}
OffsetArray(arr, ntuple(d -> indsorigin.n - first(axes(arr, d)), Val{N}()))
end;
julia> const ZeroBasedIndexing = NBasedIndexing(0);
julia> OffsetArray(zeros(3,2), ZeroBasedIndexing)
3×2 OffsetArray(::Array{Float64,2}, 0:2, 0:1) with eltype Float64 with indices 0:2×0:1:
0.0 0.0
0.0 0.0
0.0 0.0 |
This sounds like a good idea to me. I can play with it in the next couple of days. |
The following usage could be helpful to deal with multi-dimensional arrays
OffsetArray(rand(4, 4), CartesianIndex(-1, -1))
I could do it later if you think this is a good idea.
The text was updated successfully, but these errors were encountered: