Skip to content

Commit

Permalink
Reintroduce nnz and nonzeros as discussed in #6769
Browse files Browse the repository at this point in the history
Deprecate nfilled for SparseMatrixCSC.
Deprecate nonzeros for StridedArray and BitArray.
Update documentation for structural nonzeros.
  • Loading branch information
ViralBShah committed May 26, 2014
1 parent 3bc8ef2 commit 4ac5e2b
Show file tree
Hide file tree
Showing 15 changed files with 72 additions and 84 deletions.
1 change: 0 additions & 1 deletion base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ isreal{T<:Real,n}(x::AbstractArray{T,n}) = true
ndims{T,n}(::AbstractArray{T,n}) = n
ndims{T,n}(::Type{AbstractArray{T,n}}) = n
ndims{T<:AbstractArray}(::Type{T}) = ndims(super(T))
nfilled(t::AbstractArray) = length(t)
length(t::AbstractArray) = prod(size(t))::Int
endof(a::AbstractArray) = length(a)
first(a::AbstractArray) = a[1]
Expand Down
18 changes: 0 additions & 18 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1102,24 +1102,6 @@ function findnz{T}(A::StridedMatrix{T})
return (I, J, NZs)
end

function nonzeros{T}(A::StridedArray{T})
nnzA = countnz(A)
V = similar(A, T, nnzA)
count = 1
if nnzA > 0
for i=1:length(A)
Ai = A[i]
if Ai != 0
V[count] = Ai
count += 1
end
end
end
return V
end

nonzeros(x::Number) = x == 0 ? Array(typeof(x),0) : [x]

function findmax(a)
if isempty(a)
error("array must be non-empty")
Expand Down
2 changes: 0 additions & 2 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1385,8 +1385,6 @@ function findnz(B::BitMatrix)
return I, J, trues(length(I))
end

nonzeros(B::BitArray) = trues(countnz(B))

## Reductions ##

sum(A::BitArray, region) = reducedim(+, A, region, 0, Array(Int,reduced_dims(A,region)))
Expand Down
16 changes: 10 additions & 6 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,16 @@ end
export mmread

# 0.3 deprecations

function nfilled(X)
depwarn("nfilled has been renamed to nnz", :nfilled)
nnz(X)
end
export nfilled

@deprecate nonzeros(A::StridedArray) A[find(A)]
@deprecate nonzeros(B::BitArray) trues(countnz(B))

@deprecate dense full

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

# 0.3 discontinued functions

function nnz(X)
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)
countnz(X)
end
export nnz

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.")

@deprecate which(f::Callable, args...) @which f(args...)
2 changes: 1 addition & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ export
minimum,
minmax,
ndims,
nfilled,
nnz,
nonzeros,
nthperm!,
nthperm,
Expand Down
4 changes: 2 additions & 2 deletions base/linalg/cholmod.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export # types
using Base.LinAlg.UMFPACK # for decrement, increment, etc.

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

import ..LinAlg: (\), A_mul_Bc, A_mul_Bt, Ac_ldiv_B, Ac_mul_B, At_ldiv_B, At_mul_B,
cholfact, cholfact!, copy, det, diag,
Expand Down Expand Up @@ -778,7 +778,7 @@ for Ti in (:Int32,:Int64)
(Ptr{c_CholmodSparse{Tv,$Ti}},Ptr{c_CholmodSparse{Tv,$Ti}},Cint,Ptr{Uint8}),
&A.c,&B.c,true,cmn($Ti))
end
function nfilled{Tv<:CHMVTypes}(A::CholmodSparse{Tv,$Ti})
function nnz{Tv<:CHMVTypes}(A::CholmodSparse{Tv,$Ti})
ccall((@chm_nm "nnz" $Ti
,:libcholmod), Int, (Ptr{c_CholmodSparse{Tv,$Ti}},Ptr{Uint8}),&A.c,cmn($Ti))
end
Expand Down
12 changes: 6 additions & 6 deletions base/linalg/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ function sparse_diff1{Tv,Ti}(S::SparseMatrixCSC{Tv,Ti})
m,n = size(S)
m > 1 || return SparseMatrixCSC{Tv,Ti}(0, n, ones(n+1), Ti[], Tv[])
colptr = Array(Ti, n+1)
numnz = 2 * nfilled(S) # upper bound; will shrink later
numnz = 2 * nnz(S) # upper bound; will shrink later
rowval = Array(Ti, numnz)
nzval = Array(Tv, numnz)
numnz = 0
Expand Down Expand Up @@ -387,7 +387,7 @@ function sparse_diff2{Tv,Ti}(a::SparseMatrixCSC{Tv,Ti})

m,n = size(a)
colptr = Array(Ti, max(n,1))
numnz = 2 * nfilled(a) # upper bound; will shrink later
numnz = 2 * nnz(a) # upper bound; will shrink later
rowval = Array(Ti, numnz)
nzval = Array(Tv, numnz)

