Skip to content

Implement optimizations for sparse findnext/findprev #28313

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

Closed
wants to merge 1 commit into from
Closed
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
36 changes: 22 additions & 14 deletions stdlib/SparseArrays/src/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1347,36 +1347,44 @@ function findnz(S::SparseMatrixCSC{Tv,Ti}) where {Tv,Ti}
return (I, J, V)
end

function _sparse_findnextnz(m::SparseMatrixCSC, i::Integer)
if i > length(m)
function Base.findnext(f::Function, m::SparseMatrixCSC, idx::CartesianIndex{2})
if f(zero(eltype(m)))
return invoke(findnext, Tuple{Function, Any, Any}, f, m, idx)
end
row, col = idx.I
if row > size(m, 1) || col > size(m, 2)
return nothing
end
row, col = Tuple(CartesianIndices(m)[i])
lo, hi = m.colptr[col], m.colptr[col+1]
n = searchsortedfirst(m.rowval, row, lo, hi-1, Base.Order.Forward)
n = findnext(f, m.nzval, n)
n === nothing && return nothing
if lo <= n <= hi-1
return LinearIndices(m)[m.rowval[n], col]
return CartesianIndex(m.rowval[n], col)
end
nextcol = findnext(c->(c>hi), m.colptr, col+1)
nextcol = findnext(Base.Fix2(>, n), m.colptr, col+1)
nextcol === nothing && return nothing
nextlo = m.colptr[nextcol-1]
return LinearIndices(m)[m.rowval[nextlo], nextcol-1]
return CartesianIndex(m.rowval[n], nextcol-1)
end

function _sparse_findprevnz(m::SparseMatrixCSC, i::Integer)
if iszero(i)
function Base.findprev(f::Function, m::SparseMatrixCSC, idx::CartesianIndex{2})
if f(zero(eltype(m)))
return invoke(findprev, Tuple{Function, Any, Any}, f, m, idx)
end
row, col = idx.I
if row < 1 || col < 1
return nothing
end
row, col = Tuple(CartesianIndices(m)[i])
lo, hi = m.colptr[col], m.colptr[col+1]
n = searchsortedlast(m.rowval, row, lo, hi-1, Base.Order.Forward)
n = findprev(f, m.nzval, n)
n === nothing && return nothing
if lo <= n <= hi-1
return LinearIndices(m)[m.rowval[n], col]
return CartesianIndex(m.rowval[n], col)
end
prevcol = findprev(c->(c<lo), m.colptr, col-1)
prevcol = findprev(Base.Fix2(<=, n), m.colptr, col-1)
prevcol === nothing && return nothing
prevhi = m.colptr[prevcol+1]
return LinearIndices(m)[m.rowval[prevhi-1], prevcol]
return CartesianIndex(m.rowval[n], prevcol)
end

function sprand_IJ(r::AbstractRNG, m::Integer, n::Integer, density::AbstractFloat)
Expand Down
17 changes: 17 additions & 0 deletions stdlib/SparseArrays/test/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,23 @@ end
@test findall(x -> x > 1, sparse([1 2])) == [CartesianIndex(1, 2)]
end

@testset "findnext and findprev" begin
for A in (sparse([0 0 0; 0 0 0]), sparse([0 1 2; 3 4 0]), sparse([1 2 0; 0 3 4]), sparse([0 0 0; 1 2 3]),
sparse(I, 25, 25), sparse(reshape((1:17*13) .- 2, 17, 13)), sparse(reshape((1:17*13) .- (17*13) .+ 2, 17, 13)))
for B in (A, copy(reshape(A, 1, :)), copy(reshape(A, :, 1)), copy(reshape(A, size(A, 2), size(A, 1))))
C = Array(B)
for i in keys(B)
@test findnext(iszero, B, i) == findnext(iszero, C, i)
@test findnext(iseven, B, i) == findnext(iseven, C, i)
@test findnext(isodd, B, i) == findnext(isodd, C, i)
@test findprev(iszero, B, i) == findprev(iszero, C, i)
@test findprev(iseven, B, i) == findprev(iseven, C, i)
@test findprev(isodd, B, i) == findprev(isodd, C, i)
end
end
end
end

@testset "issue #5824" begin
@test sprand(4,5,0.5).^0 == sparse(fill(1,4,5))
end
Expand Down