Skip to content

Commit

Permalink
Merge pull request scipy#4660 from lightcatcher/avoid_extra_slice_copy
Browse files Browse the repository at this point in the history
Avoid extra copy for sparse compressed [:, seq] and [seq, :] slices.
  • Loading branch information
argriffing committed Apr 17, 2015
2 parents 6c59141 + 43ce18e commit 3e82f1a
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions scipy/sparse/csr.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,14 +283,22 @@ def extractor(indices,N):
# col is int or slice with step 1, row is slice with step 1.
return self._get_submatrix(row, col)
elif issequence(col):
P = extractor(col,self.shape[1]).T # [1:2,[1,2]]
# row is slice, col is sequence.
return self[row,:]*P
P = extractor(col,self.shape[1]).T # [1:2,[1,2]]
sliced = self
if row != slice(None, None, None):
sliced = sliced[row,:]
return sliced * P

elif issequence(row):
# [[1,2],??]
if isintlike(col) or isinstance(col,slice):
P = extractor(row, self.shape[0]) # [[1,2],j] or [[1,2],1:2]
return (P*self)[:,col]
extracted = P * self
if col == slice(None, None, None):
return extracted
else:
return extracted[:,col]

if not (issequence(col) and issequence(row)):
# Sample elementwise
Expand Down

0 comments on commit 3e82f1a

Please sign in to comment.