diff --git a/base/abstractarray.jl b/base/abstractarray.jl index b85ee9fa608d7..fc550a1a9c2f7 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -1825,7 +1825,7 @@ foreach(f, itrs...) = (for z in zip(itrs...); f(z...); end; nothing) ## dims specifies which dimensions will be transformed. for example ## dims==1:2 will call f on all slices A[:,:,...] """ - mapslices(f, A, dims) + mapslices(f, A; dims) Transform the given dimensions of array `A` using function `f`. `f` is called on each slice of `A` of the form `A[...,:,...,:,...]`. `dims` is an integer vector specifying where the @@ -1853,7 +1853,7 @@ julia> a = reshape(Vector(1:16),(2,2,2,2)) 13 15 14 16 -julia> mapslices(sum, a, [1,2]) +julia> mapslices(sum, a, dims = [1,2]) 1×1×2×2 Array{Int64,4}: [:, :, 1, 1] = 10 @@ -1868,11 +1868,13 @@ julia> mapslices(sum, a, [1,2]) 58 ``` """ -mapslices(f, A::AbstractArray, dims) = mapslices(f, A, [dims...]) -function mapslices(f, A::AbstractArray, dims::AbstractVector) +function mapslices(f, A::AbstractArray; dims) if isempty(dims) return map(f,A) end + if !isa(dims, AbstractVector) + dims = [dims...] + end dimsA = [axes(A)...] ndimsA = ndims(A) diff --git a/base/deprecated.jl b/base/deprecated.jl index 8c93747d6090d..cd37516e303dc 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1373,6 +1373,7 @@ export readandwrite @deprecate mapreducedim(f, op, A::AbstractArray, dims, v0) mapreduce(f, op, v0, A, dims=dims) @deprecate reducedim(op, A::AbstractArray, dims) reduce(op, A, dims=dims) @deprecate reducedim(op, A::AbstractArray, dims, v0) reduce(op, v0, A, dims=dims) +@deprecate mapslices(op, A::AbstractArray, dims) mapslices(op, A, dims=dims) @deprecate sort(A::AbstractArray, dim::Integer; kwargs...) sort(A; kwargs..., dims=dim) diff --git a/base/reducedim.jl b/base/reducedim.jl index c8d5e720b6f89..1ea3ab83c863e 100644 --- a/base/reducedim.jl +++ b/base/reducedim.jl @@ -95,8 +95,8 @@ function reducedim_initarray0(A::AbstractArray{T}, region, f, ops) where T end end -reducedim_initarray0_empty(A::AbstractArray, region, f, ops) = mapslices(x->ops(f.(x)), A, region) -reducedim_initarray0_empty(A::AbstractArray, region,::typeof(identity), ops) = mapslices(ops, A, region) +reducedim_initarray0_empty(A::AbstractArray, region, f, ops) = mapslices(x->ops(f.(x)), A, dims = region) +reducedim_initarray0_empty(A::AbstractArray, region,::typeof(identity), ops) = mapslices(ops, A, dims = region) # TODO: better way to handle reducedim initialization # diff --git a/base/statistics.jl b/base/statistics.jl index 398117f22cf0e..31f5630635b76 100644 --- a/base/statistics.jl +++ b/base/statistics.jl @@ -170,7 +170,7 @@ equivalent to calculating mean of two median elements. """ median(v::AbstractArray; dims=:) = _median(v, dims) -_median(v::AbstractArray, dims) = mapslices(median!, v, dims) +_median(v::AbstractArray, dims) = mapslices(median!, v, dims = dims) _median(v::AbstractArray{T}, ::Colon) where {T} = median!(copyto!(Array{T,1}(undef, _length(v)), v)) diff --git a/test/arrayops.jl b/test/arrayops.jl index b2ebd127ccd50..0b4479181cf68 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -1006,57 +1006,57 @@ end @testset "mapslices" begin local a, b, c, m, h, s a = rand(5,5) - s = mapslices(sort, a, [1]) - S = mapslices(sort, a, [2]) + s = mapslices(sort, a, dims=[1]) + S = mapslices(sort, a, dims=[2]) for i = 1:5 @test s[:,i] == sort(a[:,i]) @test vec(S[i,:]) == sort(vec(a[i,:])) end # issue #3613 - b = mapslices(sum, fill(1.,2,3,4), [1,2]) + b = mapslices(sum, fill(1.,2,3,4), dims=[1,2]) @test size(b) === (1,1,4) @test all(b.==6) # issue #5141 ## Update Removed the version that removes the dimensions when dims==1:ndims(A) - c1 = mapslices(x-> maximum(-x), a, []) + c1 = mapslices(x-> maximum(-x), a, dims=[]) @test c1 == -a # other types than Number - @test mapslices(prod,["1" "2"; "3" "4"],1) == ["13" "24"] - @test mapslices(prod,["1"],1) == ["1"] + @test mapslices(prod,["1" "2"; "3" "4"],dims=1) == ["13" "24"] + @test mapslices(prod,["1"],dims=1) == ["1"] # issue #5177 c = fill(1,2,3,4) - m1 = mapslices(x-> fill(1,2,3), c, [1,2]) - m2 = mapslices(x-> fill(1,2,4), c, [1,3]) - m3 = mapslices(x-> fill(1,3,4), c, [2,3]) + m1 = mapslices(x-> fill(1,2,3), c, dims=[1,2]) + m2 = mapslices(x-> fill(1,2,4), c, dims=[1,3]) + m3 = mapslices(x-> fill(1,3,4), c, dims=[2,3]) @test size(m1) == size(m2) == size(m3) == size(c) - n1 = mapslices(x-> fill(1,6), c, [1,2]) - n2 = mapslices(x-> fill(1,6), c, [1,3]) - n3 = mapslices(x-> fill(1,6), c, [2,3]) - n1a = mapslices(x-> fill(1,1,6), c, [1,2]) - n2a = mapslices(x-> fill(1,1,6), c, [1,3]) - n3a = mapslices(x-> fill(1,1,6), c, [2,3]) + n1 = mapslices(x-> fill(1,6), c, dims=[1,2]) + n2 = mapslices(x-> fill(1,6), c, dims=[1,3]) + n3 = mapslices(x-> fill(1,6), c, dims=[2,3]) + n1a = mapslices(x-> fill(1,1,6), c, dims=[1,2]) + n2a = mapslices(x-> fill(1,1,6), c, dims=[1,3]) + n3a = mapslices(x-> fill(1,1,6), c, dims=[2,3]) @test size(n1a) == (1,6,4) && size(n2a) == (1,3,6) && size(n3a) == (2,1,6) @test size(n1) == (6,1,4) && size(n2) == (6,3,1) && size(n3) == (2,6,1) # mutating functions o = fill(1, 3, 4) - m = mapslices(x->fill!(x, 0), o, 2) + m = mapslices(x->fill!(x, 0), o, dims=2) @test m == zeros(3, 4) @test o == fill(1, 3, 4) # issue #18524 - m = mapslices(x->tuple(x), [1 2; 3 4], 1) + m = mapslices(x->tuple(x), [1 2; 3 4], dims=1) @test m[1,1] == ([1,3],) @test m[1,2] == ([2,4],) # issue #21123 - @test mapslices(nnz, sparse(1.0I, 3, 3), 1) == [1 1 1] + @test mapslices(nnz, sparse(1.0I, 3, 3), dims=1) == [1 1 1] end @testset "single multidimensional index" begin @@ -1183,7 +1183,7 @@ end # mutating functions o = fill(1, 3, 4) - m = mapslices(x->fill!(x, 0), o, 2) + m = mapslices(x->fill!(x, 0), o, dims=2) @test m == zeros(3, 4) @test o == fill(1, 3, 4) @@ -1987,8 +1987,8 @@ copyto!(S, A) @test cumsum(A, dims=1) == cumsum(B, dims=1) == cumsum(S, dims=1) @test cumsum(A, dims=2) == cumsum(B, dims=2) == cumsum(S, dims=2) -@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 mapslices(sort, A, dims=1) == mapslices(sort, B, dims=1) == mapslices(sort, S, dims=1) +@test mapslices(sort, A, dims=2) == mapslices(sort, B, dims=2) == mapslices(sort, S, dims=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) diff --git a/test/offsetarray.jl b/test/offsetarray.jl index 2d5c73744e724..5d73d4be8a4dc 100644 --- a/test/offsetarray.jl +++ b/test/offsetarray.jl @@ -412,8 +412,8 @@ v = OffsetArray(rand(8), (-2,)) @test sort(A, dims=1) == OffsetArray(sort(parent(A), dims=1), A.offsets) @test sort(A, dims=2) == OffsetArray(sort(parent(A), dims=2), A.offsets) -@test mapslices(sort, A, 1) == OffsetArray(mapslices(sort, parent(A), 1), A.offsets) -@test mapslices(sort, A, 2) == OffsetArray(mapslices(sort, parent(A), 2), A.offsets) +@test mapslices(sort, A, dims=1) == OffsetArray(mapslices(sort, parent(A), dims=1), A.offsets) +@test mapslices(sort, A, dims=2) == OffsetArray(mapslices(sort, parent(A), dims=2), A.offsets) @test rotl90(A) == OffsetArray(rotl90(parent(A)), A.offsets[[2,1]]) @test rotr90(A) == OffsetArray(rotr90(parent(A)), A.offsets[[2,1]]) diff --git a/test/reducedim.jl b/test/reducedim.jl index 2c9f622cb4905..f8f78c50cbd1f 100644 --- a/test/reducedim.jl +++ b/test/reducedim.jl @@ -6,7 +6,7 @@ using Random function safe_mapslices(op, A, region) newregion = intersect(region, 1:ndims(A)) - return isempty(newregion) ? A : mapslices(op, A, newregion) + return isempty(newregion) ? A : mapslices(op, A, dims = newregion) end safe_sum(A::Array{T}, region) where {T} = safe_mapslices(sum, A, region) safe_prod(A::Array{T}, region) where {T} = safe_mapslices(prod, A, region)