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

scipy-svn at scipy.org scipy-svn at scipy.org
Mon Dec 31 21:27:52 EST 2007


Author: wnbell
Date: 2007-12-31 20:27:50 -0600 (Mon, 31 Dec 2007)
New Revision: 3756

Modified:
   trunk/scipy/sparse/compressed.py
   trunk/scipy/sparse/sputils.py
   trunk/scipy/sparse/tests/test_sparse.py
   trunk/scipy/sparse/tests/test_sputils.py
Log:
cache result of has_sorted_indices()


Modified: trunk/scipy/sparse/compressed.py
===================================================================
--- trunk/scipy/sparse/compressed.py	2008-01-01 02:27:24 UTC (rev 3755)
+++ trunk/scipy/sparse/compressed.py	2008-01-01 02:27:50 UTC (rev 3756)
@@ -570,9 +570,15 @@
     def has_sorted_indices(self):
         """Determine whether the matrix has sorted indices
         """
-        fn = sparsetools.csr_has_sorted_indices
-        return fn( len(self.indptr) - 1, self.indptr, self.indices)
 
+        #first check to see if result was cached
+        if not hasattr(self,'_has_sorted_indices'):
+            fn = sparsetools.csr_has_sorted_indices
+            self._has_sorted_indices = \
+                    fn( len(self.indptr) - 1, self.indptr, self.indices)
+
+        return self._has_sorted_indices
+
     def sorted_indices(self):
         """Return a copy of this matrix with sorted indices
         """
@@ -584,18 +590,15 @@
         # although the previous option is typically faster
         #return self.toother().toother()
 
-    def sort_indices(self,check_first=True):
+    def sort_indices(self):
         """Sort the indices of this matrix *in place*
         """
-        #see if sorting can be avoided
-        if check_first and self.has_sorted_indices(): 
-            return
+       
+        if not self.has_sorted_indices():
+            fn = sparsetools.csr_sort_indices
+            fn( len(self.indptr) - 1, self.indptr, self.indices, self.data)
+            self._has_sorted_indices = True
 
-        fn = sparsetools.csr_sort_indices
-        fn( len(self.indptr) - 1, self.indptr, self.indices, self.data)
-
-        #TODO store is_sorted flag somewhere
-
     def ensure_sorted_indices(self, inplace=False):
         """Return a copy of this matrix where the column indices are sorted
         """
@@ -624,6 +627,10 @@
         self.indices = self.indices[:self.nnz]
 
 
+    ###################
+    # utility methods #
+    ###################
+
     # needed by _data_matrix
     def _with_data(self,data,copy=True):
         """Returns a matrix with the same sparsity structure as self,
@@ -637,7 +644,6 @@
             return self.__class__((data,self.indices,self.indptr), \
                                    shape=self.shape,dtype=data.dtype)
 
-    # utility functions
     def _binopt(self, other, op, in_shape=None, out_shape=None):
         """apply the binary operation fn to two sparse matrices"""
         other = self.__class__(other)

Modified: trunk/scipy/sparse/sputils.py
===================================================================
--- trunk/scipy/sparse/sputils.py	2008-01-01 02:27:24 UTC (rev 3755)
+++ trunk/scipy/sparse/sputils.py	2008-01-01 02:27:50 UTC (rev 3756)
@@ -41,7 +41,7 @@
     upcast = sample.dtype 
 
     for t in supported_dtypes:
-        if can_cast(sample.dtype,t):
+        if np.can_cast(sample.dtype,t):
             return t
     
     raise TypeError,'no supported conversion for types: %s' % args

Modified: trunk/scipy/sparse/tests/test_sparse.py
===================================================================
--- trunk/scipy/sparse/tests/test_sparse.py	2008-01-01 02:27:24 UTC (rev 3755)
+++ trunk/scipy/sparse/tests/test_sparse.py	2008-01-01 02:27:50 UTC (rev 3756)
@@ -117,7 +117,8 @@
             start = time.clock()
             iter = 0
             while iter < 5 and time.clock() - start < 1:
-                A.sort_indices(check_first =False) 
+                A._has_sorted_indices = False
+                A.sort_indices() 
                 iter += 1
             end = time.clock()
 

Modified: trunk/scipy/sparse/tests/test_sputils.py
===================================================================
--- trunk/scipy/sparse/tests/test_sputils.py	2008-01-01 02:27:24 UTC (rev 3755)
+++ trunk/scipy/sparse/tests/test_sputils.py	2008-01-01 02:27:50 UTC (rev 3756)
@@ -1,8 +1,8 @@
 """unit tests for sparse utility functions"""
 
 import numpy as np
+from numpy.testing import *
 
-from np.testing import *
 set_package_path()
 from scipy.sparse.sputils import *
 restore_path()
@@ -12,7 +12,7 @@
 class TestSparseUtils(NumpyTestCase):
 
     def check_upcast(self):
-        assert_equal(upcast('int32'),np.int32)
+        assert_equal(upcast('intc'),np.intc)
         assert_equal(upcast('int32','float32'),np.float64)
         assert_equal(upcast('bool',complex,float),np.complex128)
         assert_equal(upcast('i','d'),np.float64)




More information about the Scipy-svn mailing list