From e254c0100aa421258d135913f70ab079d6b1e9f6 Mon Sep 17 00:00:00 2001 From: Matt Bauman Date: Tue, 29 Apr 2014 16:45:11 -0400 Subject: [PATCH] Add tests for reshape with an omitted dimension --- base/reshapedarray.jl | 8 +++++--- test/arrayops.jl | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/base/reshapedarray.jl b/base/reshapedarray.jl index 5cd7b1dc7813cf..bebd47098ed072 100644 --- a/base/reshapedarray.jl +++ b/base/reshapedarray.jl @@ -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. @@ -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}}) = @@ -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) diff --git a/test/arrayops.jl b/test/arrayops.jl index c073f0b21751a3..a1ce8cded49e8b 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -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