diff --git a/base/reducedim.jl b/base/reducedim.jl index 27c5e05e40c46..27c1162e7fedb 100644 --- a/base/reducedim.jl +++ b/base/reducedim.jl @@ -843,6 +843,12 @@ function _sum!(f, R::AbstractArray, A::AbstractArray{T,N}, w::AbstractArray; check_reducedims(R,A) reddims = size(R) .!= size(A) dim = something(findfirst(reddims), ndims(R)+1) + if dim > N + dim1 = findfirst(==(1), size(A)) + if dim1 !== nothing + dim = dim1 + end + end if findnext(reddims, dim+1) !== nothing throw(ArgumentError("reducing over more than one dimension is not supported with weights")) end diff --git a/test/reducedim.jl b/test/reducedim.jl index a90c2c1f96afb..c8c030d3704ce 100644 --- a/test/reducedim.jl +++ b/test/reducedim.jl @@ -482,6 +482,9 @@ end end end + # Corner case with a single row + @test sum([1 2], weights=[2], dims=1) == [2 4] + @test_throws ArgumentError sum(exp, [1 2], weights=[1, 10], dims=1) @test_throws ArgumentError sum([1 2], weights=[1 10], dims=1) end \ No newline at end of file