Skip to content
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

allow explicit stored zeros in SparseMatrixCSC #5538

Merged
merged 1 commit into from
Feb 4, 2014
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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ Deprecated or removed
* `myindexes` has been renamed to `localindexes` ([#5475])

* `factorize!` is deprecated in favor of `factorize`. ([#5526])

* `nnz` is removed. Use `countnz` or `nfilled` instead ([#5538])

[#4042]: https://github.com/JuliaLang/julia/issues/4042
[#5164]: https://github.com/JuliaLang/julia/issues/5164
Expand Down Expand Up @@ -230,6 +232,7 @@ Deprecated or removed
[#4888]: https://github.com/JuliaLang/julia/pull/4888
[#5475]: https://github.com/JuliaLang/julia/pull/5475
[#5526]: https://github.com/JuliaLang/julia/pull/5526
[#5538]: https://github.com/JuliaLang/julia/pull/5538

Julia v0.2.0 Release Notes
==========================
Expand Down
1 change: 1 addition & 0 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ 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(a)
length(t::AbstractArray) = prod(size(t))::Int
endof(a::AbstractArray) = length(a)
first(a::AbstractArray) = a[1]
Expand Down
8 changes: 4 additions & 4 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,7 @@ function find(testf::Function, A::AbstractArray)
end

function find(A::AbstractArray)
nnzA = nnz(A)
nnzA = countnz(A)
I = Array(Int, nnzA)
count = 1
for i=1:length(A)
Expand All @@ -1074,7 +1074,7 @@ find(testf::Function, x) = find(testf(x))
findn(A::AbstractVector) = find(A)

function findn(A::AbstractMatrix)
nnzA = nnz(A)
nnzA = countnz(A)
I = Array(Int, nnzA)
J = Array(Int, nnzA)
count = 1
Expand All @@ -1089,7 +1089,7 @@ function findn(A::AbstractMatrix)
end

function findnz{T}(A::AbstractMatrix{T})
nnzA = nnz(A)
nnzA = countnz(A)
I = zeros(Int, nnzA)
J = zeros(Int, nnzA)
NZs = zeros(T, nnzA)
Expand All @@ -1109,7 +1109,7 @@ function findnz{T}(A::AbstractMatrix{T})
end

function nonzeros{T}(A::AbstractArray{T})
nnzA = nnz(A)
nnzA = countnz(A)
V = Array(T, nnzA)
count = 1
if nnzA > 0
Expand Down
12 changes: 6 additions & 6 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1599,9 +1599,9 @@ end

#TODO: rol!, ror!

## nnz & find ##
## countnz & find ##

function nnz(B::BitArray)
function countnz(B::BitArray)
n = 0
Bc = B.chunks
@inbounds for i = 1:length(Bc)
Expand Down Expand Up @@ -1705,7 +1705,7 @@ end

function find(B::BitArray)
l = length(B)
nnzB = nnz(B)
nnzB = countnz(B)
I = Array(Int, nnzB)
if nnzB == 0
return I
Expand Down Expand Up @@ -1741,7 +1741,7 @@ end
findn(B::BitVector) = find(B)

function findn(B::BitMatrix)
nnzB = nnz(B)
nnzB = countnz(B)
I = Array(Int, nnzB)
J = Array(Int, nnzB)
count = 1
Expand All @@ -1760,13 +1760,13 @@ function findnz(B::BitMatrix)
return (I, J, trues(length(I)))
end

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

## Reductions ##

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

sum(B::BitArray) = nnz(B)
sum(B::BitArray) = countnz(B)

function all(B::BitArray)
length(B) == 0 && 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 @@ -390,3 +390,10 @@ eval(Sys, :(@deprecate shlib_list dllist))

# 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


3 changes: 2 additions & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -528,10 +528,11 @@ export
minmax,
nans,
ndims,
nnz,
nfilled,
nonzeros,
nthperm!,
nthperm,
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 @@ -137,7 +137,7 @@ end

## Structure query functions

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

function nonzero_chunks(chunks::Vector{Uint64}, pos0::Int, pos1::Int)
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, nnz, show, size, sort!, transpose, vcat
isvalid, nfilled, 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 nnz{Tv<:CHMVTypes}(A::CholmodSparse{Tv,$Ti})
function nfilled{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 @@ -328,7 +328,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 * nnz(S) # upper bound; will shrink later
numnz = 2 * nfilled(S) # upper bound; will shrink later
rowval = Array(Ti, numnz)
nzval = Array(Tv, numnz)
numnz = 0
Expand Down Expand Up @@ -368,7 +368,7 @@ function sparse_diff2{Tv,Ti}(a::SparseMatrixCSC{Tv,Ti})

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

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

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

numnz = numnzA * numnzB

Expand Down Expand Up @@ -529,7 +529,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 = nnz(A)
numnz = nfilled(A)
C.colptr = convert(Array{Ti}, A.colptr)
C.rowval = convert(Array{Ti}, A.rowval)
C.nzval = Array(Tv, numnz)
Expand All @@ -542,7 +542,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 = nnz(A)
numnz = nfilled(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/linalg/umfpack.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export UmfpackLU,
increment,
increment!

import Base: (\), Ac_ldiv_B, At_ldiv_B, findnz, getindex, nnz, show, size
import Base: (\), Ac_ldiv_B, At_ldiv_B, findnz, getindex, show, size

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

Expand Down
4 changes: 2 additions & 2 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ end


@ngenerate N function findn{T,N}(A::AbstractArray{T,N})
nnzA = nnz(A)
nnzA = countnz(A)
@nexprs N d->(I_d = Array(Int, nnzA))
k = 1
@nloops N i A begin
Expand Down Expand Up @@ -306,7 +306,7 @@ end
end

@ngenerate N function findn{N}(B::BitArray{N})
nnzB = nnz(B)
nnzB = countnz(B)
I = ntuple(N, x->Array(Int, nnzB))
if nnzB > 0
count = 1
Expand Down
8 changes: 4 additions & 4 deletions base/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ function contains(eq::Function, itr, x)
return false
end

## nnz & count
## countnz & count

function nnz{T}(a::AbstractArray{T})
function countnz{T}(a::AbstractArray{T})
n = 0
z = zero(T)
for i = 1:length(a)
Expand All @@ -137,7 +137,7 @@ function nnz{T}(a::AbstractArray{T})
return n
end

function nnz(a::AbstractArray{Bool})
function countnz(a::AbstractArray{Bool})
n = 0
for x in a
if x; n += 1; end
Expand Down Expand Up @@ -170,7 +170,7 @@ function sum(itr)
return v
end

sum(A::AbstractArray{Bool}) = nnz(A)
sum(A::AbstractArray{Bool}) = countnz(A)

# a fast implementation of sum in sequential order (from left to right)
function sum_seq{T}(a::AbstractArray{T}, ifirst::Int, ilast::Int)
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 = nnz(S)
nnzS = nfilled(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 = nnz(S)
nnzS = nfilled(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:nnz(S)
@inbounds for i=1:nfilled(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 = nnz(S)
nnzS = nfilled(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 = nnz(S)
nnzS = nfilled(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:nnz(S)
@inbounds for i=1:nfilled(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 = nnz(A)
nzorig = nfilled(A)
nz = 1
for j = 1:A.n
p = A.colptr[j] # record current position
Expand Down
Loading