-
Notifications
You must be signed in to change notification settings - Fork 40
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
Reshape accepts a single colon for AbstractArrays #220
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 6
OffsetArrays.jl/src/OffsetArrays.jl
Lines 288 to 289 in b74ae55
Base.reshape(A::OffsetArray, inds::Tuple{OffsetAxis,Vararg{OffsetAxis}}) = | |
OffsetArray(reshape(parent(A), map(_indexlength, inds)), map(_indexoffset, inds)) |
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.
@@ -8,7 +8,7 @@ _indexlength(i::Integer) = i | |||
_indexlength(i::Colon) = Colon() | |||
|
|||
_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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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. Colon
s and AbstractUnitRange
s, 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
reshape
uses_offset
to compute the offsets, but that function doesn't acceptColon
s. This PR fixes this.Now:
I'm not totally sure if the fix is correct, but
reshape
is pretty broken for non 1-based arrays to add more tests.