Skip to content
Merged
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
9 changes: 7 additions & 2 deletions src/reductions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,15 @@ _mapreducezeros(f::Base.ExtremaMap, op::typeof(Base._extrema_rf), ::Type{T}, nze
nzeros == 0 ? v0 : op(v0, f(zero(T)))

# Specialized mapreduce for any and all
# Each value of the predicate is checked to be Boolean, as the dense reductions check it:
# `|` and `&` would absorb another value next to a `missing`, or combine two of them.
_boolean(v::Union{Bool,Missing}) = v
_boolean(v) = v::Bool
_booleanpredicate(f::F) where {F} = _boolean ∘ f
Base._any(f, A::SparseMatrixCSCOrColumnSubset, ::Colon) =
iszero(widelength(A)) ? false : Base._mapreduce(f, |, IndexCartesian(), A)
iszero(widelength(A)) ? false : Base._mapreduce(_booleanpredicate(f), |, IndexCartesian(), A)
Base._all(f, A::SparseMatrixCSCOrColumnSubset, ::Colon) =
iszero(widelength(A)) ? true : Base._mapreduce(f, &, IndexCartesian(), A)
iszero(widelength(A)) ? true : Base._mapreduce(_booleanpredicate(f), &, IndexCartesian(), A)

function Base._mapreduce(f::F, op::Union{typeof(Base.mul_prod),typeof(*)}, ::Base.IndexCartesian, A::SparseMatrixCSCOrColumnSubset{T}) where {F,T}
nnzA = nnz(A)
Expand Down
4 changes: 2 additions & 2 deletions src/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1525,9 +1525,9 @@ function Base._mapreduce(f::F, op::G, ::IndexCartesian, A::SparseVectorOrView) w
end

Base._any(f, A::SparseVectorOrView, ::Colon) =
iszero(length(A)) ? false : Base._mapreduce(f, |, IndexCartesian(), A)
iszero(length(A)) ? false : Base._mapreduce(_booleanpredicate(f), |, IndexCartesian(), A)
Base._all(f, A::SparseVectorOrView, ::Colon) =
iszero(length(A)) ? true : Base._mapreduce(f, &, IndexCartesian(), A)
iszero(length(A)) ? true : Base._mapreduce(_booleanpredicate(f), &, IndexCartesian(), A)

function Base.mapreducedim!(f::F, op::G, R::AbstractVector, A::SparseVectorOrView) where {F,G}
isempty(A) && return R
Expand Down
11 changes: 11 additions & 0 deletions test/reductions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,17 @@ along(f::F, X, dims) where {F} = f(X; dims)
@test count(v) == length(v)
@test all(!iszero, spzeros(0, 0))
@test !any(iszero, spzeros(0, 0))
@static if COMPREHENSIVE
# a predicate that is not Boolean is rejected, as for dense
for S in (sparse([1 0; 0 0]), sparsevec([1, 0]))
@test_throws TypeError any(S)
@test_throws TypeError all(S)
end
# also next to a `missing`, which `|` and `&` would let absorb the other value
S = sparse(reshape(Union{Missing,Int}[missing, 1], 2, 1))
@test_throws TypeError any(S)
@test_throws TypeError all(S)
end
end

@testset "seed without copying the first slice" begin
Expand Down
Loading