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

Make sure reductions benefit from sparsity #244

Merged
merged 13 commits into from
Sep 16, 2022
1 change: 1 addition & 0 deletions src/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ nnz1(S) = sum(length.(nzrange.(Ref(S), axes(S, 2))))
function count(pred, S::AbstractSparseMatrixCSC)
count(pred, nzvalview(S)) + pred(zero(eltype(S)))*(prod(size(S)) - nnz(S))
end
count(S::AbstractSparseMatrixCSC{Bool}) = count(identity, S)
dkarrasch marked this conversation as resolved.
Show resolved Hide resolved

"""
nonzeros(A)
Expand Down
1 change: 1 addition & 0 deletions src/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ const SVorFSV{Tv,Ti} = Union{SparseVector{Tv,Ti},FixedSparseVector{Tv,Ti}}
length(x::SVorFSV) = getfield(x, :n)
size(x::SVorFSV) = (getfield(x, :n),)
count(f, x::AbstractCompressedVector) = count(f, nonzeros(x)) + f(zero(eltype(x)))*(length(x) - nnz(x))
count(x::AbstractCompressedVector{Bool}) = count(identity, x)

# implement the nnz - nzrange - nonzeros - rowvals interface for sparse vectors

Expand Down
6 changes: 3 additions & 3 deletions test/sparsematrix_constructors_indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -445,12 +445,12 @@ end

@testset "setindex" begin
a = spzeros(Int, 10, 10)
@test count(!iszero, a) == 0
@test count(!iszero, a) == count((!iszero).(a)) == 0
a[1,:] .= 1
@test count(!iszero, a) == 10
@test count(!iszero, a) == count((!iszero).(a)) == 10
@test a[1,:] == sparse(fill(1,10))
a[:,2] .= 2
@test count(!iszero, a) == 19
@test count(!iszero, a) == count((!iszero).(a)) == 19
@test a[:,2] == sparse(fill(2,10))
b = copy(a)

Expand Down