Skip to content

Commit 05280cf

Browse files
committed
make count(f,itr) count the number of nonzero values (fixes #20404), elim countnz in favor of count(itr) (fixes #20403)
1 parent 3c20d76 commit 05280cf

21 files changed

+87
-90
lines changed

base/abstractarray.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1095,7 +1095,7 @@ function cat_t(dims, T::Type, X...)
10951095
catdims = dims2cat(dims)
10961096
shape = cat_shape(catdims, (), map(cat_size, X)...)
10971097
A = cat_similar(X[1], T, shape)
1098-
if T <: Number && countnz(catdims) > 1
1098+
if T <: Number && count(catdims) > 1
10991099
fill!(A, zero(T))
11001100
end
11011101
return _cat(A, shape, catdims, X...)

base/array.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -1393,7 +1393,7 @@ julia> find(A)
13931393
```
13941394
"""
13951395
function find(A)
1396-
nnzA = countnz(A)
1396+
nnzA = count(A)
13971397
I = Vector{Int}(nnzA)
13981398
count = 1
13991399
inds = _index_remapper(A)
@@ -1438,7 +1438,7 @@ julia> findn(A)
14381438
```
14391439
"""
14401440
function findn(A::AbstractMatrix)
1441-
nnzA = countnz(A)
1441+
nnzA = count(A)
14421442
I = similar(A, Int, nnzA)
14431443
J = similar(A, Int, nnzA)
14441444
count = 1
@@ -1470,7 +1470,7 @@ julia> findnz(A)
14701470
```
14711471
"""
14721472
function findnz{T}(A::AbstractMatrix{T})
1473-
nnzA = countnz(A)
1473+
nnzA = count(A)
14741474
I = zeros(Int, nnzA)
14751475
J = zeros(Int, nnzA)
14761476
NZs = Array{T,1}(nnzA)

base/bitarray.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -1565,9 +1565,9 @@ julia> ror(A,5)
15651565
"""
15661566
ror(B::BitVector, i::Integer) = ror!(similar(B), B, i)
15671567

1568-
## countnz & find ##
1568+
## count & find ##
15691569

1570-
function countnz(B::BitArray)
1570+
function count(B::BitArray)
15711571
n = 0
15721572
Bc = B.chunks
15731573
@inbounds for i = 1:length(Bc)
@@ -1728,7 +1728,7 @@ end
17281728

17291729
function find(B::BitArray)
17301730
l = length(B)
1731-
nnzB = countnz(B)
1731+
nnzB = count(B)
17321732
I = Array{Int}(nnzB)
17331733
nnzB == 0 && return I
17341734
Bc = B.chunks
@@ -1762,7 +1762,7 @@ end
17621762
findn(B::BitVector) = find(B)
17631763

17641764
function findn(B::BitMatrix)
1765-
nnzB = countnz(B)
1765+
nnzB = count(B)
17661766
I = Array{Int}(nnzB)
17671767
J = Array{Int}(nnzB)
17681768
count = 1
@@ -1784,7 +1784,7 @@ end
17841784
## Reductions ##
17851785

17861786
sum(A::BitArray, region) = reducedim(+, A, region)
1787-
sum(B::BitArray) = countnz(B)
1787+
sum(B::BitArray) = count(B)
17881788

17891789
function all(B::BitArray)
17901790
isempty(B) && return true

base/bool.jl

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ signbit(x::Bool) = false
7676
sign(x::Bool) = x
7777
abs(x::Bool) = x
7878
abs2(x::Bool) = x
79+
iszero(x::Bool) = !x
7980

8081
<(x::Bool, y::Bool) = y&!x
8182
<=(x::Bool, y::Bool) = y|!x

base/deprecated.jl

+2
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,8 @@ function (::Type{Matrix})()
12241224
return Matrix(0, 0)
12251225
end
12261226

1227+
@deprecate countnz(itr) count(itr)
1228+
12271229
for name in ("alnum", "alpha", "cntrl", "digit", "number", "graph",
12281230
"lower", "print", "punct", "space", "upper", "xdigit")
12291231
f = Symbol("is",name)

base/exports.jl

-1
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,6 @@ export
531531
minmax,
532532
ndims,
533533
nonzeros,
534-
countnz,
535534
ones,
536535
parent,
537536
parentindexes,

base/linalg/bitarray.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ end
134134

135135
## Structure query functions
136136

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

140140
function nonzero_chunks(chunks::Vector{UInt64}, pos0::Int, pos1::Int)

base/linalg/generic.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ function vecnorm(itr, p::Real=2)
440440
return vecnormInf(itr)
441441
elseif p == 0
442442
return convert(typeof(float(real(zero(eltype(itr))))),
443-
countnz(itr))
443+
count(itr))
444444
elseif p == -Inf
445445
return vecnormMinusInf(itr)
446446
else

base/multidimensional.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ wrapped with `LogicalIndex` upon calling `to_indices`.
276276
immutable LogicalIndex{T, A<:AbstractArray{Bool}} <: AbstractVector{T}
277277
mask::A
278278
sum::Int
279-
LogicalIndex(mask::A) = new(mask, countnz(mask))
279+
LogicalIndex(mask::A) = new(mask, count(mask))
280280
end
281281
LogicalIndex(mask::AbstractVector{Bool}) = LogicalIndex{Int, typeof(mask)}(mask)
282282
LogicalIndex{N}(mask::AbstractArray{Bool, N}) = LogicalIndex{CartesianIndex{N}, typeof(mask)}(mask)
@@ -467,7 +467,7 @@ end
467467

468468
@generated function findn{T,N}(A::AbstractArray{T,N})
469469
quote
470-
nnzA = countnz(A)
470+
nnzA = count(A)
471471
@nexprs $N d->(I_d = Array{Int}(nnzA))
472472
k = 1
473473
@nloops $N i A begin
@@ -1066,7 +1066,7 @@ end
10661066

10671067
@generated function findn{N}(B::BitArray{N})
10681068
quote
1069-
nnzB = countnz(B)
1069+
nnzB = count(B)
10701070
I = ntuple(x->Array{Int}(nnzB), $N)
10711071
if nnzB > 0
10721072
count = 1

base/reduce.jl

+18-23
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ julia> sum(1:20)
348348
```
349349
"""
350350
sum(a) = mapreduce(identity, +, a)
351-
sum(a::AbstractArray{Bool}) = countnz(a)
351+
sum(a::AbstractArray{Bool}) = count(a)
352352

353353

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

640-
641-
## countnz & count
642-
643640
"""
644641
count(p, itr) -> Integer
642+
count(itr)
645643
646-
Count the number of elements in `itr` for which predicate `p` returns `true`.
644+
Count the number of elements in `itr` for which the function `p` returns
645+
a nonzero value (or returns `true`, for boolean `p`) according to the
646+
[`iszero`](@ref) function. `count(itr)` is equivalent to `p = identity`,
647+
i.e. it counts the number of nonzero (or `true`) elements of `itr`.
647648
648649
```jldoctest
649650
julia> count(i->(4<=i<=6), [2,3,4,5,6])
650651
3
651-
```
652-
"""
653-
function count(pred, itr)
654-
n = 0
655-
for x in itr
656-
n += pred(x)
657-
end
658-
return n
659-
end
660-
661-
"""
662-
countnz(A) -> Integer
663-
664-
Counts the number of nonzero values in array `A` (dense or sparse). Note that this is not a constant-time operation.
665-
For sparse matrices, one should usually use [`nnz`](@ref), which returns the number of stored values.
666652
667-
```jldoctest
668653
julia> A = [1 2 4; 0 0 1; 1 1 0]
669654
3×3 Array{Int64,2}:
670655
1 2 4
671656
0 0 1
672657
1 1 0
673658
674-
julia> countnz(A)
659+
julia> count(A)
675660
6
676661
```
677662
"""
678-
countnz(a) = count(x -> x != 0, a)
663+
function count end
664+
665+
function count{F}(pred::F, itr)
666+
n = 0
667+
for x in itr
668+
n += !iszero(pred(x))
669+
end
670+
return n
671+
end
672+
673+
count(itr) = count(identity, itr)

base/sparse/sparse.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Base.LinAlg: At_ldiv_B!, Ac_ldiv_B!
1414

1515
import Base: @get!, acos, acosd, acot, acotd, acsch, asech, asin, asind, asinh,
1616
atan, atand, atanh, broadcast!, chol, conj!, cos, cosc, cosd, cosh, cospi, cot,
17-
cotd, coth, countnz, csc, cscd, csch, ctranspose!, diag, diff, done, dot, eig,
17+
cotd, coth, count, csc, cscd, csch, ctranspose!, diag, diff, done, dot, eig,
1818
exp10, exp2, eye, findn, floor, hash, indmin, inv, issymmetric, istril, istriu,
1919
log10, log2, lu, next, sec, secd, sech, show, sin,
2020
sinc, sind, sinh, sinpi, squeeze, start, sum, summary, tan,

base/sparse/sparsematrix.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ julia> nnz(A)
4343
```
4444
"""
4545
nnz(S::SparseMatrixCSC) = Int(S.colptr[end]-1)
46-
countnz(S::SparseMatrixCSC) = countnz(S.nzval)
46+
count(S::SparseMatrixCSC) = count(S.nzval)
4747

4848
"""
4949
nonzeros(A)
@@ -1740,7 +1740,7 @@ indmax(A::SparseMatrixCSC) = findmax(A)[2]
17401740
#all(A::SparseMatrixCSC{Bool}, region) = reducedim(all,A,region,true)
17411741
#any(A::SparseMatrixCSC{Bool}, region) = reducedim(any,A,region,false)
17421742
#sum(A::SparseMatrixCSC{Bool}, region) = reducedim(+,A,region,0,Int)
1743-
#sum(A::SparseMatrixCSC{Bool}) = countnz(A)
1743+
#sum(A::SparseMatrixCSC{Bool}) = count(A)
17441744

17451745
## getindex
17461746
function rangesearch(haystack::Range, needle)

base/sparse/sparsevector.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ SparseVector{Tv,Ti}(n::Integer, nzind::Vector{Ti}, nzval::Vector{Tv}) =
3030
length(x::SparseVector) = x.n
3131
size(x::SparseVector) = (x.n,)
3232
nnz(x::SparseVector) = length(x.nzval)
33-
countnz(x::SparseVector) = countnz(x.nzval)
33+
count(x::SparseVector) = count(x.nzval)
3434
nonzeros(x::SparseVector) = x.nzval
3535
nonzeroinds(x::SparseVector) = x.nzind
3636

doc/src/manual/arrays.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ In some applications, it is convenient to store explicit zero values in a `Spars
743743
mutating operations). Such explicitly stored zeros are treated as structural nonzeros by many
744744
routines. The [`nnz()`](@ref) function returns the number of elements explicitly stored in the
745745
sparse data structure, including structural nonzeros. In order to count the exact number of actual
746-
values that are nonzero, use [`countnz()`](@ref), which inspects every stored element of a sparse
746+
values that are nonzero, use [`count()`](@ref), which inspects every stored element of a sparse
747747
matrix.
748748

