Skip to content

Commit c1b2b86

Browse files
committed
Deprecate countnz in favor of using count(predicate, x)
1 parent ff706aa commit c1b2b86

20 files changed

+91
-116
lines changed

base/abstractarray.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1275,7 +1275,7 @@ function cat_t(dims, T::Type, X...)
12751275
catdims = dims2cat(dims)
12761276
shape = cat_shape(catdims, (), map(cat_size, X)...)
12771277
A = cat_similar(X[1], T, shape)
1278-
if T <: Number && countnz(catdims) > 1
1278+
if T <: Number && count(!iszero, catdims) > 1
12791279
fill!(A, zero(T))
12801280
end
12811281
return _cat(A, shape, catdims, X...)

base/array.jl

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1945,14 +1945,14 @@ julia> find(zeros(3))
19451945
```
19461946
"""
19471947
function find(A)
1948-
nnzA = countnz(A)
1948+
nnzA = count(t -> t != 0, A)
19491949
I = Vector{Int}(nnzA)
1950-
count = 1
1950+
cnt = 1
19511951
inds = _index_remapper(A)
19521952
for (i,a) in enumerate(A)
19531953
if a != 0
1954-
I[count] = inds[i]
1955-
count += 1
1954+
I[cnt] = inds[i]
1955+
cnt += 1
19561956
end
19571957
end
19581958
return I
@@ -1991,15 +1991,15 @@ julia> findn(A)
19911991
```
19921992
"""
19931993
function findn(A::AbstractMatrix)
1994-
nnzA = countnz(A)
1994+
nnzA = count(t -> t != 0, A)
19951995
I = similar(A, Int, nnzA)
19961996
J = similar(A, Int, nnzA)
1997-
count = 1
1997+
cnt = 1
19981998
for j=indices(A,2), i=indices(A,1)
19991999
if A[i,j] != 0
2000-
I[count] = i
2001-
J[count] = j
2002-
count += 1
2000+
I[cnt] = i
2001+
J[cnt] = j
2002+
cnt += 1
20032003
end
20042004
end
20052005
return (I, J)
@@ -2024,19 +2024,19 @@ julia> findnz(A)
20242024
```
20252025
"""
20262026
function findnz(A::AbstractMatrix{T}) where T
2027-
nnzA = countnz(A)
2027+
nnzA = count(t -> t != 0, A)
20282028
I = zeros(Int, nnzA)
20292029
J = zeros(Int, nnzA)
20302030
NZs = Array{T,1}(nnzA)
2031-
count = 1
2031+
cnt = 1
20322032
if nnzA > 0
20332033
for j=indices(A,2), i=indices(A,1)
20342034
Aij = A[i,j]
20352035
if Aij != 0
2036-
I[count] = i
2037-
J[count] = j
2038-
NZs[count] = Aij
2039-
count += 1
2036+
I[cnt] = i
2037+
J[cnt] = j
2038+
NZs[cnt] = Aij
2039+
cnt += 1
20402040
end
20412041
end
20422042
end

