[Scipy-svn] r3192 - trunk/Lib/sparse/tests

scipy-svn at scipy.org scipy-svn at scipy.org
Thu Jul 26 01:07:18 EDT 2007


Author: wnbell
Date: 2007-07-26 00:06:50 -0500 (Thu, 26 Jul 2007)
New Revision: 3192

Modified:
   trunk/Lib/sparse/tests/test_sparse.py
Log:
added additional unittests for arithmetic between sparse & dense matrices



Modified: trunk/Lib/sparse/tests/test_sparse.py
===================================================================
--- trunk/Lib/sparse/tests/test_sparse.py	2007-07-26 01:01:14 UTC (rev 3191)
+++ trunk/Lib/sparse/tests/test_sparse.py	2007-07-26 05:06:50 UTC (rev 3192)
@@ -42,6 +42,10 @@
         A = matrix([[-1, 0, 17],[0, -5, 0],[1, -4, 0],[0,0,0]],'d')
         assert_equal(abs(A),abs(self.spmatrix(A)).todense())
 
+    def check_neg(self):
+        A = matrix([[-1, 0, 17],[0, -5, 0],[1, -4, 0],[0,0,0]],'d')
+        assert_equal(-A,(-self.spmatrix(A)).todense())
+
     def check_sum(self):
         """Does the matrix's sum(,axis=0) method work?
         """
@@ -123,10 +127,13 @@
     
     def check_rsub(self):
         assert_array_equal((self.dat - self.datsp),[[0,0,0,0],[0,0,0,0],[0,0,0,0]])
+        assert_array_equal((self.datsp - self.dat),[[0,0,0,0],[0,0,0,0],[0,0,0,0]])
 
         A = self.spmatrix(matrix([[1,0,0,4],[-1,0,0,0],[0,8,0,-5]],'d'))
         assert_array_equal((self.dat - A),self.dat - A.todense())
-        assert_array_equal((A.todense() - self.datsp),A.todense() - self.dat)
+        assert_array_equal((A - self.dat),A.todense() - self.dat)
+        assert_array_equal(A.todense() - self.datsp,A.todense() - self.dat)
+        assert_array_equal(self.datsp - A.todense(),self.dat - A.todense())
 
     def check_elmul(self):
         a = self.datsp
@@ -276,6 +283,15 @@
         sum2 = self.datsp + self.dat
         assert_array_equal(sum2, 2*self.dat)
 
+    def check_sub_dense(self):
+        """ Check whether adding a dense matrix to a sparse matrix works
+        """
+        sum1 = 3*self.dat - self.datsp
+        assert_array_equal(sum1, 2*self.dat)
+        sum2 = 3*self.datsp - self.dat
+        assert_array_equal(sum2, 2*self.dat)
+
+
     def check_copy(self):
         """ Check whether the copy=True and copy=False keywords work
         """
@@ -429,7 +445,7 @@
         
         self.dtypes =  [float32,float64,complex64,complex128]
 
-    def check_pl(self):
+    def check_add_sub(self):
         self.arith_init()
         
         #basic tests
@@ -448,10 +464,10 @@
                     Bsp = self.spmatrix(B)
                     Csp = self.spmatrix(C)
 
+                    #addition
                     D1 = A + B
                     D2 = A + C
                     D3 = B + C
-
                     S1 = Asp + Bsp
                     S2 = Asp + Csp
                     S3 = Bsp + Csp
@@ -459,12 +475,38 @@
                     assert_array_equal(D1,S1.todense())
                     assert_array_equal(D2,S2.todense())
                     assert_array_equal(D3,S3.todense())
+                    assert_array_equal(D1.dtype,S1.dtype)
+                    assert_array_equal(D2.dtype,S2.dtype)
+                    assert_array_equal(D3.dtype,S3.dtype)
+                    assert_array_equal(D1,Asp + B)          #check sparse + dense
+                    assert_array_equal(D2,Asp + C)
+                    assert_array_equal(D3,Bsp + C)
+                    assert_array_equal(D1,A + Bsp)          #check dense + sparse
+                    assert_array_equal(D2,A + Csp)
+                    assert_array_equal(D3,B + Csp)
 
+                    #subtraction
+                    D1 = A - B
+                    D2 = A - C
+                    D3 = B - C
+                    S1 = Asp - Bsp
+                    S2 = Asp - Csp
+                    S3 = Bsp - Csp
+                    
+                    assert_array_equal(D1,S1.todense())
+                    assert_array_equal(D2,S2.todense())
+                    assert_array_equal(D3,S3.todense())
                     assert_array_equal(D1.dtype,S1.dtype)
                     assert_array_equal(D2.dtype,S2.dtype)
                     assert_array_equal(D3.dtype,S3.dtype)
-                    
+                    assert_array_equal(D1,Asp - B)          #check sparse - dense
+                    assert_array_equal(D2,Asp - C)
+                    assert_array_equal(D3,Bsp - C)
+                    assert_array_equal(D1,A - Bsp)          #check dense - sparse
+                    assert_array_equal(D2,A - Csp)
+                    assert_array_equal(D3,B - Csp)
 
+
     def check_mu(self):
         self.arith_init()
         
@@ -495,7 +537,6 @@
                     assert_array_equal(D1,S1.todense())
                     assert_array_equal(D2,S2.todense())
                     assert_array_equal(D3,S3.todense())
-
                     assert_array_equal(D1.dtype,S1.dtype)
                     assert_array_equal(D2.dtype,S2.dtype)
                     assert_array_equal(D3.dtype,S3.dtype)




More information about the Scipy-svn mailing list