749749
### Sparse matrix constructors

doc/src/stdlib/arrays.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Base.length(::AbstractArray)
1111
Base.eachindex
1212
Base.linearindices
1313
Base.linearindexing
14-
Base.countnz
14+
Base.count
1515
Base.conj!
1616
Base.stride
1717
Base.strides

test/arrayops.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
@testset "basics" begin
66
@test length([1, 2, 3]) == 3
7-
@test countnz([1, 2, 3]) == 3
7+
@test count([1, 2, 3]) == 3
88

99
let a = ones(4), b = a+a, c = a-a
1010
@test b[1] === 2. && b[2] === 2. && b[3] === 2. && b[4] === 2.

test/bitarray.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -466,20 +466,20 @@ timesofar("constructors")
466466
@check_bit_operation setindex!(b1, true, t1) BitMatrix
467467

468468
t1 = bitrand(n1, n2)
469-
b2 = bitrand(countnz(t1))
469+
b2 = bitrand(count(t1))
470470
@check_bit_operation setindex!(b1, b2, t1) BitMatrix
471471

472472
m1 = rand(1:n1)
473473
m2 = rand(1:n2)
474474
t1 = bitrand(n1)
475-
b2 = bitrand(countnz(t1), m2)
475+
b2 = bitrand(count(t1), m2)
476476
k2 = randperm(m2)
477477
@check_bit_operation setindex!(b1, b2, t1, 1:m2) BitMatrix
478478
@check_bit_operation setindex!(b1, b2, t1, n2-m2+1:n2) BitMatrix
479479
@check_bit_operation setindex!(b1, b2, t1, k2) BitMatrix
480480

