-
-
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
Fix colon-reshaping of OffsetVector #33890
Conversation
Will this come to 1.0 LTS? |
If someone adds a 1.0 backport. Let's see what reviewers think. |
This is the result of this change with
It seems unexpected. Maybe After JuliaArrays/OffsetArrays.jl#84 this problem doesn't occur. |
You're right that However, in general In general Base has no way of knowing how to change the indexing of julia> using CatIndices, OffsetArrays
julia> a = OffsetArray([1.0, 2.0], 0:1)
2-element OffsetArray(::Array{Float64,1}, 0:1) with eltype Float64 with indices 0:1:
1.0
2.0
julia> b = BidirectionalVector([2.0])
1-element BidirectionalVector{Float64} with indices CatIndices.URange(1,1):
2.0
julia> pushfirst!(b, 1.0)
2-element BidirectionalVector{Float64} with indices CatIndices.URange(0,1):
1.0
2.0
julia> axes(a)
(Base.IdentityUnitRange(0:1),)
julia> axes(b)
(CatIndices.URange(0,1),)
julia> reshape(a, :)
2-element OffsetArray(::Array{Float64,1}, 0:1) with eltype Float64 with indices 0:1:
1.0
2.0
julia> reshape(b, :)
2-element BidirectionalVector{Float64} with indices CatIndices.URange(0,1):
1.0
2.0
julia> reshape(a, 2)
2-element Array{Float64,1}:
1.0
2.0
julia> reshape(b, 2)
ERROR: ArgumentError: offset arrays are not supported but got an array with index other than 1
Stacktrace:
[1] require_one_based_indexing at ./abstractarray.jl:89 [inlined]
[2] _reshape at ./reshapedarray.jl:168 [inlined]
[3] reshape at ./reshapedarray.jl:112 [inlined]
[4] reshape(::BidirectionalVector{Float64}, ::Int64) at ./reshapedarray.jl:116
[5] top-level scope at REPL[17]:1 That's fine because And now let's combine them: julia> c = OffsetArray(b, -5)
2-element OffsetArray(::BidirectionalVector{Float64}, -5:-4) with eltype Float64 with indices -5:-4:
1.0
2.0
julia> reshape(c, :)
2-element OffsetArray(::BidirectionalVector{Float64}, -5:-4) with eltype Float64 with indices -5:-4:
1.0
2.0
julia> reshape(c, 2)
ERROR: ArgumentError: offset arrays are not supported but got an array with index other than 1
Stacktrace:
[1] require_one_based_indexing at ./abstractarray.jl:89 [inlined]
[2] _reshape at ./reshapedarray.jl:168 [inlined]
[3] reshape at ./reshapedarray.jl:112 [inlined]
[4] reshape at /home/tim/.julia/packages/OffsetArrays/vIbpP/src/OffsetArrays.jl:113 [inlined]
[5] reshape(::OffsetArray{Float64,1,BidirectionalVector{Float64}}, ::Int64) at ./reshapedarray.jl:116
[6] top-level scope at REPL[20]:1 This seems to be the right behavior, AFAICT. |
The behavior about the colon seems to be inconsistent with the result for n-dim array.
In 2-dim array,
My understanding is that function |
The difference between 1- and higher dimensions is documented here. In particular, the part about linear indexing is relevant; reshaping is essentially an appeal to the linear index representation, and that differs for 1-dimensional objects (for which the linear index can start anywhere) and higher-dimensional objects (for which linear indexing always starts at 1).
I guess that's not unreasonable. I can't really think about it carefully now but will try to do so over the weekend. |
* Fix colon-reshaping of OffsetVector * reshape(::AbstractVector, ::Colon) is a no-op (cherry picked from commit f80c3ee)
* Fix colon-reshaping of OffsetVector * reshape(::AbstractVector, ::Colon) is a no-op (cherry picked from commit f80c3ee)
* Fix colon-reshaping of OffsetVector * reshape(::AbstractVector, ::Colon) is a no-op (cherry picked from commit f80c3ee)
I guess this caused OffsetArrays to start failing?
cc @timholy |
Fixed in OffsetArrays v0.11.3 |
* Fix colon-reshaping of OffsetVector * reshape(::AbstractVector, ::Colon) is a no-op (cherry picked from commit f80c3ee)
* Fix colon-reshaping of OffsetVector * reshape(::AbstractVector, ::Colon) is a no-op (cherry picked from commit f80c3ee)
* Fix colon-reshaping of OffsetVector * reshape(::AbstractVector, ::Colon) is a no-op (cherry picked from commit f80c3ee)
* Fix colon-reshaping of OffsetVector * reshape(::AbstractVector, ::Colon) is a no-op (cherry picked from commit f80c3ee)
* Fix colon-reshaping of OffsetVector * reshape(::AbstractVector, ::Colon) is a no-op (cherry picked from commit f80c3ee)
* Fix colon-reshaping of OffsetVector * reshape(::AbstractVector, ::Colon) is a no-op
Fixes #33614