Skip to content

Commit

Permalink
Make findn and findnz behavior match nnz such that length of returned (
Browse files Browse the repository at this point in the history
…#24724)

vectors is nnz.

Fixes #23121
  • Loading branch information
andreasnoack authored Nov 29, 2017
1 parent 710a3d8 commit 0335175
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 22 deletions.
25 changes: 5 additions & 20 deletions base/sparse/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1282,7 +1282,7 @@ function find(p::Function, S::SparseMatrixCSC)
return sub2ind(sz, I, J)
end

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

function _findn(p::Function, S::SparseMatrixCSC{Tv,Ti}) where {Tv,Ti}
numnz = nnz(S)
Expand All @@ -1298,12 +1298,6 @@ function _findn(p::Function, S::SparseMatrixCSC{Tv,Ti}) where {Tv,Ti}
end
end

count -= 1
if numnz != count
deleteat!(I, (count+1):numnz)
deleteat!(J, (count+1):numnz)
end

return (I, J)
end

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

count = 1
@inbounds for col = 1 : S.n, k = S.colptr[col] : (S.colptr[col+1]-1)
if S.nzval[k] != 0
I[count] = S.rowval[k]
J[count] = col
V[count] = S.nzval[k]
count += 1
end
end

count -= 1
if numnz != count
deleteat!(I, (count+1):numnz)
deleteat!(J, (count+1):numnz)
deleteat!(V, (count+1):numnz)
I[count] = S.rowval[k]
J[count] = col
V[count] = S.nzval[k]
count += 1
end

return (I, J, V)
Expand Down
4 changes: 2 additions & 2 deletions test/sparse/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -526,9 +526,9 @@ end
@test_throws ArgumentError sparsevec([3,5,7],[0.1,0.0,3.2],4)
end

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

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

0 comments on commit 0335175

Please sign in to comment.