481481
t2 = bitrand(n2)
482-
b2 = bitrand(m1, countnz(t2))
482+
b2 = bitrand(m1, count(t2))
483483
k1 = randperm(m1)
484484
@check_bit_operation setindex!(b1, b2, 1:m1, t2) BitMatrix
485485
@check_bit_operation setindex!(b1, b2, n1-m1+1:n1, t2) BitMatrix
@@ -1051,9 +1051,9 @@ end
10511051

10521052
timesofar("datamove")
10531053

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

10581058
@check_bit_operation findfirst(b1) Int
10591059

test/reduce.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -258,15 +258,15 @@ immutable SomeFunctor end
258258
@test contains("quick fox", "fox") == true
259259
@test contains("quick fox", "lazy dog") == false
260260

261-
# count & countnz
261+
# count & count
262262

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

266-
@test countnz(Int[]) == 0
267-
@test countnz(Int[0]) == 0
268-
@test countnz(Int[1]) == 1
269-
@test countnz([1, 0, 2, 0, 3, 0, 4]) == 4
266+
@test count(Int[]) == 0
267+
@test count(Int[0]) == 0
268+
@test count(Int[1]) == 1
269+
@test count([1, 0, 2, 0, 3, 0, 4]) == 4
270270

271271

272272
## cumsum, cummin, cummax

0 commit comments

Comments
 (0)