Skip to content

Commit

Permalink
Fix findall() on sparse matrices when some stored entries are false (J…
Browse files Browse the repository at this point in the history
…uliaLang#26182)

The number of returned indices needs to be adjusted in that case.
  • Loading branch information
nalimilan authored Mar 15, 2018
1 parent f355935 commit c6b2afc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
6 changes: 4 additions & 2 deletions stdlib/SparseArrays/src/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1306,14 +1306,16 @@ function findall(p::Function, S::SparseMatrixCSC)
numnz = nnz(S)
inds = Vector{CartesianIndex{2}}(undef, numnz)

count = 1
count = 0
@inbounds for col = 1 : S.n, k = S.colptr[col] : (S.colptr[col+1]-1)
if p(S.nzval[k])
inds[count] = CartesianIndex(S.rowval[k], col)
count += 1
inds[count] = CartesianIndex(S.rowval[k], col)
end
end

resize!(inds, count)

return inds
end
findall(p::Base.OccursIn, x::SparseMatrixCSC) =
Expand Down
9 changes: 8 additions & 1 deletion stdlib/SparseArrays/test/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -539,9 +539,16 @@ end
@test length(K) == length(J) == length(V) == 2
end

@testset "issue described in https://groups.google.com/d/msg/julia-users/Yq4dh8NOWBQ/GU57L90FZ3EJ" begin
@testset "findall" begin
# issue described in https://groups.google.com/d/msg/julia-users/Yq4dh8NOWBQ/GU57L90FZ3EJ
A = sparse(I, 5, 5)
@test findall(A) == findall(x -> x == true, A) == findall(Array(A))
# Non-stored entries are true
@test findall(x -> x == false, A) == findall(x -> x == false, Array(A))

# Not all stored entries are true
@test findall(sparse([true false])) == [CartesianIndex(1, 1)]
@test findall(x -> x > 1, sparse([1 2])) == [CartesianIndex(1, 2)]
end

@testset "issue #5824" begin
Expand Down

0 comments on commit c6b2afc

Please sign in to comment.