Expand Down Expand Up @@ -516,8 +516,8 @@ end
# kron

function kron{Tv,Ti}(a::SparseMatrixCSC{Tv,Ti}, b::SparseMatrixCSC{Tv,Ti})
numnzA = nfilled(a)
numnzB = nfilled(b)
numnzA = nnz(a)
numnzB = nnz(b)

numnz = numnzA * numnzB

Expand Down Expand Up @@ -593,7 +593,7 @@ inv(A::SparseMatrixCSC) = error("The inverse of a sparse matrix can often be den
function scale!{Tv,Ti}(C::SparseMatrixCSC{Tv,Ti}, A::SparseMatrixCSC, b::Vector)
m, n = size(A)
(n==length(b) && size(A)==size(C)) || throw(DimensionMismatch(""))
numnz = nfilled(A)
numnz = nnz(A)
C.colptr = convert(Array{Ti}, A.colptr)
C.rowval = convert(Array{Ti}, A.rowval)
C.nzval = Array(Tv, numnz)
Expand All @@ -606,7 +606,7 @@ end
function scale!{Tv,Ti}(C::SparseMatrixCSC{Tv,Ti}, b::Vector, A::SparseMatrixCSC)
m, n = size(A)
(n==length(b) && size(A)==size(C)) || throw(DimensionMismatch(""))
numnz = nfilled(A)
numnz = nnz(A)
C.colptr = convert(Array{Ti}, A.colptr)
C.rowval = convert(Array{Ti}, A.rowval)
C.nzval = Array(Tv, numnz)
Expand Down
2 changes: 1 addition & 1 deletion base/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Base.NonTupleType, Base.float

export SparseMatrixCSC,
blkdiag, dense, diag, diagm, droptol!, dropzeros!, etree, full,
getindex, ishermitian, issparse, issym, istril, istriu,
getindex, ishermitian, issparse, issym, istril, istriu, nnz,
setindex!, sparse, sparsevec, spdiagm, speye, spones,
sprand, sprandbool, sprandn, spzeros, symperm, trace, tril, tril!,
triu, triu!
Expand Down
14 changes: 7 additions & 7 deletions base/sparse/csparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function transpose!{Tv,Ti}(S::SparseMatrixCSC{Tv,Ti}, T::SparseMatrixCSC{Tv,Ti})
rowval_T = T.rowval
nzval_T = T.nzval

nnzS = nfilled(S)
nnzS = nnz(S)
colptr_S = S.colptr
rowval_S = S.rowval
nzval_S = S.nzval
Expand All @@ -147,15 +147,15 @@ end

function transpose{Tv,Ti}(S::SparseMatrixCSC{Tv,Ti})
(nT, mT) = size(S)
nnzS = nfilled(S)
nnzS = nnz(S)
rowval_S = S.rowval

rowval_T = Array(Ti, nnzS)
nzval_T = Array(Tv, nnzS)

colptr_T = zeros(Ti, nT+1)
colptr_T[1] = 1
@inbounds for i=1:nfilled(S)
@inbounds for i=1:nnz(S)
colptr_T[rowval_S[i]+1] += 1
end
colptr_T = cumsum(colptr_T)
Expand All @@ -172,7 +172,7 @@ function ctranspose!{Tv,Ti}(S::SparseMatrixCSC{Tv,Ti}, T::SparseMatrixCSC{Tv,Ti}
rowval_T = T.rowval
nzval_T = T.nzval

nnzS = nfilled(S)
nnzS = nnz(S)
colptr_S = S.colptr
rowval_S = S.rowval
nzval_S = S.nzval
Expand All @@ -191,15 +191,15 @@ end

function ctranspose{Tv,Ti}(S::SparseMatrixCSC{Tv,Ti})
(nT, mT) = size(S)
nnzS = nfilled(S)
nnzS = nnz(S)
rowval_S = S.rowval

rowval_T = Array(Ti, nnzS)
nzval_T = Array(Tv, nnzS)

colptr_T = zeros(Ti, nT+1)
colptr_T[1] = 1
@inbounds for i=1:nfilled(S)
@inbounds for i=1:nnz(S)
colptr_T[rowval_S[i]+1] += 1
end
colptr_T = cumsum(colptr_T)
Expand Down Expand Up @@ -335,7 +335,7 @@ end
# Section 2.7: Removing entries from a matrix
# http://www.cise.ufl.edu/research/sparse/CSparse/
function fkeep!{Tv,Ti}(A::SparseMatrixCSC{Tv,Ti}, f, other)
nzorig = nfilled(A)
nzorig = nnz(A)
nz = 1
for j = 1:A.n
p = A.colptr[j] # record current position
Expand Down
Loading

0 comments on commit 4ac5e2b

Please sign in to comment.