Skip to content
Closed
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
2 changes: 1 addition & 1 deletion base/permuteddimsarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 12 additions & 1 deletion base/reshapedarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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}}})
Copy link
Member Author

@jishnub jishnub May 29, 2021

Choose a reason for hiding this comment

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

This uses a general Tuple instead of one with a union of valid types to allow OffsetArrays to define methods for AbstractUnitRange arguments that return OffsetArrays


@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():
Expand Down
12 changes: 12 additions & 0 deletions test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions test/offsetarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions test/testhelpers/OffsetArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down