Skip to content

Commit

Permalink
BUG: sparse: fixed the resize() method of dok_matrix (ticket #997); t…
Browse files Browse the repository at this point in the history
…hanks to jap for report and patch.
  • Loading branch information
warren.weckesser committed Nov 27, 2010
1 parent a7eabc5 commit 0dd55fa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
7 changes: 4 additions & 3 deletions scipy/sparse/dok.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,8 +531,9 @@ def toarray(self):
return self.tocoo().toarray()

def resize(self, shape):
""" Resize the matrix to dimensions given by 'shape', removing any
non-zero elements that lie outside.
""" Resize the matrix in-place to dimensions given by 'shape'.
Any non-zero elements that lie outside the new shape are removed.
"""
if not isshape(shape):
raise TypeError("dimensions must be a 2-tuple of positive"
Expand All @@ -544,7 +545,7 @@ def resize(self, shape):
for (i, j) in self.keys():
if i >= newM or j >= newN:
del self[i, j]
self.shape = shape
self._shape = shape



Expand Down
15 changes: 15 additions & 0 deletions scipy/sparse/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1298,6 +1298,21 @@ def test_ctor(self):
c = csr_matrix(b)
assert_equal(A.todense(), c.todense())

def test_resize(self):
"""A couple basic tests of the resize() method.
resize(shape) resizes the array in-place.
"""
a = dok_matrix((5,5))
a[:,0] = 1
a.resize((2,2))
expected1 = array([[1,0],[1,0]])
assert_array_equal(a.todense(), expected1)
a.resize((3,2))
expected2 = array([[1,0],[1,0],[0,0]])
assert_array_equal(a.todense(), expected2)


def test_ticket1160(self):
"""Regression test for ticket #1160."""
a = dok_matrix((3,3))
Expand Down

0 comments on commit 0dd55fa

Please sign in to comment.