Skip to content

Commit

Permalink
deprecate flipdim to reverse with a dims keyword argument (#26369)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson authored Mar 10, 2018
1 parent 3f848d5 commit 8c2065c
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 35 deletions.
12 changes: 6 additions & 6 deletions base/abstractarraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ julia> selectdim(A, 2, 3)
end

"""
flipdim(A, d::Integer)
reverse(A; dims::Integer)
Reverse `A` in dimension `d`.
Reverse `A` in dimension `dims`.
# Examples
```jldoctest
Expand All @@ -137,14 +137,14 @@ julia> b = [1 2; 3 4]
1 2
3 4
julia> flipdim(b,2)
julia> reverse(b, dims=2)
2×2 Array{Int64,2}:
2 1
4 3
```
"""
function flipdim(A::AbstractArray, d::Integer)
nd = ndims(A)
function reverse(A::AbstractArray; dims::Integer)
nd = ndims(A); d = dims
1 d nd || throw(ArgumentError("dimension $d is not 1 ≤ $d$nd"))
if isempty(A)
return copy(A)
Expand All @@ -160,7 +160,7 @@ function flipdim(A::AbstractArray, d::Integer)
indsd = inds[d]
sd = first(indsd)+last(indsd)
if nnd==nd
# flip along the only non-singleton dimension
# reverse along the only non-singleton dimension
for i in indsd
B[i] = A[sd-i]
end
Expand Down
4 changes: 4 additions & 0 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1396,6 +1396,10 @@ function reverse(A::AbstractVector, s=first(linearindices(A)), n=last(linearindi
end
return B
end

# to resolve ambiguity with reverse(A; dims)
reverse(A::Vector) = invoke(reverse, Tuple{AbstractVector}, A)

function reverseind(a::AbstractVector, i::Integer)
li = linearindices(a)
first(li) + last(li) - i
Expand Down
6 changes: 3 additions & 3 deletions base/arraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ end

## data movement ##

function flipdim(A::Array{T}, d::Integer) where T
nd = ndims(A)
function reverse(A::Array{T}; dims::Integer) where T
nd = ndims(A); d = dims
1 d nd || throw(ArgumentError("dimension $d is not 1 ≤ $d$nd"))
sd = size(A, d)
if sd == 1 || isempty(A)
Expand All @@ -73,7 +73,7 @@ function flipdim(A::Array{T}, d::Integer) where T
nnd += Int(size(A,i)==1 || i==d)
end
if nnd==nd
# flip along the only non-singleton dimension
# reverse along the only non-singleton dimension
for i = 1:sd
B[i] = A[sd+1-i]
end
Expand Down
6 changes: 3 additions & 3 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1205,8 +1205,8 @@ end

# TODO some of this could be optimized

function flipdim(A::BitArray, d::Integer)
nd = ndims(A)
function reverse(A::BitArray; dims::Integer)
nd = ndims(A); d = dims
1 d nd || throw(ArgumentError("dimension $d is not 1 ≤ $d$nd"))
sd = size(A, d)
sd == 1 && return copy(A)
Expand All @@ -1218,7 +1218,7 @@ function flipdim(A::BitArray, d::Integer)
nnd += Int(size(A,i)==1 || i==d)
end
if nnd == nd
# flip along the only non-singleton dimension
# reverse along the only non-singleton dimension
for i = 1:sd
B[i] = A[sd+1-i]
end
Expand Down
2 changes: 2 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,8 @@ export readandwrite
@deprecate cumprod(A::AbstractArray, dim::Integer) cumprod(A, dims=dim)
@deprecate cumprod!(B, A, dim::Integer) cumprod!(B, A, dims=dim)

@deprecate flipdim(A, d) reverse(A, dims=d)

# PR #25196
@deprecate_binding ObjectIdDict IdDict{Any,Any}

Expand Down
1 change: 0 additions & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,6 @@ export
fill!,
fill,
first,
flipdim,
hcat,
hvcat,
indexin,
Expand Down
1 change: 0 additions & 1 deletion doc/src/base/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ Base.vcat
Base.hcat
Base.hvcat
Base.vect
Base.flipdim
Base.circshift
Base.circshift!
Base.circcopy!
Expand Down
4 changes: 2 additions & 2 deletions test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -765,8 +765,8 @@ end
end
end

@testset "flipdim on empty" begin
@test flipdim(Diagonal([]),1) == Diagonal([])
@testset "reverse dim on empty" begin
@test reverse(Diagonal([]),dims=1) == Diagonal([])
end

@testset "ndims and friends" begin
Expand Down
28 changes: 14 additions & 14 deletions test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1337,20 +1337,20 @@ end
@test reverse!(Any[]) == Any[]
end

@testset "flipdim" begin
@test isequal(flipdim([2,3,1], 1), [1,3,2])
@test_throws ArgumentError flipdim([2,3,1], 2)
@test isequal(flipdim([2 3 1], 1), [2 3 1])
@test isequal(flipdim([2 3 1], 2), [1 3 2])
@test_throws ArgumentError flipdim([2,3,1], -1)
@test isequal(flipdim(1:10, 1), 10:-1:1)
@test_throws ArgumentError flipdim(1:10, 2)
@test_throws ArgumentError flipdim(1:10, -1)
@test isequal(flipdim(Matrix{Int}(undef, 0,0),1), Matrix{Int}(undef, 0,0)) # issue #5872
@testset "reverse dim" begin
@test isequal(reverse([2,3,1], dims=1), [1,3,2])
@test_throws ArgumentError reverse([2,3,1], dims=2)
@test isequal(reverse([2 3 1], dims=1), [2 3 1])
@test isequal(reverse([2 3 1], dims=2), [1 3 2])
@test_throws ArgumentError reverse([2,3,1], dims=-1)
@test isequal(reverse(1:10, dims=1), 10:-1:1)
@test_throws ArgumentError reverse(1:10, dims=2)
@test_throws ArgumentError reverse(1:10, dims=-1)
@test isequal(reverse(Matrix{Int}(undef, 0,0),dims=1), Matrix{Int}(undef, 0,0)) # issue #5872

a = rand(5,3)
@test flipdim(flipdim(a,2),2) == a
@test_throws ArgumentError flipdim(a,3)
@test reverse(reverse(a,dims=2),dims=2) == a
@test_throws ArgumentError reverse(a,dims=3)
end

@testset "isdiag, istril, istriu" begin
Expand Down Expand Up @@ -1954,8 +1954,8 @@ copyto!(S, A)
@test mapslices(sort, A, 1) == mapslices(sort, B, 1) == mapslices(sort, S, 1)
@test mapslices(sort, A, 2) == mapslices(sort, B, 2) == mapslices(sort, S, 2)

@test flipdim(A, 1) == flipdim(B, 1) == flipdim(S, 2)
@test flipdim(A, 2) == flipdim(B, 2) == flipdim(S, 2)
@test reverse(A, dims=1) == reverse(B, dims=1) == reverse(S, dims=2)
@test reverse(A, dims=2) == reverse(B, dims=2) == reverse(S, dims=2)

@test A .+ 1 == B .+ 1 == S .+ 1
@test 2*A == 2*B == 2*S
Expand Down
4 changes: 2 additions & 2 deletions test/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1036,9 +1036,9 @@ timesofar("binary comparison")
#for j = 1 : size(b1, d)
@check_bit_operation selectdim(b1, d, j) SubArray{Bool, 3, BitArray{4}}
#end
@check_bit_operation flipdim(b1, d) BitArray{4}
@check_bit_operation reverse(b1, dims=d) BitArray{4}
end
@test_throws ArgumentError flipdim(b1, 5)
@test_throws ArgumentError reverse(b1, dims=5)

b1 = bitrand(n1, n2)
for k = 1:4
Expand Down
4 changes: 2 additions & 2 deletions test/offsetarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,8 @@ v = OffsetArray(rand(8), (-2,))

@test rotl90(A) == OffsetArray(rotl90(parent(A)), A.offsets[[2,1]])
@test rotr90(A) == OffsetArray(rotr90(parent(A)), A.offsets[[2,1]])
@test flipdim(A, 1) == OffsetArray(flipdim(parent(A), 1), A.offsets)
@test flipdim(A, 2) == OffsetArray(flipdim(parent(A), 2), A.offsets)
@test reverse(A, dims=1) == OffsetArray(reverse(parent(A), dims=1), A.offsets)
@test reverse(A, dims=2) == OffsetArray(reverse(parent(A), dims=2), A.offsets)

@test A .+ 1 == OffsetArray(parent(A) .+ 1, A.offsets)
@test 2*A == OffsetArray(2*parent(A), A.offsets)
Expand Down
2 changes: 1 addition & 1 deletion test/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ let
end

# ref issue #17351
@test @inferred(flipdim(view([1 2; 3 4], :, 1), 1)) == [3, 1]
@test @inferred(reverse(view([1 2; 3 4], :, 1), dims=1)) == [3, 1]

let
s = view(reshape(1:6, 2, 3), 1:2, 1:2)
Expand Down

0 comments on commit 8c2065c

Please sign in to comment.