Skip to content

Commit a6d1829

Browse files
authored
Merge pull request #25641 from JuliaLang/nl/findnz
Move findnz to SparseArrays module
2 parents e0c5d2d + 7d66c0f commit a6d1829

File tree

14 files changed

+68
-78
lines changed

14 files changed

+68
-78
lines changed

Diff for: base/array.jl

-38
Original file line numberDiff line numberDiff line change
@@ -1994,44 +1994,6 @@ findall(x::Bool) = x ? [1] : Vector{Int}()
19941994
findall(testf::Function, x::Number) = !testf(x) ? Vector{Int}() : [1]
19951995
findall(p::OccursIn, x::Number) = x in p.x ? Vector{Int}() : [1]
19961996

1997-
"""
1998-
findnz(A)
1999-
2000-
Return a tuple `(I, J, V)` where `I` and `J` are the row and column indices of the non-zero
2001-
values in matrix `A`, and `V` is a vector of the non-zero values.
2002-
2003-
# Examples
2004-
```jldoctest
2005-
julia> A = [1 2 0; 0 0 3; 0 4 0]
2006-
3×3 Array{Int64,2}:
2007-
1 2 0
2008-
0 0 3
2009-
0 4 0
2010-
2011-
julia> findnz(A)
2012-
([1, 1, 3, 2], [1, 2, 2, 3], [1, 2, 4, 3])
2013-
```
2014-
"""
2015-
function findnz(A::AbstractMatrix{T}) where T
2016-
nnzA = count(t -> t != 0, A)
2017-
I = zeros(Int, nnzA)
2018-
J = zeros(Int, nnzA)
2019-
NZs = Vector{T}(uninitialized, nnzA)
2020-
cnt = 1
2021-
if nnzA > 0
2022-
for j=axes(A,2), i=axes(A,1)
2023-
Aij = A[i,j]
2024-
if Aij != 0
2025-
I[cnt] = i
2026-
J[cnt] = j
2027-
NZs[cnt] = Aij
2028-
cnt += 1
2029-
end
2030-
end
2031-
end
2032-
return (I, J, NZs)
2033-
end
2034-
20351997
"""
20361998
findmax(itr) -> (x, index)
20371999

Diff for: base/bitarray.jl

-15
Original file line numberDiff line numberDiff line change
@@ -1634,21 +1634,6 @@ end
16341634
# For performance
16351635
findall(::typeof(!iszero), B::BitArray) = findall(B)
16361636

1637-
function findnz(B::BitMatrix)
1638-
nnzB = count(B)
1639-
I = Vector{Int}(uninitialized, nnzB)
1640-
J = Vector{Int}(uninitialized, nnzB)
1641-
cnt = 1
1642-
for j = 1:size(B,2), i = 1:size(B,1)
1643-
if B[i,j]
1644-
I[cnt] = i
1645-
J[cnt] = j
1646-
cnt += 1
1647-
end
1648-
end
1649-
return I, J, trues(length(I))
1650-
end
1651-
16521637
## Reductions ##
16531638

16541639
sum(A::BitArray, region) = reducedim(+, A, region)

Diff for: base/deprecated.jl

+1
Original file line numberDiff line numberDiff line change
@@ -1256,6 +1256,7 @@ end
12561256
@deprecate_moved rowvals "SparseArrays" true true
12571257
@deprecate_moved nzrange "SparseArrays" true true
12581258
@deprecate_moved nnz "SparseArrays" true true
1259+
@deprecate_moved findnz "SparseArrays" true true
12591260
## functions that were exported from Base.SparseArrays but not from Base
12601261
@deprecate_moved droptol! "SparseArrays" false true
12611262
## deprecated functions that are moved to stdlib/SparseArrays/src/deprecated.jl

Diff for: base/exports.jl

-1
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,6 @@ export
471471
findmax!,
472472
findnext,
473473
findprev,
474-
findnz,
475474
occursin,
476475
match,
477476
matchall,

Diff for: doc/src/base/arrays.md

-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@ Base.circshift!
123123
Base.circcopy!
124124
Base.findall(::Any)
125125
Base.findall(::Function, ::Any)
126-
Base.findnz
127126
Base.findfirst(::Any)
128127
Base.findfirst(::Function, ::Any)
129128
Base.findlast(::Any)

Diff for: stdlib/SparseArrays/docs/src/index.md

+1
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ SparseArrays.sparse
204204
SparseArrays.sparsevec
205205
SparseArrays.issparse
206206
SparseArrays.nnz
207+
SparseArrays.findnz
207208
SparseArrays.spzeros
208209
SparseArrays.spdiagm
209210
SparseArrays.blkdiag

Diff for: stdlib/SparseArrays/src/SparseArrays.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import Base: @get!, acos, acosd, acot, acotd, acsch, asech, asin, asind, asinh,
2424
sin, sinc, sind, sinh, sinpi, squeeze, start, sum, summary, tan,
2525
tand, tanh, trunc, abs, abs2,
2626
broadcast, ceil, complex, conj, convert, copy, copyto!, adjoint,
27-
exp, expm1, findall, findmax, findmin, findnz, float, getindex,
27+
exp, expm1, findall, findmax, findmin, float, getindex,
2828
vcat, hcat, hvcat, cat, imag, indmax, kron, length, log, log1p, max, min,
2929
maximum, minimum, one, promote_eltype, real, reshape, rot180,
3030
rotl90, rotr90, round, setindex!, similar, size, transpose,
@@ -35,7 +35,7 @@ using Random: GLOBAL_RNG, AbstractRNG, randsubseq, randsubseq!
3535
export AbstractSparseArray, AbstractSparseMatrix, AbstractSparseVector,
3636
SparseMatrixCSC, SparseVector, blkdiag, droptol!, dropzeros!, dropzeros,
3737
issparse, nonzeros, nzrange, rowvals, sparse, sparsevec, spdiagm,
38-
sprand, sprandn, spzeros, nnz, permute
38+
sprand, sprandn, spzeros, nnz, permute, findnz
3939

4040
include("abstractsparse.jl")
4141
include("sparsematrix.jl")

Diff for: stdlib/SparseArrays/src/abstractsparse.jl

+53
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,56 @@ function findprev(f::typeof(!iszero), v::AbstractSparseArray, i::Integer)
6363
end
6464
return j
6565
end
66+
67+
"""
68+
findnz(A)
69+
70+
Return a tuple `(I, J, V)` where `I` and `J` are the row and column indices of the non-zero
71+
values in matrix `A`, and `V` is a vector of the non-zero values.
72+
73+
# Examples
74+
```jldoctest
75+
julia> A = [1 2 0; 0 0 3; 0 4 0]
76+
3×3 Array{Int64,2}:
77+
1 2 0
78+
0 0 3
79+
0 4 0
80+
81+
julia> findnz(A)
82+
([1, 1, 3, 2], [1, 2, 2, 3], [1, 2, 4, 3])
83+
```
84+
"""
85+
function findnz(A::AbstractMatrix{T}) where T
86+
nnzA = count(t -> t != 0, A)
87+
I = zeros(Int, nnzA)
88+
J = zeros(Int, nnzA)
89+
NZs = Vector{T}(uninitialized, nnzA)
90+
cnt = 1
91+
if nnzA > 0
92+
for j=axes(A,2), i=axes(A,1)
93+
Aij = A[i,j]
94+
if Aij != 0
95+
I[cnt] = i
96+
J[cnt] = j
97+
NZs[cnt] = Aij
98+
cnt += 1
99+
end
100+
end
101+
end
102+
return (I, J, NZs)
103+
end
104+
105+
function findnz(B::BitMatrix)
106+
nnzB = count(B)
107+
I = Vector{Int}(uninitialized, nnzB)
108+
J = Vector{Int}(uninitialized, nnzB)
109+
cnt = 1
110+
for j = 1:size(B,2), i = 1:size(B,1)
111+
if B[i,j]
112+
I[cnt] = i
113+
J[cnt] = j
114+
cnt += 1
115+
end
116+
end
117+
return I, J, trues(length(I))
118+
end

Diff for: stdlib/SparseArrays/src/sparsevector.jl

+3-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
### Common definitions
44

5-
import Base: sort, findall, findnz
5+
import Base: sort, findall
66
import LinearAlgebra: promote_to_array_type, promote_to_arrays_
77

88
### The SparseVector
@@ -716,19 +716,9 @@ function findnz(x::SparseVector{Tv,Ti}) where {Tv,Ti}
716716
nzind = x.nzind
717717
nzval = x.nzval
718718

719-
count = 1
720719
@inbounds for i = 1 : numnz
721-
if nzval[i] != 0
722-
I[count] = nzind[i]
723-
V[count] = nzval[i]
724-
count += 1
725-
end
726-
end
727-
728-
count -= 1
729-
if numnz != count
730-
deleteat!(I, (count+1):numnz)
731-
deleteat!(V, (count+1):numnz)
720+
I[i] = nzind[i]
721+
V[i] = nzval[i]
732722
end
733723

734724
return (I, V)

Diff for: stdlib/SparseArrays/test/sparse.jl

+5
Original file line numberDiff line numberDiff line change
@@ -2204,3 +2204,8 @@ end
22042204
v[1] = 2
22052205
@test A[1,1] == 2
22062206
end
2207+
2208+
@testset "findnz on non-sparse arrays" begin
2209+
@test findnz([0 1; 0 2]) == ([1, 2], [2, 2], [1, 2])
2210+
@test findnz(BitArray([false true; false true])) == ([1, 2], [2, 2], trues(2))
2211+
end

Diff for: stdlib/SparseArrays/test/sparsevector.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ end
277277
@test findnz(spv_x1) == (findall(!iszero, x1_full), filter(x->x!=0, x1_full))
278278
let xc = SparseVector(8, [2, 3, 5], [1.25, 0, -0.75]), fc = Array(xc)
279279
@test findall(!iszero, xc) == findall(!iszero, fc)
280-
@test findnz(xc) == ([2, 5], [1.25, -0.75])
280+
@test findnz(xc) == ([2, 3, 5], [1.25, 0, -0.75])
281281
end
282282
end
283283
### Array manipulation

Diff for: stdlib/SuiteSparse/src/umfpack.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module UMFPACK
44

55
export UmfpackLU
66

7-
import Base: (\), findnz, getproperty, show, size
7+
import Base: (\), getproperty, show, size
88
using LinearAlgebra
99
import LinearAlgebra: Factorization, det, lufact, ldiv!
1010

Diff for: test/bitarray.jl

+1-2
Original file line numberDiff line numberDiff line change
@@ -1108,10 +1108,9 @@ timesofar("datamove")
11081108
b1 = bitrand(n1, n2)
11091109
@check_bit_operation findall(b1) Vector{CartesianIndex{2}}
11101110
@check_bit_operation findall(!iszero, b1) Vector{CartesianIndex{2}}
1111-
@check_bit_operation findnz(b1) Tuple{Vector{Int}, Vector{Int}, BitArray}
11121111
end
11131112

1114-
timesofar("nnz&find")
1113+
timesofar("find")
11151114

11161115
@testset "Findnext/findprev" begin
11171116
b1 = trues(v1)

Diff for: test/offsetarray.jl

-4
Original file line numberDiff line numberDiff line change
@@ -363,10 +363,6 @@ pmax, ipmax = findmax(parent(A))
363363
z = OffsetArray([0 0; 2 0; 0 0; 0 0], (-3,-1))
364364
I = findall(!iszero, z)
365365
@test I == [CartesianIndex(-1, 0)]
366-
I,J,N = findnz(z)
367-
@test I == [-1]
368-
@test J == [0]
369-
@test N == [2]
370366
@test findall(!iszero,h) == [-2:1;]
371367
@test findall(x->x>0, h) == [-1,1]
372368
@test findall(x->x<0, h) == [-2,0]

0 commit comments

Comments
 (0)