Reshape accepts a single colon for AbstractArrays#220
Reshape accepts a single colon for AbstractArrays#220jishnub merged 3 commits intoJuliaArrays:masterfrom
Conversation
Codecov Report
@@ Coverage Diff @@
## master #220 +/- ##
=======================================
Coverage 98.33% 98.33%
=======================================
Files 5 5
Lines 301 301
=======================================
Hits 296 296
Misses 5 5
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
When it comes to OffsetArray, the axes seems not correct to me:
julia> A1 = OffsetArray([1 2 3; 4 5 6], -1, -1)
2×3 OffsetArray(::Matrix{Int64}, 0:1, 0:2) with eltype Int64 with indices 0:1×0:2:
1 2 3
4 5 6
julia> reshape(A1, 2:4, :)
3×2 OffsetArray(::Matrix{Int64}, 2:4, 1:2) with eltype Int64 with indices 2:4×1:2:
1 5
4 3
2 6
julia> reshape(A1, 2:3, :)
2×3 OffsetArray(::Matrix{Int64}, 2:3, 1:3) with eltype Int64 with indices 2:3×1:3:
1 2 3
4 5 6OffsetArrays.jl/src/OffsetArrays.jl
Lines 288 to 289 in b74ae55
I think the problem here is that we strip the OffsetArray wrapper, and thus loss the : information. Hence we might need to manually construct the correct axes of : by using A instead of parent(A) here.
|
|
||
| _offset(axparent::AbstractUnitRange, ax::AbstractUnitRange) = first(ax) - first(axparent) | ||
| _offset(axparent::AbstractUnitRange, ax::Integer) = 1 - first(axparent) | ||
| _offset(axparent::AbstractUnitRange, ::Union{Integer, Colon}) = 1 - first(axparent) |
There was a problem hiding this comment.
We can let OffsetArray constructor decide how to deal with Colon:
| _offset(axparent::AbstractUnitRange, ::Union{Integer, Colon}) = 1 - first(axparent) | |
| _offset(axparent::AbstractUnitRange, ::Integer) = 1 - first(axparent) | |
| _offset(axparent::AbstractUnitRange, ::Colon) = Colon() |
There was a problem hiding this comment.
We may do this, but this would mean interpreting Colon as an offset as opposed to an axis as we do now. So after this a Tuple{Vararg{Union{Integer, Colon}}} may be seen as offsets and other combinations (eg. Colons and AbstractUnitRanges, CartesianIndices etc) as axes. I think it should be clear from the context what the meaning of the colon is.
|
I'm not sure what the correct choice is here. Eg. in the first example, |
|
I don't think we have ever interpreted
|
|
I meant that |
I give it an implementation in #228 |
johnnychen94
left a comment
There was a problem hiding this comment.
Since this one only extends the current behavior, I'm good with it if you want to have it merged. In the meantime, we can keep #228 an 2.0 issue.
Currently this is broken as
reshapeuses_offsetto compute the offsets, but that function doesn't acceptColons. This PR fixes this.Now:
I'm not totally sure if the fix is correct, but
reshapeis pretty broken for non 1-based arrays to add more tests.