diff --git a/stdlib/Statistics/src/Statistics.jl b/stdlib/Statistics/src/Statistics.jl index 3f57ddb773c0c..8f38882ec3b56 100644 --- a/stdlib/Statistics/src/Statistics.jl +++ b/stdlib/Statistics/src/Statistics.jl @@ -98,7 +98,7 @@ julia> mean(√, [1 2 3; 4 5 6], dims=2) mean(f, A::AbstractArray; dims = :) = _mean(f, A, dims) _mean(f, A::AbstractArray, ::Colon) = sum(f, A) / length(A) -_mean(f, A::AbstractArray, dims) = sum(f, A, dims = dims) / prod(size(A, i) for i in dims) +_mean(f, A::AbstractArray, dims) = sum(f, A, dims = dims) / mapreduce(i -> size(A, i), *, unique(dims); init=1) """ mean!(r, v) diff --git a/stdlib/Statistics/test/runtests.jl b/stdlib/Statistics/test/runtests.jl index 4d59b5e44510b..34a448e9462a5 100644 --- a/stdlib/Statistics/test/runtests.jl +++ b/stdlib/Statistics/test/runtests.jl @@ -73,9 +73,11 @@ end @test mean([1,2,3]) == 2. @test mean([0 1 2; 4 5 6], dims=1) == [2. 3. 4.] @test mean([1 2 3; 4 5 6], dims=1) == [2.5 3.5 4.5] - @test mean(-, [1 2 3 ; 4 5 6], dims = 1) == [-2.5 -3.5 -4.5] - @test mean(-, [1 2 3 ; 4 5 6], dims = 2) == transpose([-2.0 -5.0]) - @test mean(-, [1 2 3; 4 5 6], dims = (1, 2)) == -3.5 .* ones(1, 1) + @test mean(-, [1 2 3 ; 4 5 6], dims=1) == [-2.5 -3.5 -4.5] + @test mean(-, [1 2 3 ; 4 5 6], dims=2) == transpose([-2.0 -5.0]) + @test mean(-, [1 2 3 ; 4 5 6], dims=(1, 2)) == -3.5 .* ones(1, 1) + @test mean(-, [1 2 3 ; 4 5 6], dims=(1, 1)) == [-2.5 -3.5 -4.5] + @test mean(-, [1 2 3 ; 4 5 6], dims=()) == Float64[-1 -2 -3 ; -4 -5 -6] @test mean(i->i+1, 0:2) === 2. @test mean(isodd, [3]) === 1. @test mean(x->3x, (1,1)) === 3.