[Scipy-svn] r3892 - trunk/scipy/sparse

scipy-svn@scip... scipy-svn@scip...
Sun Feb 3 13:25:31 CST 2008


Author: wnbell
Date: 2008-02-03 13:25:28 -0600 (Sun, 03 Feb 2008)
New Revision: 3892

Modified:
   trunk/scipy/sparse/coo.py
Log:
make coo_matrix.nnz a property


Modified: trunk/scipy/sparse/coo.py
===================================================================
--- trunk/scipy/sparse/coo.py	2008-02-02 19:30:41 UTC (rev 3891)
+++ trunk/scipy/sparse/coo.py	2008-02-03 19:25:28 UTC (rev 3892)
@@ -5,9 +5,8 @@
 from itertools import izip
 from warnings import warn 
 
-from numpy import array, asarray, empty, intc, zeros, bincount, \
-        unique, searchsorted, atleast_2d, lexsort, cumsum, concatenate, \
-        empty_like, arange
+from numpy import array, asarray, empty, intc, zeros,  \
+        unique, searchsorted, atleast_2d, empty_like, rank
 
 from sparsetools import coo_tocsr, coo_tocsc, coo_todense
 from base import isspmatrix
@@ -175,16 +174,22 @@
 
         self._check()
 
-
-    def _check(self):
-        """ Checks for consistency and stores the number of non-zeros as
-        self.nnz.
-        """
+    def getnnz(self):
         nnz = len(self.data)
         if (nnz != len(self.row)) or (nnz != len(self.col)):
             raise ValueError, "row, column, and data array must all be "\
                   "the same length"
 
+        if rank(self.data) != 1 or rank(self.row) != 1 or rank(self.col) != 1:
+            raise ValueError, "row, column, and data arrays must have rank 1"
+
+        return nnz
+    nnz = property(fget=getnnz)
+
+    def _check(self):
+        """ Checks data structure for consistency """
+        nnz = self.nnz
+
         # index arrays should have integer data types
         if self.row.dtype.kind != 'i':
             warn("row index array has non-integer dtype (%s)  " \
@@ -210,7 +215,6 @@
 
         # some functions pass floats
         self.shape = tuple([int(x) for x in self.shape])
-        self.nnz = nnz
 
     def rowcol(self, num):
         return (self.row[num], self.col[num])
@@ -305,12 +309,7 @@
 
         dok = dok_matrix((self.shape),dtype=self.dtype)
 
-        try:
-            dok.update( izip(izip(self.row,self.col),self.data) ) 
-        except AttributeError:
-            # the dict() call is for Python 2.3 compatibility
-            # ideally dok_matrix would accept an iterator
-            dok.update( dict( izip(izip(self.row,self.col),self.data) ) )
+        dok.update( izip(izip(self.row,self.col),self.data) ) 
 
         return dok
 



More information about the Scipy-svn mailing list