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

scipy-svn at scipy.org scipy-svn at scipy.org
Wed Jan 2 19:03:05 EST 2008


Author: wnbell
Date: 2008-01-02 18:02:34 -0600 (Wed, 02 Jan 2008)
New Revision: 3767

Modified:
   trunk/scipy/sparse/bsr.py
   trunk/scipy/sparse/compressed.py
   trunk/scipy/sparse/csc.py
   trunk/scipy/sparse/csr.py
   trunk/scipy/sparse/tests/test_base.py
Log:
made has_sorted_indices a property


Modified: trunk/scipy/sparse/bsr.py
===================================================================
--- trunk/scipy/sparse/bsr.py	2008-01-02 23:20:18 UTC (rev 3766)
+++ trunk/scipy/sparse/bsr.py	2008-01-03 00:02:34 UTC (rev 3767)
@@ -441,7 +441,7 @@
     def sort_indices(self):
         """Sort the indices of this matrix *in place*
         """
-        if self.has_sorted_indices():
+        if self.has_sorted_indices:
             return
 
         from csr import csr_matrix
@@ -460,7 +460,7 @@
         self.data[:] = self.data[proxy.data]
         self.indices[:] = proxy.indices
 
-        self._has_sorted_indices = True
+        self.has_sorted_indices = True
 
     def prune(self):
         """ Remove empty space after all non-zero elements.

Modified: trunk/scipy/sparse/compressed.py
===================================================================
--- trunk/scipy/sparse/compressed.py	2008-01-02 23:20:18 UTC (rev 3766)
+++ trunk/scipy/sparse/compressed.py	2008-01-03 00:02:34 UTC (rev 3767)
@@ -414,6 +414,7 @@
             col = key[1]
            
             #TODO implement CSR[ [1,2,3], X ] with sparse matmat
+            #TODO make use of sorted indices
 
             if isintlike(row) and isintlike(col):
                 return self._get_single_element(row,col)
@@ -486,9 +487,9 @@
                 warn('changing the sparsity structure of a %s_matrix is expensive. ' \
                         'lil_matrix is more efficient.' % self.format, \
                         SparseEfficiencyWarning)
+
                 self.sort_indices()
    
-                #no harm if not sorted
                 newindx = self.indices[start:end].searchsorted(minor_index)
                 newindx += start
 
@@ -567,18 +568,26 @@
 
         self.prune() #nnz may have changed
 
-    def has_sorted_indices(self):
+
+    def __get_sorted(self):
         """Determine whether the matrix has sorted indices
+
+            True if the indices of the matrix are in
+            sorted order, False otherwise.
         """
 
         #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 = \
+            self.__has_sorted_indices = \
                     fn( len(self.indptr) - 1, self.indptr, self.indices)
+        return self.__has_sorted_indices
 
-        return self._has_sorted_indices
+    def __set_sorted(self,val):
+        self.__has_sorted_indices = bool(val)
 
+    has_sorted_indices = property(fget=__get_sorted, fset=__set_sorted)
+
     def sorted_indices(self):
         """Return a copy of this matrix with sorted indices
         """
@@ -594,10 +603,10 @@
         """Sort the indices of this matrix *in place*
         """
        
-        if not self.has_sorted_indices():
+        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
+            self.has_sorted_indices = True
 
     def ensure_sorted_indices(self, inplace=False):
         """Return a copy of this matrix where the column indices are sorted
@@ -677,7 +686,9 @@
             indices = indices.copy()
             data    = data.copy()
 
-        return self.__class__((data, indices, indptr), shape=out_shape)
+        A = self.__class__((data, indices, indptr), shape=out_shape)
+        A.has_sorted_indices = True
+        return A
 
     def _get_submatrix( self, shape0, shape1, slice0, slice1 ):
         """Return a submatrix of this matrix (new matrix is created)."""

Modified: trunk/scipy/sparse/csc.py
===================================================================
--- trunk/scipy/sparse/csc.py	2008-01-02 23:20:18 UTC (rev 3766)
+++ trunk/scipy/sparse/csc.py	2008-01-03 00:02:34 UTC (rev 3767)
@@ -128,7 +128,7 @@
 
         from csr import csr_matrix
         A = csr_matrix((data, indices, indptr), self.shape)
-        A._has_sorted_indices = True
+        A.has_sorted_indices = True
         return A
 
     def tobsr(self, blocksize=None):

Modified: trunk/scipy/sparse/csr.py
===================================================================
--- trunk/scipy/sparse/csr.py	2008-01-02 23:20:18 UTC (rev 3766)
+++ trunk/scipy/sparse/csr.py	2008-01-03 00:02:34 UTC (rev 3767)
@@ -144,7 +144,7 @@
 
         from csc import csc_matrix
         A = csc_matrix((data, indices, indptr), self.shape)
-        A._has_sorted_indices = True
+        A.has_sorted_indices = True
         return A
 
     def tobsr(self,blocksize=None,copy=True):

Modified: trunk/scipy/sparse/tests/test_base.py
===================================================================
--- trunk/scipy/sparse/tests/test_base.py	2008-01-02 23:20:18 UTC (rev 3766)
+++ trunk/scipy/sparse/tests/test_base.py	2008-01-03 00:02:34 UTC (rev 3767)
@@ -33,6 +33,7 @@
 #TODO check that spmatrix( ... , copy=X ) is respected
 #TODO test repr(spmatrix)
 #TODO test prune
+#TODO test has_sorted_indices
 class _TestCommon:
     """test common functionality shared by all sparse formats"""
 




More information about the Scipy-svn mailing list