Skip to content

Commit

Permalink
make count(f,itr) count the number of nonzero values (fixes #20404), …
Browse files Browse the repository at this point in the history
…elim countnz in favor of count(itr) (fixes #20403)
  • Loading branch information
stevengj committed Feb 2, 2017
1 parent 3c20d76 commit b51ecbd
Show file tree
Hide file tree
Showing 21 changed files with 87 additions and 90 deletions.
2 changes: 1 addition & 1 deletion base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,7 @@ function cat_t(dims, T::Type, X...)
catdims = dims2cat(dims)
shape = cat_shape(catdims, (), map(cat_size, X)...)
A = cat_similar(X[1], T, shape)
if T <: Number && countnz(catdims) > 1
if T <: Number && count(catdims) > 1
fill!(A, zero(T))
end
return _cat(A, shape, catdims, X...)
Expand Down
6 changes: 3 additions & 3 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1393,7 +1393,7 @@ julia> find(A)
```
"""
function find(A)
nnzA = countnz(A)
nnzA = count(A)
I = Vector{Int}(nnzA)
count = 1
inds = _index_remapper(A)
Expand Down Expand Up @@ -1438,7 +1438,7 @@ julia> findn(A)
```
"""
function findn(A::AbstractMatrix)
nnzA = countnz(A)
nnzA = count(A)
I = similar(A, Int, nnzA)
J = similar(A, Int, nnzA)
count = 1
Expand Down Expand Up @@ -1470,7 +1470,7 @@ julia> findnz(A)
```
"""
function findnz{T}(A::AbstractMatrix{T})
nnzA = countnz(A)
nnzA = count(A)
I = zeros(Int, nnzA)
J = zeros(Int, nnzA)
NZs = Array{T,1}(nnzA)
Expand Down
10 changes: 5 additions & 5 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1565,9 +1565,9 @@ julia> ror(A,5)
"""
ror(B::BitVector, i::Integer) = ror!(similar(B), B, i)

## countnz & find ##
## count & find ##

function countnz(B::BitArray)
function count(B::BitArray)
n = 0
Bc = B.chunks
@inbounds for i = 1:length(Bc)
Expand Down Expand Up @@ -1728,7 +1728,7 @@ end

function find(B::BitArray)
l = length(B)
nnzB = countnz(B)
nnzB = count(B)
I = Array{Int}(nnzB)
nnzB == 0 && return I
Bc = B.chunks
Expand Down Expand Up @@ -1762,7 +1762,7 @@ end
findn(B::BitVector) = find(B)

function findn(B::BitMatrix)
nnzB = countnz(B)
nnzB = count(B)
I = Array{Int}(nnzB)
J = Array{Int}(nnzB)
count = 1
Expand All @@ -1784,7 +1784,7 @@ end
## Reductions ##

sum(A::BitArray, region) = reducedim(+, A, region)
sum(B::BitArray) = countnz(B)
sum(B::BitArray) = count(B)

function all(B::BitArray)
isempty(B) && return true
Expand Down
1 change: 1 addition & 0 deletions base/bool.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ signbit(x::Bool) = false
sign(x::Bool) = x
abs(x::Bool) = x
abs2(x::Bool) = x
iszero(x::Bool) = !x

<(x::Bool, y::Bool) = y&!x
<=(x::Bool, y::Bool) = y|!x
Expand Down
2 changes: 2 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1224,6 +1224,8 @@ function (::Type{Matrix})()
return Matrix(0, 0)
end

@deprecate countnz(itr) count(itr)

for name in ("alnum", "alpha", "cntrl", "digit", "number", "graph",
"lower", "print", "punct", "space", "upper", "xdigit")
f = Symbol("is",name)
Expand Down
1 change: 0 additions & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,6 @@ export
minmax,
ndims,
nonzeros,
countnz,
ones,
parent,
parentindexes,
Expand Down
2 changes: 1 addition & 1 deletion base/linalg/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ end

## Structure query functions

issymmetric(A::BitMatrix) = size(A, 1)==size(A, 2) && countnz(A - A.')==0
issymmetric(A::BitMatrix) = size(A, 1)==size(A, 2) && count(A - A.')==0
ishermitian(A::BitMatrix) = issymmetric(A)

function nonzero_chunks(chunks::Vector{UInt64}, pos0::Int, pos1::Int)
Expand Down
2 changes: 1 addition & 1 deletion base/linalg/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ function vecnorm(itr, p::Real=2)
return vecnormInf(itr)
elseif p == 0
return convert(typeof(float(real(zero(eltype(itr))))),
countnz(itr))
count(itr))
elseif p == -Inf
return vecnormMinusInf(itr)
else
Expand Down
6 changes: 3 additions & 3 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ wrapped with `LogicalIndex` upon calling `to_indices`.
immutable LogicalIndex{T, A<:AbstractArray{Bool}} <: AbstractVector{T}
mask::A
sum::Int
LogicalIndex(mask::A) = new(mask, countnz(mask))
LogicalIndex(mask::A) = new(mask, count(mask))
end
LogicalIndex(mask::AbstractVector{Bool}) = LogicalIndex{Int, typeof(mask)}(mask)
LogicalIndex{N}(mask::AbstractArray{Bool, N}) = LogicalIndex{CartesianIndex{N}, typeof(mask)}(mask)
Expand Down Expand Up @@ -467,7 +467,7 @@ end

@generated function findn{T,N}(A::AbstractArray{T,N})
quote
nnzA = countnz(A)
nnzA = count(A)
@nexprs $N d->(I_d = Array{Int}(nnzA))
k = 1
@nloops $N i A begin
Expand Down Expand Up @@ -1066,7 +1066,7 @@ end

@generated function findn{N}(B::BitArray{N})
quote
nnzB = countnz(B)
nnzB = count(B)
I = ntuple(x->Array{Int}(nnzB), $N)
if nnzB > 0
count = 1
Expand Down
41 changes: 18 additions & 23 deletions base/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ julia> sum(1:20)
```
"""
sum(a) = mapreduce(identity, +, a)
sum(a::AbstractArray{Bool}) = countnz(a)
sum(a::AbstractArray{Bool}) = count(a)


# Kahan (compensated) summation: O(1) error growth, at the expense
Expand Down Expand Up @@ -637,42 +637,37 @@ function contains(eq::Function, itr, x)
return false
end


## countnz & count

"""
count(p, itr) -> Integer
count(itr)
Count the number of elements in `itr` for which predicate `p` returns `true`.
Count the number of elements in `itr` for which the function `p` returns
a nonzero value (or returns `true`, for boolean `p`) according to the
[`iszero`](@ref) function. `count(itr)` is equivalent to `p = identity`,
i.e. it counts the number of nonzero (or `true`) elements of `itr`.
```jldoctest
julia> count(i->(4<=i<=6), [2,3,4,5,6])
3
```
"""
function count(pred, itr)
n = 0
for x in itr
n += pred(x)
end
return n
end

"""
countnz(A) -> Integer
Counts the number of nonzero values in array `A` (dense or sparse). Note that this is not a constant-time operation.
For sparse matrices, one should usually use [`nnz`](@ref), which returns the number of stored values.
```jldoctest
julia> A = [1 2 4; 0 0 1; 1 1 0]
3×3 Array{Int64,2}:
1 2 4
0 0 1
1 1 0
julia> countnz(A)
julia> count(A)
6
```
"""
countnz(a) = count(x -> x != 0, a)
function count end

function count(pred, itr)
n = 0
for x in itr
n += !iszero(pred(x))
end
return n
end

count(itr) = count(identity, itr)
2 changes: 1 addition & 1 deletion base/sparse/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Base.LinAlg: At_ldiv_B!, Ac_ldiv_B!

import Base: @get!, acos, acosd, acot, acotd, acsch, asech, asin, asind, asinh,
atan, atand, atanh, broadcast!, chol, conj!, cos, cosc, cosd, cosh, cospi, cot,
cotd, coth, countnz, csc, cscd, csch, ctranspose!, diag, diff, done, dot, eig,
cotd, coth, count, csc, cscd, csch, ctranspose!, diag, diff, done, dot, eig,
exp10, exp2, eye, findn, floor, hash, indmin, inv, issymmetric, istril, istriu,
log10, log2, lu, next, sec, secd, sech, show, sin,
sinc, sind, sinh, sinpi, squeeze, start, sum, summary, tan,
Expand Down
4 changes: 2 additions & 2 deletions base/sparse/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ julia> nnz(A)
```
"""
nnz(S::SparseMatrixCSC) = Int(S.colptr[end]-1)
countnz(S::SparseMatrixCSC) = countnz(S.nzval)
count(S::SparseMatrixCSC) = count(S.nzval)

"""
nonzeros(A)
Expand Down Expand Up @@ -1740,7 +1740,7 @@ indmax(A::SparseMatrixCSC) = findmax(A)[2]
#all(A::SparseMatrixCSC{Bool}, region) = reducedim(all,A,region,true)
#any(A::SparseMatrixCSC{Bool}, region) = reducedim(any,A,region,false)
#sum(A::SparseMatrixCSC{Bool}, region) = reducedim(+,A,region,0,Int)
#sum(A::SparseMatrixCSC{Bool}) = countnz(A)
#sum(A::SparseMatrixCSC{Bool}) = count(A)

## getindex
function rangesearch(haystack::Range, needle)
Expand Down
2 changes: 1 addition & 1 deletion base/sparse/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ SparseVector{Tv,Ti}(n::Integer, nzind::Vector{Ti}, nzval::Vector{Tv}) =
length(x::SparseVector) = x.n
size(x::SparseVector) = (x.n,)
nnz(x::SparseVector) = length(x.nzval)
countnz(x::SparseVector) = countnz(x.nzval)
count(x::SparseVector) = count(x.nzval)
nonzeros(x::SparseVector) = x.nzval
nonzeroinds(x::SparseVector) = x.nzind

Expand Down
2 changes: 1 addition & 1 deletion doc/src/manual/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ In some applications, it is convenient to store explicit zero values in a `Spars
mutating operations). Such explicitly stored zeros are treated as structural nonzeros by many
routines. The [`nnz()`](@ref) function returns the number of elements explicitly stored in the
sparse data structure, including structural nonzeros. In order to count the exact number of actual
values that are nonzero, use [`countnz()`](@ref), which inspects every stored element of a sparse
values that are nonzero, use [`count()`](@ref), which inspects every stored element of a sparse
matrix.

### Sparse matrix constructors
Expand Down
2 changes: 1 addition & 1 deletion doc/src/stdlib/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Base.length(::AbstractArray)
Base.eachindex
Base.linearindices
Base.linearindexing
Base.countnz
Base.count
Base.conj!
Base.stride
Base.strides
Expand Down
2 changes: 1 addition & 1 deletion test/arrayops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

@testset "basics" begin
@test length([1, 2, 3]) == 3
@test countnz([1, 2, 3]) == 3
@test count([1, 2, 3]) == 3

let a = ones(4), b = a+a, c = a-a
@test b[1] === 2. && b[2] === 2. && b[3] === 2. && b[4] === 2.
Expand Down
10 changes: 5 additions & 5 deletions test/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -466,20 +466,20 @@ timesofar("constructors")
@check_bit_operation setindex!(b1, true, t1) BitMatrix

t1 = bitrand(n1, n2)
b2 = bitrand(countnz(t1))
b2 = bitrand(count(t1))
@check_bit_operation setindex!(b1, b2, t1) BitMatrix

m1 = rand(1:n1)
m2 = rand(1:n2)
t1 = bitrand(n1)
b2 = bitrand(countnz(t1), m2)
b2 = bitrand(count(t1), m2)
k2 = randperm(m2)
@check_bit_operation setindex!(b1, b2, t1, 1:m2) BitMatrix
@check_bit_operation setindex!(b1, b2, t1, n2-m2+1:n2) BitMatrix
@check_bit_operation setindex!(b1, b2, t1, k2) BitMatrix

t2 = bitrand(n2)
b2 = bitrand(m1, countnz(t2))
b2 = bitrand(m1, count(t2))
k1 = randperm(m1)
@check_bit_operation setindex!(b1, b2, 1:m1, t2) BitMatrix
@check_bit_operation setindex!(b1, b2, n1-m1+1:n1, t2) BitMatrix
Expand Down Expand Up @@ -1051,9 +1051,9 @@ end

timesofar("datamove")

@testset "countnz & find" begin
@testset "count & find" begin
for m = 0:v1, b1 in Any[bitrand(m), trues(m), falses(m)]
@check_bit_operation countnz(b1) Int
@check_bit_operation count(b1) Int

@check_bit_operation findfirst(b1) Int

Expand Down
10 changes: 5 additions & 5 deletions test/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -258,15 +258,15 @@ immutable SomeFunctor end
@test contains("quick fox", "fox") == true
@test contains("quick fox", "lazy dog") == false

# count & countnz
# count & count

@test count(x->x>0, Int[]) == 0
@test count(x->x>0, -3:5) == 5

@test countnz(Int[]) == 0
@test countnz(Int[0]) == 0
@test countnz(Int[1]) == 1
@test countnz([1, 0, 2, 0, 3, 0, 4]) == 4
@test count(Int[]) == 0
@test count(Int[0]) == 0
@test count(Int[1]) == 1
@test count([1, 0, 2, 0, 3, 0, 4]) == 4


## cumsum, cummin, cummax
Expand Down
Loading

0 comments on commit b51ecbd

Please sign in to comment.