Skip to content

Commit

Permalink
ENH sparse: Allow sparse matrices to be size 0.
Browse files Browse the repository at this point in the history
  • Loading branch information
cowlicks committed Aug 20, 2013
1 parent dfbc4fa commit 9f5bb3b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 10 deletions.
2 changes: 1 addition & 1 deletion scipy/sparse/base.py
Expand Up @@ -79,7 +79,7 @@ def set_shape(self,shape):
except:
raise TypeError('invalid shape')

if not (shape[0] >= 1 and shape[1] >= 1):
if not (shape[0] >= 0 and shape[1] >= 0):
raise ValueError('invalid shape')

if (self._shape != shape) and (self._shape is not None):
Expand Down
12 changes: 3 additions & 9 deletions scipy/sparse/csr.py
Expand Up @@ -274,10 +274,6 @@ def extractor(indices,N):
# If all else fails, try elementwise
row, col = self._index_to_arrays(row, col)

if row.size == 0 or col.size == 0:
raise ValueError("Slice returns a size 0 matrix which is not "
"supported by sparse matrices.")

return self.__class__([[self._get_single_element(iii, jjj) for
iii, jjj in zip(ii, jj)] for ii, jj in
zip(row.tolist(), col.tolist())])
Expand Down Expand Up @@ -358,9 +354,6 @@ def _get_row_slice(self, i, cslice):
row_indices = abs(row_indices[::-1])

shape = (1, int(np.ceil(float(stop - start) / stride)))
if 0 in shape:
raise ValueError("Slice returns a size 0 matrix which is not "
"supported by sparse matrices.")

row_slice = csr_matrix((row_data, row_indices, row_indptr),
shape=shape)
Expand Down Expand Up @@ -396,9 +389,10 @@ def process_slice(sl, num):
raise TypeError('expected slice or scalar')

def check_bounds(i0, i1, num):
if not (0 <= i0 < num) or not (0 < i1 <= num) or not (i0 < i1):
if not (0 <= i0 <= num) or not (0 <= i1 <= num) or not (i0 <= i1):
raise IndexError(
"index out of bounds: 0<=%d<%d, 0<=%d<%d, %d<%d" %
"index out of bounds: 0 <= %d <= %d, 0 <= %d <= %d,"
" %d <= %d" %
(i0, num, i1, num, i0, i1))

i0, i1 = process_slice(row_slice, M)
Expand Down

0 comments on commit 9f5bb3b

Please sign in to comment.