base/bitarray.jl

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,17 +1614,16 @@ julia> ror(A,5)
16141614
"""
16151615
ror(B::BitVector, i::Integer) = ror!(similar(B), B, i)
16161616

1617-
## countnz & find ##
1617+
## count & find ##
16181618

1619-
function countnz(B::BitArray)
1619+
function count(B::BitArray)
16201620
n = 0
16211621
Bc = B.chunks
16221622
@inbounds for i = 1:length(Bc)
16231623
n += count_ones(Bc[i])
16241624
end
16251625
return n
16261626
end
1627-
count(B::BitArray) = countnz(B)
16281627

16291628
# returns the index of the next non-zero element, or 0 if all zeros
16301629
function findnext(B::BitArray, start::Integer)
@@ -1778,7 +1777,7 @@ end
17781777

17791778
function find(B::BitArray)
17801779
l = length(B)
1781-
nnzB = countnz(B)
1780+
nnzB = count(B)
17821781
I = Vector{Int}(nnzB)
17831782
nnzB == 0 && return I
17841783
Bc = B.chunks
@@ -1812,15 +1811,15 @@ end
18121811
findn(B::BitVector) = find(B)
18131812

18141813
function findn(B::BitMatrix)
1815-
nnzB = countnz(B)
1814+
nnzB = count(B)
18161815
I = Vector{Int}(nnzB)
18171816
J = Vector{Int}(nnzB)
1818-
count = 1
1817+
cnt = 1
18191818
for j = 1:size(B,2), i = 1:size(B,1)
18201819
if B[i,j]
1821-
I[count] = i
1822-
J[count] = j
1823-
count += 1
1820+
I[cnt] = i
1821+
J[cnt] = j
1822+
cnt += 1
18241823
end
18251824
end
18261825
return I, J
@@ -1834,7 +1833,7 @@ end
18341833
## Reductions ##
18351834

18361835
sum(A::BitArray, region) = reducedim(+, A, region)
1837-
sum(B::BitArray) = countnz(B)
1836+
sum(B::BitArray) = count(B)
18381837

18391838
function all(B::BitArray)
18401839
isempty(B) && return true

base/deprecated.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,6 +1739,8 @@ end
17391739

17401740
@deprecate IOContext(io::IO, key, value) IOContext(io, key=>value)
17411741

1742+
@deprecate countnz(x) count(!iszero, x)
1743+
17421744
# issue #22791
17431745
@deprecate select partialsort
17441746
@deprecate select! partialsort!

base/exports.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,6 @@ export
488488
minmax,
489489
ndims,
490490
nonzeros,
491-
countnz,
492491
ones,
493492
parent,
494493
parentindexes,

base/linalg/bitarray.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ end
133133

134134
## Structure query functions
135135

136-
issymmetric(A::BitMatrix) = size(A, 1)==size(A, 2) && countnz(A - A.')==0
136+
issymmetric(A::BitMatrix) = size(A, 1)==size(A, 2) && count(!iszero, A - A.')==0
137137
ishermitian(A::BitMatrix) = issymmetric(A)
138138

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

base/multidimensional.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ wrapped with `LogicalIndex` upon calling `to_indices`.
394394
struct LogicalIndex{T, A<:AbstractArray{Bool}} <: AbstractVector{T}
395395
mask::A
396396
sum::Int
397-
LogicalIndex{T,A}(mask::A) where {T,A<:AbstractArray{Bool}} = new(mask, countnz(mask))
397+
LogicalIndex{T,A}(mask::A) where {T,A<:AbstractArray{Bool}} = new(mask, count(mask))
398398
end
399399
LogicalIndex(mask::AbstractVector{Bool}) = LogicalIndex{Int, typeof(mask)}(mask)
400400
LogicalIndex(mask::AbstractArray{Bool, N}) where {N} = LogicalIndex{CartesianIndex{N}, typeof(mask)}(mask)
@@ -576,7 +576,7 @@ end
576576

577577
@generated function findn(A::AbstractArray{T,N}) where {T,N}
578578
quote
579-
nnzA = countnz(A)
579+
nnzA = count(!iszero, A)
580580
@nexprs $N d->(I_d = Vector{Int}(nnzA))
581581
k = 1
582582
@nloops $N i A begin
@@ -1301,7 +1301,7 @@ end
13011301

13021302
@generated function findn(B::BitArray{N}) where N
13031303
quote
1304-
nnzB = countnz(B)
1304+
nnzB = count(B)
13051305
I = ntuple(x->Vector{Int}(nnzB), Val($N))
13061306
if nnzB > 0
13071307
count = 1

base/reduce.jl

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ julia> sum(1:20)
359359
```
360360
"""
361361
sum(a) = mapreduce(identity, +, a)
362-
sum(a::AbstractArray{Bool}) = countnz(a)
362+
sum(a::AbstractArray{Bool}) = count(a)
363363

