Skip to content

Commit

Permalink
Sparse matrix indexing with ranges is slow and memory hungry
Browse files Browse the repository at this point in the history
This fixes the case when a whole column is indexed.  However, indexing
parts of columns still has that problem and needs a bigger rewrite.
  • Loading branch information
mauro3 committed Mar 27, 2014
1 parent 7d475bf commit e5fc806
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions base/sparse/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,7 @@ end
# TODO: See if growing arrays is faster than pre-computing structure
# and then populating nonzeros
# TODO: Use binary search in cases where nI >> nfilled(A[:,j]) or nI << nfilled(A[:,j])
function getindex_I_sorted{Tv,Ti}(A::SparseMatrixCSC{Tv,Ti}, I::Vector, J::AbstractVector)
function getindex_I_sorted{Tv,Ti}(A::SparseMatrixCSC{Tv,Ti}, I::AbstractVector, J::AbstractVector)

(m, n) = size(A)
nI = length(I)
Expand Down Expand Up @@ -929,17 +929,26 @@ end
# S = A[I, J]
function getindex{Tv,Ti}(A::SparseMatrixCSC{Tv,Ti}, I::AbstractVector, J::AbstractVector)
m = size(A, 1)

if isa(I, Range) || isa(I, Range1); I = [I]; end

if I == 1:m
return getindex_cols(A, J)
elseif issorted(I)
return getindex_I_sorted(A, I, J)
else
return getindex_general(A, I, J)
if isa(I, Ranges)
if I == 1:m # whole columns
return getindex_cols(A, J)
else # ranges are always sorted, but maybe in reverse
if step(I)>0
return getindex_I_sorted(A, I, J)
else
I = [I]
return getindex_general(A, I, J)
# todo:
# return reverse(getindex_I_sorted(A, reverse(I), J))
end
end
else
if issorted(I)
return getindex_I_sorted(A, I, J)
else
return getindex_general(A, I, J)
end
end

end

# logical getindex
Expand Down

0 comments on commit e5fc806

Please sign in to comment.