diff --git a/base/permuteddimsarray.jl b/base/permuteddimsarray.jl index ea966c44efc38..185251781769a 100644 --- a/base/permuteddimsarray.jl +++ b/base/permuteddimsarray.jl @@ -188,7 +188,7 @@ julia> transpose(V) [1 3; 2 4] [5 7; 6 8] ``` """ -permutedims(v::AbstractVector) = reshape(v, (1, length(v))) +permutedims(v::AbstractVector) = reshape(v, (1, axes(v,1))) """ permutedims!(dest, src, perm) diff --git a/base/reshapedarray.jl b/base/reshapedarray.jl index 671dd2d86a840..9c081a2de41a4 100644 --- a/base/reshapedarray.jl +++ b/base/reshapedarray.jl @@ -107,8 +107,19 @@ julia> reshape(1:6, 2, 3) """ reshape -reshape(parent::AbstractArray, dims::IntOrInd...) = reshape(parent, dims) +reshape(parent::AbstractArray, dims::Union{Integer, AbstractUnitRange{<:Integer}, Colon}...) = reshape(parent, dims) reshape(parent::AbstractArray, shp::Tuple{Union{Integer,OneTo}, Vararg{Union{Integer,OneTo}}}) = reshape(parent, to_shape(shp)) +reshape(parent::AbstractArray, shp::Tuple) = reshape(parent, map(_toshape, shp)::Tuple{Vararg{Union{Int,Colon}}}) + +@inline function _toshape(r::AbstractUnitRange{<:Integer}) + @noinline throw(r) = "range arguments to reshape must begin at 1, received $r" + first(r) == 1 || throw(r) + to_shape(OneTo(r)) +end +_toshape(r::Integer) = to_shape(r) +_toshape(x::Colon) = x +_toshape(x) = throw(ArgumentError("reshape accepts Integers, AbstractUnitRanges or Colons as arguments, received $x")) + reshape(parent::AbstractArray, dims::Dims) = _reshape(parent, dims) # Allow missing dimensions with Colon(): diff --git a/test/abstractarray.jl b/test/abstractarray.jl index 9af20b8047701..47c60aa5a5de6 100644 --- a/test/abstractarray.jl +++ b/test/abstractarray.jl @@ -1385,3 +1385,15 @@ end @test_throws ArgumentError keepat!(a, [2, 1]) @test isempty(keepat!(a, [])) end + +@testset "reshape with Integers" begin + @test reshape(1:2, 1, 1:2) == reshape(1:2, 1, 2) + @test reshape(1:2, 1, Int32(2)) == reshape(1:2, 1, 2) + @test reshape(1:2, 1, :, Base.OneTo(Int32(2))) == reshape(1:2, 1, 1, 2) + @test reshape(reshape(1:2, Int32(2), 1), 1, Int32(2)) == reshape(1:2, 1, 2) + @test reshape(reshape(1:2, Int32(2), 1), :, Int32(2)) == reshape(1:2, 1, 2) + @test reshape(reshape(1:2, Int32(2), 1), :, Int32(2), Base.OneTo(1)) == reshape(1:2, 1, 2, 1) + @test_throws Exception reshape(1:2, 1, "a") + @test_throws Exception reshape(1:2, (1, "a")) + @test_throws Exception reshape(1:2, (1, :, "a")) +end diff --git a/test/offsetarray.jl b/test/offsetarray.jl index 811d3dd26f509..2401bf2eda282 100644 --- a/test/offsetarray.jl +++ b/test/offsetarray.jl @@ -795,3 +795,7 @@ end @test Iterators.partition(OffsetArray(reshape(collect(1:9),3,3), (3,3)), 5) |> collect == [1:5,6:9] #OffsetMatrix @test Iterators.partition(IdOffsetRange(2:7,10), 5) |> collect == [12:16,17:17] # IdOffsetRange end + +@testset "permutedims" begin + @test axes(permutedims(ones(2:3))) == (1:1, 2:3) +end diff --git a/test/testhelpers/OffsetArrays.jl b/test/testhelpers/OffsetArrays.jl index 67de3ef476652..0b6beef6deab6 100644 --- a/test/testhelpers/OffsetArrays.jl +++ b/test/testhelpers/OffsetArrays.jl @@ -132,11 +132,11 @@ _indexoffset(r::AbstractRange) = first(r) - 1 _indexoffset(i::Integer) = 0 _indexoffset(i::Colon) = 0 _indexlength(r::AbstractRange) = length(r) -_indexlength(i::Integer) = i +_indexlength(i::Integer) = Int(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) abstract type AxisConversionStyle end struct SingleRange <: AxisConversionStyle end