Skip to content

Commit 96ba17c

Browse files
committed
Reintroduce nnz and nonzeros as discussed in #6769
Deprecate nfilled Deprecate nonzeros for StridedArray and BitArray find()/findnz() over sparse matrices no longer filters out stored zeros Update documentation for structural nonzeros.
1 parent 3bc8ef2 commit 96ba17c

15 files changed

+81
-102
lines changed

base/abstractarray.jl

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ isreal{T<:Real,n}(x::AbstractArray{T,n}) = true
1919
ndims{T,n}(::AbstractArray{T,n}) = n
2020
ndims{T,n}(::Type{AbstractArray{T,n}}) = n
2121
ndims{T<:AbstractArray}(::Type{T}) = ndims(super(T))
22-
nfilled(t::AbstractArray) = length(t)
2322
length(t::AbstractArray) = prod(size(t))::Int
2423
endof(a::AbstractArray) = length(a)
2524
first(a::AbstractArray) = a[1]

base/array.jl

-18
Original file line numberDiff line numberDiff line change
@@ -1102,24 +1102,6 @@ function findnz{T}(A::StridedMatrix{T})
11021102
return (I, J, NZs)
11031103
end
11041104

1105-
function nonzeros{T}(A::StridedArray{T})
1106-
nnzA = countnz(A)
1107-
V = similar(A, T, nnzA)
1108-
count = 1
1109-
if nnzA > 0
1110-
for i=1:length(A)
1111-
Ai = A[i]
1112-
if Ai != 0
1113-
V[count] = Ai
1114-
count += 1
1115-
end
1116-
end
1117-
end
1118-
return V
1119-
end
1120-
1121-
nonzeros(x::Number) = x == 0 ? Array(typeof(x),0) : [x]
1122-
11231105
function findmax(a)
11241106
if isempty(a)
11251107
error("array must be non-empty")

base/bitarray.jl

-2
Original file line numberDiff line numberDiff line change
@@ -1385,8 +1385,6 @@ function findnz(B::BitMatrix)
13851385
return I, J, trues(length(I))
13861386
end
13871387

1388-
nonzeros(B::BitArray) = trues(countnz(B))
1389-
13901388
## Reductions ##
13911389

13921390
sum(A::BitArray, region) = reducedim(+, A, region, 0, Array(Int,reduced_dims(A,region)))

base/deprecated.jl

+10-6
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,16 @@ end
380380
export mmread
381381

382382
# 0.3 deprecations
383+
384+
function nfilled(X)
385+
depwarn("nfilled has been renamed to nnz", :nfilled)
386+
nnz(X)
387+
end
388+
export nfilled
389+
390+
@deprecate nonzeros(A::StridedArray) A[find(A)]
391+
@deprecate nonzeros(B::BitArray) trues(countnz(B))
392+
383393
@deprecate dense full
384394

385395
export Stat
@@ -448,12 +458,6 @@ Set{T<:Number}(xs::T...) = Set{T}(xs)
448458

449459
# 0.3 discontinued functions
450460

451-
function nnz(X)
452-
depwarn("nnz has been renamed to countnz and is no longer computed in constant time for sparse matrices. Instead, use nfilled() for the number of elements in a sparse matrix.", :nnz)
453-
countnz(X)
454-
end
455-
export nnz
456-
457461
scale!{T<:Base.LinAlg.BlasReal}(X::Array{T}, s::Complex) = error("scale!: Cannot scale a real array by a complex value in-place. Use scale(X::Array{Real}, s::Complex) instead.")
458462

459463
@deprecate which(f::Callable, args...) @which f(args...)

base/exports.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ export
536536
minimum,
537537
minmax,
538538
ndims,
539-
nfilled,
539+
nnz,
540540
nonzeros,
541541
nthperm!,
542542
nthperm,

base/linalg/cholmod.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export # types
1414
using Base.LinAlg.UMFPACK # for decrement, increment, etc.
1515

1616
import Base: (*), convert, copy, ctranspose, eltype, findnz, getindex, hcat,
17-
isvalid, nfilled, show, size, sort!, transpose, vcat
17+
isvalid, nnz, show, size, sort!, transpose, vcat
1818

1919
import ..LinAlg: (\), A_mul_Bc, A_mul_Bt, Ac_ldiv_B, Ac_mul_B, At_ldiv_B, At_mul_B,
2020
cholfact, cholfact!, copy, det, diag,
@@ -778,7 +778,7 @@ for Ti in (:Int32,:Int64)
778778
(Ptr{c_CholmodSparse{Tv,$Ti}},Ptr{c_CholmodSparse{Tv,$Ti}},Cint,Ptr{Uint8}),
779779
&A.c,&B.c,true,cmn($Ti))
780780
end
781-
function nfilled{Tv<:CHMVTypes}(A::CholmodSparse{Tv,$Ti})
781+
function nnz{Tv<:CHMVTypes}(A::CholmodSparse{Tv,$Ti})
782782
ccall((@chm_nm "nnz" $Ti
783783
,:libcholmod), Int, (Ptr{c_CholmodSparse{Tv,$Ti}},Ptr{Uint8}),&A.c,cmn($Ti))
784784
end

base/linalg/sparse.jl

+6-6
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ function sparse_diff1{Tv,Ti}(S::SparseMatrixCSC{Tv,Ti})
347347
m,n = size(S)
348348
m > 1 || return SparseMatrixCSC{Tv,Ti}(0, n, ones(n+1), Ti[], Tv[])
349349
colptr = Array(Ti, n+1)
350-
numnz = 2 * nfilled(S) # upper bound; will shrink later
350+
numnz = 2 * nnz(S) # upper bound; will shrink later
351351
rowval = Array(Ti, numnz)
352352
nzval = Array(Tv, numnz)
353353
numnz = 0
@@ -387,7 +387,7 @@ function sparse_diff2{Tv,Ti}(a::SparseMatrixCSC{Tv,Ti})
387387

