Skip to content

Commit

Permalink
Add tests for reshape with an omitted dimension
Browse files Browse the repository at this point in the history
  • Loading branch information
mbauman committed Jan 8, 2017
1 parent a80c09b commit e254c01
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
8 changes: 5 additions & 3 deletions base/reshapedarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ length(R::ReshapedArrayIterator) = length(R.iter)
Return an array with the same data as the given array, but with different dimensions.
The new dimensions may be specified either as a list of arguments or as a shape
tuple. Up to one dimension may be specified with a `:`, in which case its
tuple. At most one dimension may be specified with a `:`, in which case its
length is computed such that its product with all the specified dimensions is
equal to the length of the original array A.
Expand Down Expand Up @@ -88,8 +88,10 @@ reshape(parent::AbstractArray, shp::NeedsShaping) = reshape(parent, to_shape(shp
reshape(parent::AbstractArray, dims::Dims) = _reshape(parent, dims)

# Allow missing dimensions with Colon():
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(A, dims))
reshape(parent::AbstractArray, dims::Tuple{Vararg{Int}}) = _reshape(parent, dims)
reshape(parent::AbstractArray, dims::Tuple{Vararg{Union{Int,Colon}}}) = _reshape(parent, _reshape_uncolon(parent, dims))
# Recursively move dimensions to pre and post tuples, splitting on the Colon
@inline _reshape_uncolon(A, dims) = _reshape_uncolon(A, (), nothing, (), dims)
@inline _reshape_uncolon(A, pre, c::Void, post, dims::Tuple{Any, Vararg{Any}}) =
Expand All @@ -99,7 +101,7 @@ reshape(parent::AbstractArray, dims::Tuple{Vararg{Union{Int,Colon}}}) = _reshape
@inline _reshape_uncolon(A, pre, c::Colon, post, dims::Tuple{Any, Vararg{Any}}) =
_reshape_uncolon(A, pre, c, (post..., dims[1]), tail(dims))
_reshape_uncolon(A, pre, c::Colon, post, dims::Tuple{Colon, Vararg{Any}}) =
throw(DimensionMismatch("new dimensions $((pre..., c, post..., dims...)) may only have up to one omitted dimension specified by Colon()"))
throw(DimensionMismatch("new dimensions $((pre..., c, post..., dims...)) may only have at most one omitted dimension specified by Colon()"))
@inline function _reshape_uncolon(A, pre, c::Colon, post, dims::Tuple{})
sz, remainder = divrem(length(A), prod(pre)*prod(post))
remainder == 0 || _throw_reshape_colon_dimmismatch(A, pre, post)
Expand Down
21 changes: 21 additions & 0 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,27 @@ end
@test isa(reshape(s, Val{N}), Base.ReshapedArray{Int,N})
end
end
@testset "reshape with colon" begin
# Reshape with an omitted dimension
let A = linspace(1, 60, 60)
@test size(reshape(A, :)) == (60,)
@test size(reshape(A, :, 1)) == (60, 1)
@test size(reshape(A, (:, 2))) == (30, 2)
@test size(reshape(A, 3, :)) == (3, 20)
@test size(reshape(A, 2, 3, :)) == (2, 3, 10)
@test size(reshape(A, (2, :, 5))) == (2, 6, 5)
@test_throws DimensionMismatch reshape(A, 7, :)
@test_throws DimensionMismatch reshape(A, :, 2, 3, 4)
@test_throws DimensionMismatch reshape(A, (:, :))

B = rand(2,2,2,2)
@test size(reshape(B, :)) == (16,)
@test size(reshape(B, :, 4)) == (4, 4)
@test size(reshape(B, (2, 1, :))) == (2, 1, 8)
@test_throws DimensionMismatch reshape(B, 3, :)
@test_throws DimensionMismatch reshape(B, :, :, 2, 2)
end
end

@test reshape(1:5, (5,)) === 1:5
@test reshape(1:5, 5) === 1:5
Expand Down

0 comments on commit e254c01

Please sign in to comment.