[Scipy-svn] r4678 - in trunk/scipy/sparse: . tests

scipy-svn@scip... scipy-svn@scip...
Fri Aug 29 02:15:01 CDT 2008


Author: wnbell
Date: 2008-08-29 02:14:57 -0500 (Fri, 29 Aug 2008)
New Revision: 4678

Modified:
   trunk/scipy/sparse/lil.py
   trunk/scipy/sparse/tests/test_base.py
Log:
fixed inplace operations on lil_matrix


Modified: trunk/scipy/sparse/lil.py
===================================================================
--- trunk/scipy/sparse/lil.py	2008-08-29 02:01:12 UTC (rev 4677)
+++ trunk/scipy/sparse/lil.py	2008-08-29 07:14:57 UTC (rev 4678)
@@ -292,8 +292,15 @@
         if isscalar(x):
             x = self.dtype.type(x)
         elif not isinstance(x, spmatrix):
-            x = numpy.asarray(x, dtype=self.dtype)
+            x = lil_matrix(x)
 
+        if isspmatrix(x) and index == (slice(None), slice(None)):
+            # self[:,:] = other_sparse
+            x = lil_matrix(x)
+            self.rows = x.rows
+            self.data = x.data
+            return
+
         try:
             i, j = index
         except (ValueError, TypeError):

Modified: trunk/scipy/sparse/tests/test_base.py
===================================================================
--- trunk/scipy/sparse/tests/test_base.py	2008-08-29 02:01:12 UTC (rev 4677)
+++ trunk/scipy/sparse/tests/test_base.py	2008-08-29 07:14:57 UTC (rev 4678)
@@ -1193,25 +1193,22 @@
         B[0,:] = A[0,:]
         assert_array_equal(A[0,:].A, B[0,:].A)
 
-    def tst_inplace_op(self,op,arr,other,result):
-        cpy = arr
-        getattr(arr,"__i%s__" % op)(other)
 
-        assert_array_equal(cpy.todense(),arr.todense())
-        assert_array_equal(arr.todense(),result)
+    def test_inplace_ops(self):
+        A = lil_matrix([[0,2,3],[4,0,6]])
+        B = lil_matrix([[0,1,0],[0,2,3]])
 
-    def testip_inplace_ops(self):
-        B = self.B[:3,:3].copy()
-        B[:,:] = B-B
-        C = B.todense()
+        data = {'add': (B,A + B),
+                'sub': (B,A - B),
+                'mul': (3,A * 3)}
 
-        data = {'add':(B,C+C),
-                'sub':(B,zeros(B.shape)),
-                'mul':(3,C*3)}
+        for op,(other,expected) in data.iteritems():
+            result = A.copy()
+            getattr(result, '__i%s__' % op)(other)
 
-        return [(self.tst_inplace_op,op,B,other,result)
-                for op,(other,result) in data.iteritems()]
+            assert_array_equal(result.todense(), expected.todense())
 
+
     def test_lil_slice_assignment(self):
         B = lil_matrix((4,3))
         B[0,0] = 5



More information about the Scipy-svn mailing list