Skip to content

unify reduce/reducedim empty case behaviors #615

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions src/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2650,12 +2650,7 @@ function _findr(op, A::AbstractSparseMatrixCSC{Tv}, region) where {Tv}
N = nnz(A)
L = widelength(A)
if L == 0
if prod(map(length, Base.reduced_indices(A, region))) != 0
throw(ArgumentError("array slices must be non-empty"))
else
ri = Base.reduced_indices0(A, region)
return (zeros(Tv, ri), zeros(Ti, ri))
end
throw(ArgumentError("reducing over an empty collection is not allowed"))
end

colptr = getcolptr(A); rowval = rowvals(A); nzval = nonzeros(A); m = size(A, 1); n = size(A, 2)
Expand Down
1 change: 1 addition & 0 deletions src/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1682,6 +1682,7 @@ Base._all(f, A::SparseVectorUnion, ::Colon) =
iszero(length(A)) ? true : Base._mapreduce(f, &, IndexCartesian(), A)

function Base.mapreducedim!(f, op, R::AbstractVector, A::SparseVectorUnion)
isempty(A) && return R
# dim1 reduction could be safely replaced with a mapreduce
if length(R) == 1
I = firstindex(R)
Expand Down
49 changes: 47 additions & 2 deletions test/sparsematrix_ops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ sC = similar(sA)
dA = Array(sA)

@testset "reductions" begin
se33 = SparseMatrixCSC{Float64}(I, 3, 3)
do33 = fill(1.,3)
sA = sprandn(3, 7, 0.5)
sC = similar(sA)
dA = Array(sA)

pA = sparse(rand(3, 7))
p28227 = sparse(Real[0 0.5])

Expand Down Expand Up @@ -256,9 +262,48 @@ dA = Array(sA)
end
for f in (minimum, maximum, findmin, findmax)
@test_throws errchecker f(spzeros(0, 1), dims=1)
@test isequal(f(spzeros(0, 1), dims=2), f(Matrix{Int}(I, 0, 1), dims=2))
@test_throws errchecker f(spzeros(0, 1), dims=2)
@test_throws errchecker f(spzeros(0, 1), dims=(1, 2))
@test isequal(f(spzeros(0, 1), dims=3), f(Matrix{Int}(I, 0, 1), dims=3))
@test_throws errchecker f(spzeros(0, 1), dims=3)
end

some_exception(op) = try return (Some(op()), nothing); catch ex; return (nothing, ex); end
reduced_shape(sz, dims) = ntuple(d -> d in dims ? 1 : sz[d], length(sz))

@testset "$r(spzeros($T, $sz); dims=$dims)" for
r in (minimum, maximum, findmin, findmax, extrema, sum, prod, mapreduce, all, any, count),
T in (Int, Union{Missing, Int}, Number, Union{Missing, Number}, Bool, Union{Missing, Bool}),
sz in ((0,), (0,1), (1,0), (0,0),),
dims in (1, 2, (1,2))

A = spzeros(T, sz...)
rsz = reduced_shape(sz, dims)

v, ex = some_exception() do; r(A); end
if isnothing(v)
@test_throws typeof(ex) r(A; dims)
@test_throws typeof(ex) r(zeros(T, sz...))
@test_throws typeof(ex) r(zeros(T, sz...); dims)
else
actual = fill(something(v), rsz)
@test something(v) === r(zeros(T, sz...))
@test isequal(r(A; dims), actual)
@test eltype(r(A; dims)) === eltype(actual)
end

for f in (identity, abs, abs2)
v, ex = some_exception() do; r(f, A); end
if isnothing(v)
@test_throws typeof(ex) r(f, A; dims)
@test_throws typeof(ex) r(f, zeros(T, sz...))
@test_throws typeof(ex) r(f, zeros(T, sz...); dims)
else
actual = fill(something(v), rsz)
@test something(v) === r(f, zeros(T, sz...))
@test isequal(r(f, A; dims), actual)
@test eltype(r(f, A; dims)) === eltype(actual)
end
end
end
end
end
Expand Down
Loading