Skip to content

Commit

Permalink
reverse traversal of row entries if step(I) < 0 (JuliaLang#22544)
Browse files Browse the repository at this point in the history
* reverse traversal of row entries if step(I) < 0

* add test

* Update sparse.jl
  • Loading branch information
KristofferC committed Jun 26, 2017
1 parent 49fc8e6 commit eaf31b5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
5 changes: 4 additions & 1 deletion base/sparse/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1851,6 +1851,9 @@ function getindex_cols(A::SparseMatrixCSC{Tv,Ti}, J::AbstractVector) where {Tv,T
return SparseMatrixCSC(m, nJ, colptrS, rowvalS, nzvalS)
end

getindex_traverse_col(::AbstractUnitRange, lo::Int, hi::Int) = lo:hi
getindex_traverse_col(I::StepRange, lo::Int, hi::Int) = step(I) > 0 ? (lo:1:hi) : (hi:-1:lo)

function getindex(A::SparseMatrixCSC{Tv,Ti}, I::Range, J::AbstractVector) where {Tv,Ti<:Integer}
# Ranges for indexing rows
(m, n) = size(A)
Expand Down Expand Up @@ -1884,7 +1887,7 @@ function getindex(A::SparseMatrixCSC{Tv,Ti}, I::Range, J::AbstractVector) where

@inbounds for j = 1:nJ
col = J[j]
for k = colptrA[col]:colptrA[col+1]-1
for k = getindex_traverse_col(I, colptrA[col], colptrA[col+1]-1)
rowA = rowvalA[k]
i = rangesearch(I, rowA)
if i > 0
Expand Down
9 changes: 9 additions & 0 deletions test/sparse/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1795,3 +1795,12 @@ end
@test nnz(A) == n
@test A == eye(n)
end

@testset "reverse search direction if step < 0 #21986" begin
srand(1234)
A = sprand(5, 5, 1/5)
A = max.(A, A')
A = spones(A)
B = A[5:-1:1, 5:-1:1]
@test issymmetric(B)
end

0 comments on commit eaf31b5

Please sign in to comment.