Skip to content
This repository was archived by the owner on Dec 16, 2022. It is now read-only.

Commit c93ed16

Browse files
committed
allow explicit stored zeros in SparseMatrixCSC. closes #5424
1 parent 6e5ac65 commit c93ed16

20 files changed

+96
-72
lines changed

NEWS.md

+3
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ Deprecated or removed
185185
* `myindexes` has been renamed to `localindexes` ([#5475])
186186

187187
* `factorize!` is deprecated in favor of `factorize`. ([#5526])
188+
189+
* `nnz` is removed. Use `countnz` or `nfilled` instead ([#5538])
188190

189191
[#4042]: https://github.com/JuliaLang/julia/issues/4042
190192
[#5164]: https://github.com/JuliaLang/julia/issues/5164
@@ -230,6 +232,7 @@ Deprecated or removed
230232
[#4888]: https://github.com/JuliaLang/julia/pull/4888
231233
[#5475]: https://github.com/JuliaLang/julia/pull/5475
232234
[#5526]: https://github.com/JuliaLang/julia/pull/5526
235+
[#5538]: https://github.com/JuliaLang/julia/pull/5538
233236

234237
Julia v0.2.0 Release Notes
235238
==========================

base/abstractarray.jl

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ 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(a)
2223
length(t::AbstractArray) = prod(size(t))::Int
2324
endof(a::AbstractArray) = length(a)
2425
first(a::AbstractArray) = a[1]

base/array.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,7 @@ function find(testf::Function, A::AbstractArray)
10561056
end
10571057

10581058
function find(A::AbstractArray)
1059-
nnzA = nnz(A)
1059+
nnzA = countnz(A)
10601060
I = Array(Int, nnzA)
10611061
count = 1
10621062
for i=1:length(A)
@@ -1074,7 +1074,7 @@ find(testf::Function, x) = find(testf(x))
10741074
findn(A::AbstractVector) = find(A)
10751075

10761076
function findn(A::AbstractMatrix)
1077-
nnzA = nnz(A)
1077+
nnzA = countnz(A)
10781078
I = Array(Int, nnzA)
10791079
J = Array(Int, nnzA)
10801080
count = 1
@@ -1089,7 +1089,7 @@ function findn(A::AbstractMatrix)
10891089
end
10901090

10911091
function findnz{T}(A::AbstractMatrix{T})
1092-
nnzA = nnz(A)
1092+
nnzA = countnz(A)
10931093
I = zeros(Int, nnzA)
10941094
J = zeros(Int, nnzA)
10951095
NZs = zeros(T, nnzA)
@@ -1109,7 +1109,7 @@ function findnz{T}(A::AbstractMatrix{T})
11091109
end
11101110

11111111
function nonzeros{T}(A::AbstractArray{T})
1112-
nnzA = nnz(A)
1112+
nnzA = countnz(A)
11131113
V = Array(T, nnzA)
11141114
count = 1
11151115
if nnzA > 0

base/bitarray.jl

+6-6
Original file line numberDiff line numberDiff line change
@@ -1599,9 +1599,9 @@ end
15991599

16001600
#TODO: rol!, ror!
16011601

1602-
## nnz & find ##
1602+
## countnz & find ##
16031603

1604-
function nnz(B::BitArray)
1604+
function countnz(B::BitArray)
16051605
n = 0
16061606
Bc = B.chunks
16071607
@inbounds for i = 1:length(Bc)
@@ -1705,7 +1705,7 @@ end
17051705

17061706
function find(B::BitArray)
17071707
l = length(B)
1708-
nnzB = nnz(B)
1708+
nnzB = countnz(B)
17091709
I = Array(Int, nnzB)
17101710
if nnzB == 0
17111711
return I
@@ -1741,7 +1741,7 @@ end
17411741
findn(B::BitVector) = find(B)
17421742

17431743
function findn(B::BitMatrix)
1744-
nnzB = nnz(B)
1744+
nnzB = countnz(B)
17451745
I = Array(Int, nnzB)
17461746
J = Array(Int, nnzB)
17471747
count = 1
@@ -1760,13 +1760,13 @@ function findnz(B::BitMatrix)
17601760
return (I, J, trues(length(I)))
17611761
end
17621762

1763-
nonzeros(B::BitArray) = trues(nnz(B))
1763+
nonzeros(B::BitArray) = trues(countnz(B))
17641764

17651765
## Reductions ##
17661766

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

1769-
sum(B::BitArray) = nnz(B)
1769+
sum(B::BitArray) = countnz(B)
17701770

17711771
function all(B::BitArray)
17721772
length(B) == 0 && return true

base/deprecated.jl

+7
Original file line numberDiff line numberDiff line change
@@ -390,3 +390,10 @@ eval(Sys, :(@deprecate shlib_list dllist))
390390

391391
# 0.3 discontinued functions
392392

393+
function nnz(X)
394+
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)
395+
countnz(X)
396+
end
397+
export nnz
398+
399+

base/exports.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -528,10 +528,11 @@ export
528528
minmax,
529529
nans,
530530
ndims,
531-
nnz,
531+
nfilled,
532532
nonzeros,
533533
nthperm!,
534534
nthperm,
535+
countnz,
535536
ones,
536537
parent,
537538
parentindexes,

base/linalg/bitarray.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ end
137137

138138
## Structure query functions
139139

140-
issym(A::BitMatrix) = size(A, 1)==size(A, 2) && nnz(A - A.')==0
140+
issym(A::BitMatrix) = size(A, 1)==size(A, 2) && numnz(A - A.')==0
141141
ishermitian(A::BitMatrix) = issym(A)
142142

143143
function nonzero_chunks(chunks::Vector{Uint64}, pos0::Int, pos1::Int)

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, nnz, show, size, sort!, transpose, vcat
17+
isvalid, nfilled, 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 nnz{Tv<:CHMVTypes}(A::CholmodSparse{Tv,$Ti})
781+
function nfilled{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
@@ -328,7 +328,7 @@ function sparse_diff1{Tv,Ti}(S::SparseMatrixCSC{Tv,Ti})
328328
m,n = size(S)
329329
m > 1 && return SparseMatrixCSC{Tv,Ti}(0, n, ones(n+1), Ti[], Tv[])
330330
colptr = Array(Ti, n+1)
331-
numnz = 2 * nnz(S) # upper bound; will shrink later
331+
numnz = 2 * nfilled(S) # upper bound; will shrink later
332332
rowval = Array(Ti, numnz)
333333
nzval = Array(Tv, numnz)
334334
numnz = 0
@@ -368,7 +368,7 @@ function sparse_diff2{Tv,Ti}(a::SparseMatrixCSC{Tv,Ti})
368368

369369
m,n = size(a)
370370
colptr = Array(Ti, max(n,1))
371-
numnz = 2 * nnz(a) # upper bound; will shrink later
371+
numnz = 2 * nfilled(a) # upper bound; will shrink later
372372
rowval = Array(Ti, numnz)
373373
nzval = Array(Tv, numnz)
374374

@@ -463,8 +463,8 @@ diff(a::SparseMatrixCSC, dim::Integer)= dim==1 ? sparse_diff1(a) : sparse_diff2(
463463
# kron
464464

465465
function kron{Tv,Ti}(a::SparseMatrixCSC{Tv,Ti}, b::SparseMatrixCSC{Tv,Ti})
466-
numnzA = nnz(a)
467-
numnzB = nnz(b)
466+
numnzA = nfilled(a)
467+
numnzB = nfilled(b)
468468

469469
numnz = numnzA * numnzB
470470

@@ -529,7 +529,7 @@ inv(A::SparseMatrixCSC) = error("The inverse of a sparse matrix can often be den
529529
function scale!{Tv,Ti}(C::SparseMatrixCSC{Tv,Ti}, A::SparseMatrixCSC, b::Vector)
530530
m, n = size(A)
531531
(n==length(b) && size(A)==size(C)) || throw(DimensionMismatch(""))
532-
numnz = nnz(A)
532+
numnz = nfilled(A)
533533
C.colptr = convert(Array{Ti}, A.colptr)
534534
C.rowval = convert(Array{Ti}, A.rowval)
535535
C.nzval = Array(Tv, numnz)
@@ -542,7 +542,7 @@ end
542542
function scale!{Tv,Ti}(C::SparseMatrixCSC{Tv,Ti}, b::Vector, A::SparseMatrixCSC)
543543
m, n = size(A)
544544
(n==length(b) && size(A)==size(C)) || throw(DimensionMismatch(""))
545-
numnz = nnz(A)
545+
numnz = nfilled(A)
546546
C.colptr = convert(Array{Ti}, A.colptr)
547547
C.rowval = convert(Array{Ti}, A.rowval)
548548
C.nzval = Array(Tv, numnz)

base/linalg/umfpack.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export UmfpackLU,
66
increment,
77
increment!
88

9-
import Base: (\), Ac_ldiv_B, At_ldiv_B, findnz, getindex, nnz, show, size
9+
import Base: (\), Ac_ldiv_B, At_ldiv_B, findnz, getindex, show, size
1010

1111
import ..LinAlg: A_ldiv_B!, Ac_ldiv_B!, At_ldiv_B!, Factorization, det, lufact, lufact!, solve
1212

base/multidimensional.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ end
9595

9696

9797
@ngenerate N function findn{T,N}(A::AbstractArray{T,N})
98-
nnzA = nnz(A)
98+
nnzA = countnz(A)
9999
@nexprs N d->(I_d = Array(Int, nnzA))
100100
k = 1
101101
@nloops N i A begin
@@ -306,7 +306,7 @@ end
306306
end
307307

308308
@ngenerate N function findn{N}(B::BitArray{N})
309-
nnzB = nnz(B)
309+
nnzB = countnz(B)
310310
I = ntuple(N, x->Array(Int, nnzB))
311311
if nnzB > 0
312312
count = 1

base/reduce.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,9 @@ function contains(eq::Function, itr, x)
126126
return false
127127
end
128128

129-
## nnz & count
129+
## countnz & count
130130

131-
function nnz{T}(a::AbstractArray{T})
131+
function countnz{T}(a::AbstractArray{T})
132132
n = 0
133133
z = zero(T)
134134
for i = 1:length(a)
@@ -137,7 +137,7 @@ function nnz{T}(a::AbstractArray{T})
137137
return n
138138
end
139139

140-
function nnz(a::AbstractArray{Bool})
140+
function countnz(a::AbstractArray{Bool})
141141
n = 0
142142
for x in a
143143
if x; n += 1; end
@@ -170,7 +170,7 @@ function sum(itr)
170170
return v
171171
end
172172

173-
sum(A::AbstractArray{Bool}) = nnz(A)
173+
sum(A::AbstractArray{Bool}) = countnz(A)
174174

175175
# a fast implementation of sum in sequential order (from left to right)
176176
function sum_seq{T}(a::AbstractArray{T}, ifirst::Int, ilast::Int)

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 = nnz(S)
131+
nnzS = nfilled(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 = nnz(S)
150+
nnzS = nfilled(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:nnz(S)
158+
@inbounds for i=1:nfilled(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 = nnz(S)
175+
nnzS = nfilled(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 = nnz(S)
194+
nnzS = nfilled(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:nnz(S)
202+
@inbounds for i=1:nfilled(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 = nnz(A)
338+
nzorig = nfilled(A)
339339
nz = 1
340340
for j = 1:A.n
341341
p = A.colptr[j] # record current position

0 commit comments

Comments
 (0)