Skip to content

Deprecate countnz in favor of using count(predicate, x) #23485

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

Merged
merged 1 commit into from
Aug 29, 2017
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
2 changes: 1 addition & 1 deletion base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1275,7 +1275,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(!iszero, catdims) > 1
fill!(A, zero(T))
end
return _cat(A, shape, catdims, X...)
Expand Down
30 changes: 15 additions & 15 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1945,14 +1945,14 @@ julia> find(zeros(3))
```
"""
function find(A)
nnzA = countnz(A)
nnzA = count(t -> t != 0, A)
I = Vector{Int}(nnzA)
count = 1
cnt = 1
inds = _index_remapper(A)
for (i,a) in enumerate(A)
if a != 0
I[count] = inds[i]
count += 1
I[cnt] = inds[i]
cnt += 1
end
end
return I
Expand Down Expand Up @@ -1991,15 +1991,15 @@ julia> findn(A)
```
"""
function findn(A::AbstractMatrix)
nnzA = countnz(A)
nnzA = count(t -> t != 0, A)
I = similar(A, Int, nnzA)
J = similar(A, Int, nnzA)
count = 1
cnt = 1
for j=indices(A,2), i=indices(A,1)
if A[i,j] != 0
I[count] = i
J[count] = j
count += 1
I[cnt] = i
J[cnt] = j
cnt += 1
end
end
return (I, J)
Expand All @@ -2024,19 +2024,19 @@ julia> findnz(A)
```
"""
function findnz(A::AbstractMatrix{T}) where T
nnzA = countnz(A)
nnzA = count(t -> t != 0, A)
I = zeros(Int, nnzA)
J = zeros(Int, nnzA)
NZs = Array{T,1}(nnzA)
count = 1
cnt = 1
if nnzA > 0
for j=indices(A,2), i=indices(A,1)
Aij = A[i,j]
if Aij != 0
I[count] = i
J[count] = j
NZs[count] = Aij
count += 1
I[cnt] = i
J[cnt] = j
NZs[cnt] = Aij
cnt += 1
end
end
end
Expand Down
19 changes: 9 additions & 10 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1614,17 +1614,16 @@ 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)
n += count_ones(Bc[i])
end
return n
end
count(B::BitArray) = countnz(B)

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

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

function findn(B::BitMatrix)
nnzB = countnz(B)
nnzB = count(B)
I = Vector{Int}(nnzB)
J = Vector{Int}(nnzB)
count = 1
cnt = 1
for j = 1:size(B,2), i = 1:size(B,1)
if B[i,j]
I[count] = i
J[count] = j
count += 1
I[cnt] = i
J[cnt] = j
cnt += 1
end
end
return I, J
Expand All @@ -1834,7 +1833,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
7 changes: 7 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1739,6 +1739,13 @@ end

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

# PR #23485
export foo
function countnz(x)
depwarn("countnz(x) is deprecated, use either count(!iszero, x) or count(t -> t != 0, x) instead.", :depwarn)
return count(t -> t != 0, x)
end

