diff --git a/base/abstractarray.jl b/base/abstractarray.jl index d767e9205d71e..5790c7c82cc7f 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -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...) diff --git a/base/array.jl b/base/array.jl index 4844173a8bad2..d59b154e86b54 100644 --- a/base/array.jl +++ b/base/array.jl @@ -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) @@ -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 @@ -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) diff --git a/base/bitarray.jl b/base/bitarray.jl index e3f71da10f0d8..5c071e94a5f5e 100644 --- a/base/bitarray.jl +++ b/base/bitarray.jl @@ -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) @@ -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 @@ -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 @@ -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 diff --git a/base/bool.jl b/base/bool.jl index f42324a90e105..b65e56b0065db 100644 --- a/base/bool.jl +++ b/base/bool.jl @@ -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 diff --git a/base/deprecated.jl b/base/deprecated.jl index b538e3da0b5e8..6f25cabf34bc6 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -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) diff --git a/base/exports.jl b/base/exports.jl index 829885b21da87..e2a33aa3ca303 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -531,7 +531,6 @@ export minmax, ndims, nonzeros, - countnz, ones, parent, parentindexes, diff --git a/base/linalg/bitarray.jl b/base/linalg/bitarray.jl index 2b053e52745de..a02c93f7113b4 100644 --- a/base/linalg/bitarray.jl +++ b/base/linalg/bitarray.jl @@ -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) diff --git a/base/linalg/generic.jl b/base/linalg/generic.jl index 3ee643c384c05..006899b60e002 100644 --- a/base/linalg/generic.jl +++ b/base/linalg/generic.jl @@ -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 diff --git a/base/multidimensional.jl b/base/multidimensional.jl index ffe1b10d7a7af..ddec40d6781ba 100644 --- a/base/multidimensional.jl +++ b/base/multidimensional.jl @@ -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) @@ -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 @@ -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 diff --git a/base/reduce.jl b/base/reduce.jl index d943b60370a37..cc4e3d9eda5f2 100644 --- a/base/reduce.jl +++ b/base/reduce.jl @@ -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 @@ -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) diff --git a/base/sparse/sparse.jl b/base/sparse/sparse.jl index 8f97fcecb57de..eaca513e6ba9b 100644 --- a/base/sparse/sparse.jl +++ b/base/sparse/sparse.jl @@ -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, diff --git a/base/sparse/sparsematrix.jl b/base/sparse/sparsematrix.jl index 8732875eee35f..df7532f7af1f4 100644 --- a/base/sparse/sparsematrix.jl +++ b/base/sparse/sparsematrix.jl @@ -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) @@ -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) diff --git a/base/sparse/sparsevector.jl b/base/sparse/sparsevector.jl index ef94ec75c4006..758bb82c46a9b 100644 --- a/base/sparse/sparsevector.jl +++ b/base/sparse/sparsevector.jl @@ -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 diff --git a/doc/src/manual/arrays.md b/doc/src/manual/arrays.md index 33cc36bae296a..ebd32afbd2bad 100644 --- a/doc/src/manual/arrays.md +++ b/doc/src/manual/arrays.md @@ -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 diff --git a/doc/src/stdlib/arrays.md b/doc/src/stdlib/arrays.md index 47bb3663fd2de..9a241d964f7e6 100644 --- a/doc/src/stdlib/arrays.md +++ b/doc/src/stdlib/arrays.md @@ -11,7 +11,7 @@ Base.length(::AbstractArray) Base.eachindex Base.linearindices Base.linearindexing -Base.countnz +Base.count Base.conj! Base.stride Base.strides diff --git a/test/arrayops.jl b/test/arrayops.jl index 96f212198152c..6dcc8b7f8c568 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -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. diff --git a/test/bitarray.jl b/test/bitarray.jl index caa518eaab58b..6d4cb0af416ed 100644 --- a/test/bitarray.jl +++ b/test/bitarray.jl @@ -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 @@ -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 diff --git a/test/reduce.jl b/test/reduce.jl index 3883646024e2f..66b1694a5df99 100644 --- a/test/reduce.jl +++ b/test/reduce.jl @@ -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 diff --git a/test/sparse/sparse.jl b/test/sparse/sparse.jl index 1786aa7a41632..14caa8b8352e8 100644 --- a/test/sparse/sparse.jl +++ b/test/sparse/sparse.jl @@ -656,56 +656,56 @@ end @testset "setindex" begin a = spzeros(Int, 10, 10) - @test countnz(a) == 0 + @test count(a) == 0 a[1,:] = 1 - @test countnz(a) == 10 + @test count(a) == 10 @test a[1,:] == sparse(ones(Int,10)) a[:,2] = 2 - @test countnz(a) == 19 + @test count(a) == 19 @test a[:,2] == 2*sparse(ones(Int,10)) b = copy(a) # Zero-assignment behavior of setindex!(A, v, i, j) a[1,3] = 0 @test nnz(a) == 19 - @test countnz(a) == 18 + @test count(a) == 18 a[2,1] = 0 @test nnz(a) == 19 - @test countnz(a) == 18 + @test count(a) == 18 # Zero-assignment behavior of setindex!(A, v, I, J) a[1,:] = 0 @test nnz(a) == 19 - @test countnz(a) == 9 + @test count(a) == 9 a[2,:] = 0 @test nnz(a) == 19 - @test countnz(a) == 8 + @test count(a) == 8 a[:,1] = 0 @test nnz(a) == 19 - @test countnz(a) == 8 + @test count(a) == 8 a[:,2] = 0 @test nnz(a) == 19 - @test countnz(a) == 0 + @test count(a) == 0 a = copy(b) a[:,:] = 0 @test nnz(a) == 19 - @test countnz(a) == 0 + @test count(a) == 0 # Zero-assignment behavior of setindex!(A, B::SparseMatrixCSC, I, J) a = copy(b) a[1:2,:] = spzeros(2, 10) @test nnz(a) == 19 - @test countnz(a) == 8 + @test count(a) == 8 a[1:2,1:3] = sparse([1 0 1; 0 0 1]) @test nnz(a) == 20 - @test countnz(a) == 11 + @test count(a) == 11 a = copy(b) a[1:2,:] = let c = sparse(ones(2,10)); fill!(c.nzval, 0); c; end @test nnz(a) == 19 - @test countnz(a) == 8 + @test count(a) == 8 a[1:2,1:3] = let c = sparse(ones(2,3)); c[1,2] = c[2,1] = c[2,2] = 0; c; end @test nnz(a) == 20 - @test countnz(a) == 11 + @test count(a) == 11 a[1,:] = 1:10 @test a[1,:] == sparse([1:10;]) @@ -749,34 +749,34 @@ end A = spzeros(Int, 10, 20) A[1:5,1:10] = 10 A[1:5,1:10] = 10 - @test countnz(A) == 50 + @test count(A) == 50 @test A[1:5,1:10] == 10 * ones(Int, 5, 10) A[6:10,11:20] = 0 - @test countnz(A) == 50 + @test count(A) == 50 A[6:10,11:20] = 20 - @test countnz(A) == 100 + @test count(A) == 100 @test A[6:10,11:20] == 20 * ones(Int, 5, 10) A[4:8,8:16] = 15 - @test countnz(A) == 121 + @test count(A) == 121 @test A[4:8,8:16] == 15 * ones(Int, 5, 9) ASZ = 1000 TSZ = 800 A = sprand(ASZ, 2*ASZ, 0.0001) B = copy(A) - nA = countnz(A) + nA = count(A) x = A[1:TSZ, 1:(2*TSZ)] - nx = countnz(x) + nx = count(x) A[1:TSZ, 1:(2*TSZ)] = 0 - nB = countnz(A) + nB = count(A) @test nB == (nA - nx) A[1:TSZ, 1:(2*TSZ)] = x - @test countnz(A) == nA + @test count(A) == nA @test A == B A[1:TSZ, 1:(2*TSZ)] = 10 - @test countnz(A) == nB + 2*TSZ*TSZ + @test count(A) == nB + 2*TSZ*TSZ A[1:TSZ, 1:(2*TSZ)] = x - @test countnz(A) == nA + @test count(A) == nA @test A == B A = speye(Int, 5) @@ -787,17 +787,17 @@ end @test A[I] == A[X] == collect(1:10) A[I] = zeros(Int, 10) @test nnz(A) == 13 - @test countnz(A) == 3 + @test count(A) == 3 @test A[I] == A[X] == zeros(Int, 10) c = collect(11:20); c[1] = c[3] = 0 A[I] = c @test nnz(A) == 13 - @test countnz(A) == 11 + @test count(A) == 11 @test A[I] == A[X] == c A = speye(Int, 5) A[I] = c @test nnz(A) == 12 - @test countnz(A) == 11 + @test count(A) == 11 @test A[I] == A[X] == c S = sprand(50, 30, 0.5, x -> round.(Int, rand(x) * 100)) @@ -812,7 +812,7 @@ end nnzS1 = nnz(S) S[FI] = 0 sumS2 = sum(S) - cnzS2 = countnz(S) + cnzS2 = count(S) @test sum(S[FI]) == 0 @test nnz(S) == nnzS1 @test (sum(S) + sumFI) == sumS1 @@ -823,7 +823,7 @@ end S[FI] = 0 @test sum(S) == sumS2 @test nnz(S) == nnzS3 - @test countnz(S) == cnzS2 + @test count(S) == cnzS2 S[FI] = [1:sum(FI);] @test sum(S) == sumS2 + sum(1:sum(FI)) @@ -1114,11 +1114,11 @@ end sm = sparse(D) sv = sparsevec(D) - @test countnz(sm) == 10 - @test countnz(sv) == 10 + @test count(sm) == 10 + @test count(sv) == 10 - @test countnz(sparse(Diagonal(Int[]))) == 0 - @test countnz(sparsevec(Diagonal(Int[]))) == 0 + @test count(sparse(Diagonal(Int[]))) == 0 + @test count(sparsevec(Diagonal(Int[]))) == 0 end @testset "explicit zeros" begin diff --git a/test/sparse/sparsevector.jl b/test/sparse/sparsevector.jl index 77542edbada1c..1b2c9832b3ac0 100644 --- a/test/sparse/sparsevector.jl +++ b/test/sparse/sparsevector.jl @@ -20,7 +20,7 @@ let x = spv_x1 @test size(x,2) == 1 @test !isempty(x) - @test countnz(x) == 3 + @test count(x) == 3 @test nnz(x) == 3 @test SparseArrays.nonzeroinds(x) == [2, 5, 6] @test nonzeros(x) == [1.25, -0.75, 3.5] diff --git a/test/subarray.jl b/test/subarray.jl index cb10e3f1c4be4..cda29e9fa1250 100644 --- a/test/subarray.jl +++ b/test/subarray.jl @@ -421,7 +421,7 @@ msk = ones(Bool, 2, 2) msk[2,1] = false sA = view(A, :, :, 1) sA[msk] = 1.0 -@test sA[msk] == ones(countnz(msk)) +@test sA[msk] == ones(count(msk)) # bounds checking upon construction; see #4044, #10296 @test_throws BoundsError view(1:10, 8:11)