Skip to content

Commit 0335175

Browse files
authored
Make findn and findnz behavior match nnz such that length of returned (#24724)
vectors is nnz. Fixes #23121
1 parent 710a3d8 commit 0335175

File tree

2 files changed

+7
-22
lines changed

2 files changed

+7
-22
lines changed

base/sparse/sparsematrix.jl

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,7 +1282,7 @@ function find(p::Function, S::SparseMatrixCSC)
12821282
return sub2ind(sz, I, J)
12831283
end
12841284

1285-
findn(S::SparseMatrixCSC{Tv,Ti}) where {Tv,Ti} = _findn(x->x!=0, S)
1285+
findn(S::SparseMatrixCSC{Tv,Ti}) where {Tv,Ti} = _findn(x->true, S)
12861286

12871287
function _findn(p::Function, S::SparseMatrixCSC{Tv,Ti}) where {Tv,Ti}
12881288
numnz = nnz(S)
@@ -1298,12 +1298,6 @@ function _findn(p::Function, S::SparseMatrixCSC{Tv,Ti}) where {Tv,Ti}
12981298
end
12991299
end
13001300

1301-
count -= 1
1302-
if numnz != count
1303-
deleteat!(I, (count+1):numnz)
1304-
deleteat!(J, (count+1):numnz)
1305-
end
1306-
13071301
return (I, J)
13081302
end
13091303

@@ -1315,19 +1309,10 @@ function findnz(S::SparseMatrixCSC{Tv,Ti}) where {Tv,Ti}
13151309

13161310
count = 1
13171311
@inbounds for col = 1 : S.n, k = S.colptr[col] : (S.colptr[col+1]-1)
1318-
if S.nzval[k] != 0
1319-
I[count] = S.rowval[k]
1320-
J[count] = col
1321-
V[count] = S.nzval[k]
1322-
count += 1
1323-
end
1324-
end
1325-
1326-
count -= 1
1327-
if numnz != count
1328-
deleteat!(I, (count+1):numnz)
1329-
deleteat!(J, (count+1):numnz)
1330-
deleteat!(V, (count+1):numnz)
1312+
I[count] = S.rowval[k]
1313+
J[count] = col
1314+
V[count] = S.nzval[k]
1315+
count += 1
13311316
end
13321317

13331318
return (I, J, V)

test/sparse/sparse.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,9 +526,9 @@ end
526526
@test_throws ArgumentError sparsevec([3,5,7],[0.1,0.0,3.2],4)
527527
end
528528

529-
@testset "issue #5386" begin
529+
@testset "what used to be issue #5386" begin
530530
K,J,V = findnz(SparseMatrixCSC(2,1,[1,3],[1,2],[1.0,0.0]))
531-
@test length(K) == length(J) == length(V) == 1
531+
@test length(K) == length(J) == length(V) == 2
532532
end
533533

534534
@testset "issue described in https://groups.google.com/d/msg/julia-users/Yq4dh8NOWBQ/GU57L90FZ3EJ" begin

0 commit comments

Comments
 (0)