364364

365365
# Kahan (compensated) summation: O(1) error growth, at the expense
@@ -670,7 +670,7 @@ function contains(eq::Function, itr, x)
670670
end
671671

672672

673-
## countnz & count
673+
## count
674674

675675
"""
676676
count(p, itr) -> Integer
@@ -703,22 +703,3 @@ function count(pred, a::AbstractArray)
703703
return n
704704
end
705705
count(itr) = count(identity, itr)
706-
707-
"""
708-
countnz(A) -> Integer
709-
710-
Counts the number of nonzero values in array `A` (dense or sparse). Note that this is not a constant-time operation.
711-
For sparse matrices, one should usually use [`nnz`](@ref), which returns the number of stored values.
712-
713-
```jldoctest
714-
julia> A = [1 2 4; 0 0 1; 1 1 0]
715-
3×3 Array{Int64,2}:
716-
1 2 4
717-
0 0 1
718-
1 1 0
719-
720-
julia> countnz(A)
721-
6
722-
```
723-
"""
724-
countnz(a) = count(x -> x != 0, a)

base/sparse/sparse.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Base.LinAlg: At_ldiv_B!, Ac_ldiv_B!, A_rdiv_B!, A_rdiv_Bc!
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, adjoint!, diag, diff, done, dot, eig,
17+
cotd, coth, count, csc, cscd, csch, adjoint!, 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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@ julia> nnz(A)
7070
3
7171
```
7272
"""
73-
nnz(S::SparseMatrixCSC) = Int(S.colptr[S.n + 1]-1)
74-
countnz(S::SparseMatrixCSC) = countnz(S.nzval)
75-
count(S::SparseMatrixCSC) = count(S.nzval)
73+
nnz(S::SparseMatrixCSC) = Int(S.colptr[S.n + 1] - 1)
74+
count(S::SparseMatrixCSC) = count(S.nzval)
75+
count(f, S::SparseMatrixCSC) = count(f, S.nzval) + f(zero(eltype(S)))*(prod(size(S)) - nnz(S))
7676

