From c474d3f0dfd13b0c22ec2d3dde9856844436786a Mon Sep 17 00:00:00 2001 From: Michael Abbott <32575566+mcabbott@users.noreply.github.com> Date: Thu, 26 Sep 2019 18:15:32 +0200 Subject: [PATCH] https://github.com/JuliaLang/julia/pull/33130 --- README.md | 2 ++ src/Compat.jl | 5 +++++ test/runtests.jl | 35 ++++++++++++++++++++++++++++++++++- 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2751b9d14..83f50aa56 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,8 @@ Currently, the `@compat` macro supports the following syntaxes: ## New functions, macros, and methods +* A new `dropdims(f, args...; dims, kwargs...)` method computes the reduction `f` over the region described by `dims` and then drops those dimensions from the result ([#33130]). + * `eachrow`, `eachcol`, and `eachslice` to iterate over first, second, or given dimension of an array ([#29749]). diff --git a/src/Compat.jl b/src/Compat.jl index 1be0250ec..df9474bbe 100644 --- a/src/Compat.jl +++ b/src/Compat.jl @@ -1866,6 +1866,11 @@ if v"0.7.0" <= VERSION < v"1.1.0-DEV.594" Base.merge(a::NamedTuple) = a end +# https://github.com/JuliaLang/julia/pull/33130 +if VERSION < v"1.4.0-DEV.79" # not in fact merged yet! + Base.dropdims(f, args...; dims, kwargs...) = _dropdims(f(args...; kwargs..., dims=dims), dims) +end + include("deprecated.jl") end # module Compat diff --git a/test/runtests.jl b/test/runtests.jl index b6a873768..a4d589de3 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -38,7 +38,7 @@ if VERSION >= v"0.7" @test collect(eachrow(M)) == collect(eachslice(M, dims = 1)) == [[1, 2, 3], [4, 5, 6], [7, 8, 9]] @test collect(eachcol(M)) == collect(eachslice(M, dims = 2)) == [[1, 4, 7], [2, 5, 8], [3, 6, 9]] @test_throws DimensionMismatch eachslice(M, dims = 4) - + # Higher-dimensional case M = reshape([(1:16)...], 2, 2, 2, 2) @test_throws MethodError collect(eachrow(M)) @@ -1499,4 +1499,37 @@ end @test merge((a=1,), (b=2,), (c=3,)) == (a=1,b=2,c=3) end +# https://github.com/JuliaLang/julia/pull/33130 +@testset "dropdims with reductions" begin # issue #16606 + @test (@inferred(dropdims(sum, a, dims=1)) == + @inferred(dropdims(sum, a, dims=(1,))) == + reshape(sum(a, dims=1), (1, 8, 8, 1))) + @test (@inferred(dropdims(sum, a, dims=3)) == + @inferred(dropdims(sum, a, dims=(3,))) == + reshape(sum(a, dims=3), (1, 1, 8, 1))) + @test (@inferred(dropdims(sum, a, dims=4)) == + @inferred(dropdims(sum, a, dims=(4,))) == + reshape(sum(a, dims=4), (1, 1, 8, 1))) + @test (@inferred(dropdims(sum, a, dims=(1, 5))) == + dropdims(sum, a, dims=(5, 1)) == + reshape(sum(a, dims=(5, 1)), (1, 8, 8))) + @test (@inferred(dropdims(sum, a, dims=(1, 2, 5))) == + dropdims(sum, a, dims=(5, 2, 1)) == + reshape(sum(a, dims=(5, 2, 1)), (8, 8))) + @test (@inferred(dropdims(sum, abs2, a, dims=1)) == + @inferred(dropdims(sum, abs2, a, dims=(1,))) == + reshape(sum(abs2, a, dims=1), (1, 8, 8, 1))) + _sumplus(x; dims, plus) = sum(x; dims=dims) .+ plus # reduction with keywords + @test (@inferred(dropdims(_sumplus, a, dims=4, plus=1)) == + @inferred(dropdims(_sumplus, a, dims=(4,), plus=1)) == + reshape(sum(a, dims=4) .+ 1, (1, 1, 8, 1))) + @test_throws UndefKeywordError dropdims(sum, a) + @test_throws UndefKeywordError dropdims(sum, a, 1) + @test_throws ArgumentError dropdims(sum, a, dims=0) + @test_throws ArgumentError dropdims(sum, a, dims=(1, 1)) + @test_throws ArgumentError dropdims(sum, a, dims=(1, 2, 1)) + @test_throws ArgumentError dropdims(sum, a, dims=(1, 1, 2)) + @test_throws ArgumentError dropdims(sum, a, dims=6) +end + nothing