Skip to content

Commit 440a1a5

Browse files
JeffBezansonmbauman
authored andcommitted
make dims argument to mapslices a keyword arg (#27828)
fixes #27774
1 parent 7ebcbc0 commit 440a1a5

File tree

7 files changed

+34
-31
lines changed

7 files changed

+34
-31
lines changed

base/abstractarray.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,7 +1840,7 @@ foreach(f, itrs...) = (for z in zip(itrs...); f(z...); end; nothing)
18401840
## dims specifies which dimensions will be transformed. for example
18411841
## dims==1:2 will call f on all slices A[:,:,...]
18421842
"""
1843-
mapslices(f, A, dims)
1843+
mapslices(f, A; dims)
18441844
18451845
Transform the given dimensions of array `A` using function `f`. `f` is called on each slice
18461846
of `A` of the form `A[...,:,...,:,...]`. `dims` is an integer vector specifying where the
@@ -1868,7 +1868,7 @@ julia> a = reshape(Vector(1:16),(2,2,2,2))
18681868
13 15
18691869
14 16
18701870
1871-
julia> mapslices(sum, a, [1,2])
1871+
julia> mapslices(sum, a, dims = [1,2])
18721872
1×1×2×2 Array{Int64,4}:
18731873
[:, :, 1, 1] =
18741874
10
@@ -1883,11 +1883,13 @@ julia> mapslices(sum, a, [1,2])
18831883
58
18841884
```
18851885
"""
1886-
mapslices(f, A::AbstractArray, dims) = mapslices(f, A, [dims...])
1887-
function mapslices(f, A::AbstractArray, dims::AbstractVector)
1886+
function mapslices(f, A::AbstractArray; dims)
18881887
if isempty(dims)
18891888
return map(f,A)
18901889
end
1890+
if !isa(dims, AbstractVector)
1891+
dims = [dims...]
1892+
end
18911893

18921894
dimsA = [axes(A)...]
18931895
ndimsA = ndims(A)

base/deprecated.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,6 +1373,7 @@ export readandwrite
13731373
@deprecate mapreducedim(f, op, A::AbstractArray, dims, v0) mapreduce(f, op, v0, A, dims=dims)
13741374
@deprecate reducedim(op, A::AbstractArray, dims) reduce(op, A, dims=dims)
13751375
@deprecate reducedim(op, A::AbstractArray, dims, v0) reduce(op, v0, A, dims=dims)
1376+
@deprecate mapslices(op, A::AbstractArray, dims) mapslices(op, A, dims=dims)
13761377

13771378
@deprecate sort(A::AbstractArray, dim::Integer; kwargs...) sort(A; kwargs..., dims=dim)
13781379

base/reducedim.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ function reducedim_initarray0(A::AbstractArray{T}, region, f, ops) where T
9595
end
9696
end
9797

98-
reducedim_initarray0_empty(A::AbstractArray, region, f, ops) = mapslices(x->ops(f.(x)), A, region)
99-
reducedim_initarray0_empty(A::AbstractArray, region,::typeof(identity), ops) = mapslices(ops, A, region)
98+
reducedim_initarray0_empty(A::AbstractArray, region, f, ops) = mapslices(x->ops(f.(x)), A, dims = region)
99+
reducedim_initarray0_empty(A::AbstractArray, region,::typeof(identity), ops) = mapslices(ops, A, dims = region)
100100

101101
# TODO: better way to handle reducedim initialization
102102
#

base/statistics.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ equivalent to calculating mean of two median elements.
170170
"""
171171
median(v::AbstractArray; dims=:) = _median(v, dims)
172172

173-
_median(v::AbstractArray, dims) = mapslices(median!, v, dims)
173+
_median(v::AbstractArray, dims) = mapslices(median!, v, dims = dims)
174174

175175
_median(v::AbstractArray{T}, ::Colon) where {T} = median!(copyto!(Array{T,1}(undef, _length(v)), v))
176176

test/arrayops.jl

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,57 +1006,57 @@ end
10061006
@testset "mapslices" begin
10071007
local a, b, c, m, h, s
10081008
a = rand(5,5)
1009-
s = mapslices(sort, a, [1])
1010-
S = mapslices(sort, a, [2])
1009+
s = mapslices(sort, a, dims=[1])
1010+
S = mapslices(sort, a, dims=[2])
10111011
for i = 1:5
10121012
@test s[:,i] == sort(a[:,i])
10131013
@test vec(S[i,:]) == sort(vec(a[i,:]))
10141014
end
10151015

10161016
# issue #3613
1017-
b = mapslices(sum, fill(1.,2,3,4), [1,2])
1017+
b = mapslices(sum, fill(1.,2,3,4), dims=[1,2])
10181018
@test size(b) === (1,1,4)
10191019
@test all(b.==6)
10201020

10211021
# issue #5141
10221022
## Update Removed the version that removes the dimensions when dims==1:ndims(A)
1023-
c1 = mapslices(x-> maximum(-x), a, [])
1023+
c1 = mapslices(x-> maximum(-x), a, dims=[])
10241024
@test c1 == -a
10251025

10261026
# other types than Number
1027-
@test mapslices(prod,["1" "2"; "3" "4"],1) == ["13" "24"]
1028-
@test mapslices(prod,["1"],1) == ["1"]
1027+
@test mapslices(prod,["1" "2"; "3" "4"],dims=1) == ["13" "24"]
1028+
@test mapslices(prod,["1"],dims=1) == ["1"]
10291029

10301030
# issue #5177
10311031

10321032
c = fill(1,2,3,4)
1033-
m1 = mapslices(x-> fill(1,2,3), c, [1,2])
1034-
m2 = mapslices(x-> fill(1,2,4), c, [1,3])
1035-
m3 = mapslices(x-> fill(1,3,4), c, [2,3])
1033+
m1 = mapslices(x-> fill(1,2,3), c, dims=[1,2])
1034+
m2 = mapslices(x-> fill(1,2,4), c, dims=[1,3])
1035+
m3 = mapslices(x-> fill(1,3,4), c, dims=[2,3])
10361036
@test size(m1) == size(m2) == size(m3) == size(c)
10371037

1038-
n1 = mapslices(x-> fill(1,6), c, [1,2])
1039-
n2 = mapslices(x-> fill(1,6), c, [1,3])
1040-
n3 = mapslices(x-> fill(1,6), c, [2,3])
1041-
n1a = mapslices(x-> fill(1,1,6), c, [1,2])
1042-
n2a = mapslices(x-> fill(1,1,6), c, [1,3])
1043-
n3a = mapslices(x-> fill(1,1,6), c, [2,3])
1038+
n1 = mapslices(x-> fill(1,6), c, dims=[1,2])
1039+
n2 = mapslices(x-> fill(1,6), c, dims=[1,3])
1040+
n3 = mapslices(x-> fill(1,6), c, dims=[2,3])
1041+
n1a = mapslices(x-> fill(1,1,6), c, dims=[1,2])
1042+
n2a = mapslices(x-> fill(1,1,6), c, dims=[1,3])
1043+
n3a = mapslices(x-> fill(1,1,6), c, dims=[2,3])
10441044
@test size(n1a) == (1,6,4) && size(n2a) == (1,3,6) && size(n3a) == (2,1,6)
10451045
@test size(n1) == (6,1,4) && size(n2) == (6,3,1) && size(n3) == (2,6,1)
10461046

10471047
# mutating functions
10481048
o = fill(1, 3, 4)
1049-
m = mapslices(x->fill!(x, 0), o, 2)
1049+
m = mapslices(x->fill!(x, 0), o, dims=2)
10501050
@test m == zeros(3, 4)
10511051
@test o == fill(1, 3, 4)
10521052

10531053
# issue #18524
1054-
m = mapslices(x->tuple(x), [1 2; 3 4], 1)
1054+
m = mapslices(x->tuple(x), [1 2; 3 4], dims=1)
10551055
@test m[1,1] == ([1,3],)
10561056
@test m[1,2] == ([2,4],)
10571057

10581058
# issue #21123
1059-
@test mapslices(nnz, sparse(1.0I, 3, 3), 1) == [1 1 1]
1059+
@test mapslices(nnz, sparse(1.0I, 3, 3), dims=1) == [1 1 1]
10601060
end
10611061

10621062
@testset "single multidimensional index" begin
@@ -1183,7 +1183,7 @@ end
11831183

11841184
# mutating functions
11851185
o = fill(1, 3, 4)
1186-
m = mapslices(x->fill!(x, 0), o, 2)
1186+
m = mapslices(x->fill!(x, 0), o, dims=2)
11871187
@test m == zeros(3, 4)
11881188
@test o == fill(1, 3, 4)
11891189

@@ -1987,8 +1987,8 @@ copyto!(S, A)
19871987
@test cumsum(A, dims=1) == cumsum(B, dims=1) == cumsum(S, dims=1)
19881988
@test cumsum(A, dims=2) == cumsum(B, dims=2) == cumsum(S, dims=2)
19891989

1990-
@test mapslices(sort, A, 1) == mapslices(sort, B, 1) == mapslices(sort, S, 1)
1991-
@test mapslices(sort, A, 2) == mapslices(sort, B, 2) == mapslices(sort, S, 2)
1990+
@test mapslices(sort, A, dims=1) == mapslices(sort, B, dims=1) == mapslices(sort, S, dims=1)
1991+
@test mapslices(sort, A, dims=2) == mapslices(sort, B, dims=2) == mapslices(sort, S, dims=2)
19921992

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

test/offsetarray.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,8 @@ v = OffsetArray(rand(8), (-2,))
412412
@test sort(A, dims=1) == OffsetArray(sort(parent(A), dims=1), A.offsets)
413413
@test sort(A, dims=2) == OffsetArray(sort(parent(A), dims=2), A.offsets)
414414

415-
@test mapslices(sort, A, 1) == OffsetArray(mapslices(sort, parent(A), 1), A.offsets)
416-
@test mapslices(sort, A, 2) == OffsetArray(mapslices(sort, parent(A), 2), A.offsets)
415+
@test mapslices(sort, A, dims=1) == OffsetArray(mapslices(sort, parent(A), dims=1), A.offsets)
416+
@test mapslices(sort, A, dims=2) == OffsetArray(mapslices(sort, parent(A), dims=2), A.offsets)
417417

418418
@test rotl90(A) == OffsetArray(rotl90(parent(A)), A.offsets[[2,1]])
419419
@test rotr90(A) == OffsetArray(rotr90(parent(A)), A.offsets[[2,1]])

test/reducedim.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using Random
66

77
function safe_mapslices(op, A, region)
88
newregion = intersect(region, 1:ndims(A))
9-
return isempty(newregion) ? A : mapslices(op, A, newregion)
9+
return isempty(newregion) ? A : mapslices(op, A, dims = newregion)
1010
end
1111
safe_sum(A::Array{T}, region) where {T} = safe_mapslices(sum, A, region)
1212
safe_prod(A::Array{T}, region) where {T} = safe_mapslices(prod, A, region)

0 commit comments

Comments
 (0)