7777
"""
7878
nonzeros(A)
@@ -1911,11 +1911,6 @@ findmax(A::SparseMatrixCSC) = (r=findmax(A,(1,2)); (r[1][1], r[2][1]))
19111911
indmin(A::SparseMatrixCSC) = findmin(A)[2]
19121912
indmax(A::SparseMatrixCSC) = findmax(A)[2]
19131913

1914-
#all(A::SparseMatrixCSC{Bool}, region) = reducedim(all,A,region,true)
1915-
#any(A::SparseMatrixCSC{Bool}, region) = reducedim(any,A,region,false)
1916-
#sum(A::SparseMatrixCSC{Bool}, region) = reducedim(+,A,region,0,Int)
1917-
#sum(A::SparseMatrixCSC{Bool}) = countnz(A)
1918-
19191914
## getindex
19201915
function rangesearch(haystack::Range, needle)
19211916
(i,rem) = divrem(needle - first(haystack), step(haystack))

base/sparse/sparsevector.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ const SparseVectorUnion{T} = Union{SparseVector{T}, SparseColumnView{T}}
3737

3838
### Basic properties
3939

40-
length(x::SparseVector) = x.n
41-
size(x::SparseVector) = (x.n,)
42-
nnz(x::SparseVector) = length(x.nzval)
43-
countnz(x::SparseVector) = countnz(x.nzval)
44-
count(x::SparseVector) = count(x.nzval)
40+
length(x::SparseVector) = x.n
41+
size(x::SparseVector) = (x.n,)
42+
nnz(x::SparseVector) = length(x.nzval)
43+
count(x::SparseVector) = count(x.nzval)
44+
count(f, x::SparseVector) = count(f, x.nzval) + f(zero(eltype(x)))*(length(x) - nnz(x))
4545

4646
nonzeros(x::SparseVector) = x.nzval
4747
function nonzeros(x::SparseColumnView)

doc/src/manual/arrays.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ In some applications, it is convenient to store explicit zero values in a `Spars
745745
mutating operations). Such explicitly stored zeros are treated as structural nonzeros by many
746746
routines. The [`nnz()`](@ref) function returns the number of elements explicitly stored in the
747747
sparse data structure, including structural nonzeros. In order to count the exact number of
748-
numerical nonzeros, use [`countnz()`](@ref), which inspects every stored element of a sparse
748+
numerical nonzeros, use [`count(!iszero, x)`](@ref), which inspects every stored element of a sparse
749749
matrix. [`dropzeros()`](@ref), and the in-place [`dropzeros!()`](@ref), can be used to
750750
remove stored zeros from the sparse matrix.
751751

doc/src/stdlib/arrays.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ Base.length(::AbstractArray)
4242
Base.eachindex
4343
Base.linearindices
4444
Base.IndexStyle
45-
Base.countnz
4645
Base.conj!
4746
Base.stride
4847
Base.strides

test/arrayops.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ using TestHelpers.OAs
66

77
@testset "basics" begin
88
@test length([1, 2, 3]) == 3
9-
@test countnz([1, 2, 3]) == 3
9+
@test count(!iszero, [1, 2, 3]) == 3
1010

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

test/bitarray.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -471,20 +471,20 @@ timesofar("constructors")
471471
@check_bit_operation setindex!(b1, true, t1) BitMatrix
472472

473473
t1 = bitrand(n1, n2)
474-
b2 = bitrand(countnz(t1))
474+
b2 = bitrand(count(t1))
475475
@check_bit_operation setindex!(b1, b2, t1) BitMatrix
476476

477477
m1 = rand(1:n1)
478478
m2 = rand(1:n2)
479479
t1 = bitrand(n1)
480-
b2 = bitrand(countnz(t1), m2)
480+
b2 = bitrand(count(t1), m2)
481481
k2 = randperm(m2)
482482
@check_bit_operation setindex!(b1, b2, t1, 1:m2) BitMatrix
483483
@check_bit_operation setindex!(b1, b2, t1, n2-m2+1:n2) BitMatrix
484484
@check_bit_operation setindex!(b1, b2, t1, k2) BitMatrix
485485

486486
t2 = bitrand(n2)
487-
b2 = bitrand(m1, countnz(t2))
487+
b2 = bitrand(m1, count(t2))
488488
k1 = randperm(m1)
489489
@check_bit_operation setindex!(b1, b2, 1:m1, t2) BitMatrix
490490
@check_bit_operation setindex!(b1, b2, n1-m1+1:n1, t2) BitMatrix
@@ -1056,9 +1056,9 @@ end
10561056

10571057
timesofar("datamove")
10581058

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

10631063
@check_bit_operation findfirst(b1) Int
10641064

test/reduce.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ struct SomeFunctor end
318318
@test contains("quick fox", "fox") == true
319319
@test contains("quick fox", "lazy dog") == false
320320

321-
# count & countnz
321+
# count
322322

323323
@test count(x->x>0, Int[]) == count(Bool[]) == 0
324324
@test count(x->x>0, -3:5) == count((-3:5) .> 0) == 5
@@ -333,10 +333,10 @@ end
333333
@test count(iseven(x) for x in 1:10 if x < 7) == 3
334334
@test count(iseven(x) for x in 1:10 if x < -7) == 0
335335

336-
@test countnz(Int[]) == 0
337-
@test countnz(Int[0]) == 0
338-
@test countnz(Int[1]) == 1
339-
@test countnz([1, 0, 2, 0, 3, 0, 4]) == 4
336+
@test count(!iszero, Int[]) == 0
337+
@test count(!iszero, Int[0]) == 0
338+
@test count(!iszero, Int[1]) == 1
339+
@test count(!iszero, [1, 0, 2, 0, 3, 0, 4]) == 4
340340

341341

342342
## cumsum, cummin, cummax

0 commit comments

Comments
 (0)