Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reduce with dims argument over empty arrays #30928

Closed
simonbyrne opened this issue Feb 1, 2019 · 2 comments
Closed

reduce with dims argument over empty arrays #30928

simonbyrne opened this issue Feb 1, 2019 · 2 comments

Comments

@simonbyrne
Copy link
Contributor

simonbyrne commented Feb 1, 2019

Currently this is a bit inconsistent:

julia> a = zeros(0,0)
0×0 Array{Float64,2}

julia> sum(a,dims=1)
1×0 Array{Float64,2}

julia> minimum(a,dims=1)
ERROR: ArgumentError: reducing over an empty collection is not allowed
Stacktrace:
 [1] _empty_reduce_error() at ./reduce.jl:216
 [2] reducedim_init(::Function, ::typeof(min), ::Array{Float64,2}, ::Int64) at ./reducedim.jl:135
 [3] _mapreduce_dim at ./reducedim.jl:313 [inlined]
 [4] #mapreduce#543 at ./reducedim.jl:304 [inlined]
 [5] #mapreduce at ./none:0 [inlined]
 [6] _minimum at ./reducedim.jl:675 [inlined]
 [7] _minimum at ./reducedim.jl:674 [inlined]
 [8] #minimum#551 at ./reducedim.jl:648 [inlined]
 [9] (::getfield(Base, Symbol("#kw##minimum")))(::NamedTuple{(:dims,),Tuple{Int64}}, ::typeof(minimum), ::Array{Float64,2}) at ./none:0
 [10] top-level scope at REPL[10]:1

Basically it comes down to whether or not Base.reduce_empty(op, eltype(a)) is defined. Is it possible to support this more generally?

@simonbyrne
Copy link
Contributor Author

Maybe this isn't possible, since it we would need a value if the non-reduced dimension is non-zero:

julia> a = zeros(0,3)
0×3 Array{Float64,2}

julia> sum(a,dims=1)
1×3 Array{Float64,2}:
 0.0  0.0  0.0

@laborg
Copy link
Contributor

laborg commented Feb 4, 2022

The init keyword has been added in #36188 for this reason.

@laborg laborg closed this as completed Feb 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants