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

Make reshape accept a combination of Integers, Base.OneTo and a single Colon consistently #41069

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions base/reshapedarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,15 @@ julia> reshape(1:6, 2, 3)
"""
reshape

reshape(parent::AbstractArray, dims::IntOrInd...) = reshape(parent, dims)
reshape(parent::AbstractArray, dims::Union{Integer, Colon, AbstractUnitRange{<:Integer}}...) = reshape(parent, dims)
reshape(parent::AbstractArray, shp::Tuple{Integer, Vararg{Integer}}) = reshape(parent, to_shape(shp))
reshape(parent::AbstractArray, shp::Tuple{Union{Integer,OneTo}, Vararg{Union{Integer,OneTo}}}) = reshape(parent, to_shape(shp))
reshape(parent::AbstractArray, dims::Dims) = _reshape(parent, dims)

# Allow missing dimensions with Colon():
reshape(parent::AbstractVector, ::Colon) = parent
reshape(parent::AbstractVector, ::Tuple{Colon}) = parent
reshape(parent::AbstractArray, dims::Int...) = reshape(parent, dims)
reshape(parent::AbstractArray, dims::Union{Int,Colon}...) = reshape(parent, dims)
reshape(parent::AbstractArray, dims::Tuple{Vararg{Union{Int,Colon}}}) = reshape(parent, _reshape_uncolon(parent, dims))
reshape(parent::AbstractArray, dims::Tuple{Vararg{Union{Integer,Colon,Base.OneTo}}}) = reshape(parent, _reshape_uncolon(parent, dims))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like it should be:

Suggested change
reshape(parent::AbstractArray, dims::Tuple{Vararg{Union{Integer,Colon,Base.OneTo}}}) = reshape(parent, _reshape_uncolon(parent, dims))
reshape(parent::AbstractArray, dims::Tuple) = reshape(parent, _reshape_uncolon(parent, dims))

wdyt?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it, although perhaps worth mentioning that this almost makes _reshape_uncolon part of the API (it becomes a tempting target for package specialization).

@inline function _reshape_uncolon(A, dims)
@noinline throw1(dims) = throw(DimensionMismatch(string("new dimensions $(dims) ",
"may have at most one omitted dimension specified by `Colon()`")))
Expand All @@ -125,7 +124,7 @@ reshape(parent::AbstractArray, dims::Tuple{Vararg{Union{Int,Colon}}}) = reshape(
pre = _before_colon(dims...)
post = _after_colon(dims...)
_any_colon(post...) && throw1(dims)
sz, remainder = divrem(length(A), prod(pre)*prod(post))
sz, remainder = divrem(length(A), prod(to_shape(pre))*prod(to_shape(post)))
remainder == 0 || throw2(A, dims)
(pre..., Int(sz), post...)
end
Expand Down
20 changes: 20 additions & 0 deletions test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1566,6 +1566,26 @@ end
@test isempty(keepat!(a, []))
end

@testset "reshape may mix axes, Integers and colon" begin
for a in Any[1:3, collect(1:3), reshape(collect(1:3), 1, 1, 3)]
r13 = reshape(a, 1, 3)
r31 = reshape(a, 3, 1)
@test reshape(a, Int8(1), Int8(3)) == r13
@test reshape(a, 1, Int8(3)) == r13
@test reshape(a, 1, Base.OneTo(3)) == r13
@test reshape(a, 1, Base.OneTo(Int8(3))) == r13
@test reshape(a, 1, :) == r13
@test reshape(a, :, 1) == r31
@test reshape(a, Base.OneTo(1), :) == r13
@test reshape(a, :, Base.OneTo(1)) == r31
@test reshape(a, Base.OneTo(Int8(1)), :) == r13
@test reshape(a, Base.OneTo(1), Int8(3)) == r13
@test reshape(a, Base.OneTo(Int8(1)), Int8(3)) == r13
end
a = 1:3
@test reshape(a, axes(a, 1), axes(a,2)) == reshape(a, size(a, 1), size(a,2))
end

@testset "reshape methods for AbstractVectors" begin
r = Base.IdentityUnitRange(3:4)
@test reshape(r, :) === reshape(r, (:,)) === r
Expand Down