[Scipy-svn] r2951 - in trunk/Lib/sparse: . tests

scipy-svn at scipy.org scipy-svn at scipy.org
Wed May 2 04:56:01 EDT 2007


Author: stefan
Date: 2007-05-02 03:55:39 -0500 (Wed, 02 May 2007)
New Revision: 2951

Modified:
   trunk/Lib/sparse/sparse.py
   trunk/Lib/sparse/tests/test_sparse.py
Log:
Fix multiplication by zero in lil_matrix.


Modified: trunk/Lib/sparse/sparse.py
===================================================================
--- trunk/Lib/sparse/sparse.py	2007-04-30 22:15:08 UTC (rev 2950)
+++ trunk/Lib/sparse/sparse.py	2007-05-02 08:55:39 UTC (rev 2951)
@@ -2415,21 +2415,21 @@
                     self._insertat3(row, data, j, xx)
         else:
             raise ValueError, "invalid index value: %s" % str((i, j))
-                
 
     def __mul__(self, other):           # self * other
         if isscalarlike(other):
-            new = self.copy()
             if other == 0:
                 # Multiply by zero: return the zero matrix
-                return new
-            # Multiply this scalar by every element.
-            new.data = numpy.array([[val*other for val in rowvals] for rowvals in new.data], dtype=object)
+                new = lil_matrix(shape=self.shape, dtype=self.dtype)
+            else:
+                new = self.copy()
+                # Multiply this scalar by every element.
+                new.data = numpy.array([[val*other for val in rowvals] for
+                                        rowvals in new.data], dtype=object)
             return new
         else:
             return self.dot(other)
 
-
     def copy(self):
         new = lil_matrix(self.shape, dtype=self.dtype)
         new.data = copy.deepcopy(self.data)

Modified: trunk/Lib/sparse/tests/test_sparse.py
===================================================================
--- trunk/Lib/sparse/tests/test_sparse.py	2007-04-30 22:15:08 UTC (rev 2950)
+++ trunk/Lib/sparse/tests/test_sparse.py	2007-05-02 08:55:39 UTC (rev 2951)
@@ -710,7 +710,17 @@
         B[5,6] = 20
         assert_array_equal(A * A.T, (B * B.T).todense())
         assert_array_equal(A * A.H, (B * B.H).todense())
-    
+
+    def check_scalar_mul(self):
+        x = lil_matrix((3,3))
+        x[0,0] = 2
+
+        x = x*2
+        assert_equal(x[0,0],4)
+
+        x = x*0
+        assert_equal(x[0,0],0)
+
     def check_lil_lil_assignment(self):
         """ Tests whether a row of one lil_matrix can be assigned to
         another.




More information about the Scipy-svn mailing list