388388
m,n = size(a)
389389
colptr = Array(Ti, max(n,1))
390-
numnz = 2 * nfilled(a) # upper bound; will shrink later
390+
numnz = 2 * nnz(a) # upper bound; will shrink later
391391
rowval = Array(Ti, numnz)
392392
nzval = Array(Tv, numnz)
393393

@@ -516,8 +516,8 @@ end
516516
# kron
517517

518518
function kron{Tv,Ti}(a::SparseMatrixCSC{Tv,Ti}, b::SparseMatrixCSC{Tv,Ti})
519-
numnzA = nfilled(a)
520-
numnzB = nfilled(b)
519+
numnzA = nnz(a)
520+
numnzB = nnz(b)
521521

522522
numnz = numnzA * numnzB
523523

@@ -593,7 +593,7 @@ inv(A::SparseMatrixCSC) = error("The inverse of a sparse matrix can often be den
593593
function scale!{Tv,Ti}(C::SparseMatrixCSC{Tv,Ti}, A::SparseMatrixCSC, b::Vector)
594594
m, n = size(A)
595595
(n==length(b) && size(A)==size(C)) || throw(DimensionMismatch(""))
596-
numnz = nfilled(A)
596+
numnz = nnz(A)
597597
C.colptr = convert(Array{Ti}, A.colptr)
598598
C.rowval = convert(Array{Ti}, A.rowval)
599599
C.nzval = Array(Tv, numnz)
@@ -606,7 +606,7 @@ end
606606
function scale!{Tv,Ti}(C::SparseMatrixCSC{Tv,Ti}, b::Vector, A::SparseMatrixCSC)
607607
m, n = size(A)
608608
(n==length(b) && size(A)==size(C)) || throw(DimensionMismatch(""))
609-
numnz = nfilled(A)
609+
numnz = nnz(A)
610610
C.colptr = convert(Array{Ti}, A.colptr)
611611
C.rowval = convert(Array{Ti}, A.rowval)
612612
C.nzval = Array(Tv, numnz)

base/sparse.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Base.NonTupleType, Base.float
77

88
export SparseMatrixCSC,
99
blkdiag, dense, diag, diagm, droptol!, dropzeros!, etree, full,
10-
getindex, ishermitian, issparse, issym, istril, istriu,
10+
getindex, ishermitian, issparse, issym, istril, istriu, nnz,
1111
setindex!, sparse, sparsevec, spdiagm, speye, spones,
1212
sprand, sprandbool, sprandn, spzeros, symperm, trace, tril, tril!,
1313
triu, triu!

base/sparse/csparse.jl

+7-7
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ function transpose!{Tv,Ti}(S::SparseMatrixCSC{Tv,Ti}, T::SparseMatrixCSC{Tv,Ti})
128128
rowval_T = T.rowval
129129
nzval_T = T.nzval
130130

131-
nnzS = nfilled(S)
131+
nnzS = nnz(S)
132132
colptr_S = S.colptr
133133
rowval_S = S.rowval
134134
nzval_S = S.nzval
@@ -147,15 +147,15 @@ end
147147

148148
function transpose{Tv,Ti}(S::SparseMatrixCSC{Tv,Ti})
149149
(nT, mT) = size(S)
150-
nnzS = nfilled(S)
150+
nnzS = nnz(S)
151151
rowval_S = S.rowval
152152

153153
rowval_T = Array(Ti, nnzS)
154154
nzval_T = Array(Tv, nnzS)
155155

156156
colptr_T = zeros(Ti, nT+1)
157157
colptr_T[1] = 1
158-
@inbounds for i=1:nfilled(S)
158+
@inbounds for i=1:nnz(S)
159159
colptr_T[rowval_S[i]+1] += 1
160160
end
161161
colptr_T = cumsum(colptr_T)
@@ -172,7 +172,7 @@ function ctranspose!{Tv,Ti}(S::SparseMatrixCSC{Tv,Ti}, T::SparseMatrixCSC{Tv,Ti}
172172
rowval_T = T.rowval
173173
nzval_T = T.nzval
174174

175-
nnzS = nfilled(S)
175+
nnzS = nnz(S)
176176
colptr_S = S.colptr
177177
rowval_S = S.rowval
178178
nzval_S = S.nzval
@@ -191,15 +191,15 @@ end
191191

192192
function ctranspose{Tv,Ti}(S::SparseMatrixCSC{Tv,Ti})
193193
(nT, mT) = size(S)
194-
nnzS = nfilled(S)
194+
nnzS = nnz(S)
195195
rowval_S = S.rowval
196196

197197
rowval_T = Array(Ti, nnzS)
198198
nzval_T = Array(Tv, nnzS)
199199

200200
colptr_T = zeros(Ti, nT+1)
201201
colptr_T[1] = 1
202-
@inbounds for i=1:nfilled(S)
202+
@inbounds for i=1:nnz(S)
203203
colptr_T[rowval_S[i]+1] += 1
204204
end
205205
colptr_T = cumsum(colptr_T)
@@ -335,7 +335,7 @@ end
335335
# Section 2.7: Removing entries from a matrix
336336
# http://www.cise.ufl.edu/research/sparse/CSparse/
337337
function fkeep!{Tv,Ti}(A::SparseMatrixCSC{Tv,Ti}, f, other)
338-
nzorig = nfilled(A)
338+
nzorig = nnz(A)
339339
nz = 1
340340
for j = 1:A.n
341341
p = A.colptr[j] # record current position

0 commit comments

Comments
 (0)