# issue #22791
@deprecate select partialsort
@deprecate select! partialsort!
Expand Down
1 change: 0 additions & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,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 @@ -133,7 +133,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(!iszero, A - A.')==0
ishermitian(A::BitMatrix) = issymmetric(A)

function nonzero_chunks(chunks::Vector{UInt64}, pos0::Int, pos1::Int)
Expand Down
9 changes: 6 additions & 3 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ wrapped with `LogicalIndex` upon calling `to_indices`.
struct LogicalIndex{T, A<:AbstractArray{Bool}} <: AbstractVector{T}
mask::A
sum::Int
LogicalIndex{T,A}(mask::A) where {T,A<:AbstractArray{Bool}} = new(mask, countnz(mask))
LogicalIndex{T,A}(mask::A) where {T,A<:AbstractArray{Bool}} = new(mask, count(mask))
end
LogicalIndex(mask::AbstractVector{Bool}) = LogicalIndex{Int, typeof(mask)}(mask)
LogicalIndex(mask::AbstractArray{Bool, N}) where {N} = LogicalIndex{CartesianIndex{N}, typeof(mask)}(mask)
Expand Down Expand Up @@ -574,9 +574,12 @@ end

##

# small helper function since we cannot use a closure in a generated function
_countnz(x) = x != 0

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

@generated function findn(B::BitArray{N}) where N
quote
nnzB = countnz(B)
nnzB = count(B)
I = ntuple(x->Vector{Int}(nnzB), Val($N))
if nnzB > 0
count = 1
Expand Down
23 changes: 2 additions & 21 deletions base/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,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 @@ -670,7 +670,7 @@ function contains(eq::Function, itr, x)
end


## countnz & count
## count

"""
count(p, itr) -> Integer
Expand Down Expand Up @@ -703,22 +703,3 @@ function count(pred, a::AbstractArray)
return n
end
count(itr) = count(identity, itr)

"""
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)
6
```
"""
countnz(a) = count(x -> x != 0, a)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the deprecation is different from the current definition, which (as you remarked) is technically breaking. I think the best approach would be to use Base.depwarn to print a custom deprecation warning proposing both forms, but continue calling x -> x != 0 under the hood.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed the deprecation to suggest both versions following your suggestion.

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!, A_rdiv_B!, A_rdiv_Bc!

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, adjoint!, diag, diff, done, dot, eig,
cotd, coth, count, csc, cscd, csch, adjoint!, 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
11 changes: 3 additions & 8 deletions base/sparse/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ julia> nnz(A)
3
```
"""
nnz(S::SparseMatrixCSC) = Int(S.colptr[S.n + 1]-1)
countnz(S::SparseMatrixCSC) = countnz(S.nzval)
count(S::SparseMatrixCSC) = count(S.nzval)
nnz(S::SparseMatrixCSC) = Int(S.colptr[S.n + 1] - 1)
count(S::SparseMatrixCSC) = count(S.nzval)
count(pred, S::SparseMatrixCSC) = count(pred, S.nzval) + pred(zero(eltype(S)))*(prod(size(S)) - nnz(S))

"""
nonzeros(A)
Expand Down Expand Up @@ -1911,11 +1911,6 @@ findmax(A::SparseMatrixCSC) = (r=findmax(A,(1,2)); (r[1][1], r[2][1]))
indmin(A::SparseMatrixCSC) = findmin(A)[2]
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)

## getindex
function rangesearch(haystack::Range, needle)
(i,rem) = divrem(needle - first(haystack), step(haystack))
Expand Down
10 changes: 5 additions & 5 deletions base/sparse/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ const SparseVectorUnion{T} = Union{SparseVector{T}, SparseColumnView{T}}

### Basic properties

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)
length(x::SparseVector) = x.n
size(x::SparseVector) = (x.n,)
nnz(x::SparseVector) = length(x.nzval)
count(x::SparseVector) = count(x.nzval)
count(f, x::SparseVector) = count(f, x.nzval) + f(zero(eltype(x)))*(length(x) - nnz(x))

nonzeros(x::SparseVector) = x.nzval
function nonzeros(x::SparseColumnView)
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 @@ -745,7 +745,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
numerical nonzeros, use [`countnz()`](@ref), which inspects every stored element of a sparse
numerical nonzeros, use [`count(!iszero, x)`](@ref), which inspects every stored element of a sparse
matrix. [`dropzeros()`](@ref), and the in-place [`dropzeros!()`](@ref), can be used to
remove stored zeros from the sparse matrix.

Expand Down
1 change: 0 additions & 1 deletion doc/src/stdlib/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ Base.length(::AbstractArray)
Base.eachindex
Base.linearindices
Base.IndexStyle
Base.countnz
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 @@ -6,7 +6,7 @@ using TestHelpers.OAs

@testset "basics" begin
@test length([1, 2, 3]) == 3
@test countnz([1, 2, 3]) == 3
@test count(!iszero, [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 @@ -471,20 +471,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 @@ -1056,9 +1056,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 @@ -318,7 +318,7 @@ struct SomeFunctor end
@test contains("quick fox", "fox") == true
@test contains("quick fox", "lazy dog") == false

# count & countnz
# count

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

@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(!iszero, Int[]) == 0
@test count(!iszero, Int[0]) == 0
@test count(!iszero, Int[1]) == 1
@test count(!iszero, [1, 0, 2, 0, 3, 0, 4]) == 4


## cumsum, cummin, cummax
Expand Down
Loading