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

scipy-svn at scipy.org scipy-svn at scipy.org
Fri Nov 2 01:19:19 EDT 2007


Author: wnbell
Date: 2007-11-02 00:19:17 -0500 (Fri, 02 Nov 2007)
New Revision: 3486

Modified:
   trunk/scipy/sparse/sparse.py
Log:
further refactoring of csr/csc constructors


Modified: trunk/scipy/sparse/sparse.py
===================================================================
--- trunk/scipy/sparse/sparse.py	2007-11-02 04:18:04 UTC (rev 3485)
+++ trunk/scipy/sparse/sparse.py	2007-11-02 05:19:17 UTC (rev 3486)
@@ -91,10 +91,19 @@
         self.allocsize = allocsize
 
     def set_shape(self,shape):
-        s = tuple(shape)
-        if len(s) != 2:
+        shape = tuple(shape)
+        
+        if len(shape) != 2:
             raise ValueError("Only two-dimensional sparse arrays "
-                             "are supported.")
+                                     "are supported.")
+        try:
+            shape = int(shape[0]),int(shape[1]) #floats, other weirdness
+        except:
+            raise TypeError,'invalid shape'
+
+        if not (shape[0] >= 1 and shape[1] >= 1):
+            raise TypeError,'invalid shape'
+        
         if (self._shape != shape) and (self._shape is not None):
             try:
                 self = self.reshape(shape)
@@ -506,7 +515,10 @@
 
 class _cs_matrix(spmatrix):
     """base matrix class for compressed row and column oriented matrices"""
+
     def _set_self(self, other, copy=False):
+        """take the member variables of other and assign them to self"""
+        
         if copy:
             other = other.copy()
         
@@ -983,32 +995,19 @@
             raise ValueError, "unrecognized form for csc_matrix constructor"
 
 
-
         # Read matrix dimensions given, if any
         if dims is not None:
-            try:
-                (M, N) = dims
-                M,N = int(M),int(N)
-            except (TypeError, ValueError), e:
-                raise TypeError, "dimensions not understood"
+            self.shape = dims
         else:
-            # Read existing matrix dimensions
-            try:
-                (oldM, oldN) = self.shape
-            except:
-                oldM = oldN = None
+            if self.shape is None:                
+                # shape not already set, try to infer dimensions
+                try:
+                    M = self.indices.max() + 1
+                    N = len(self.indptr) - 1
+                    self.shape = (M,N)
+                except:
+                    raise ValueError,'unable to infer matrix dimensions'
 
-            # Expand if necessary
-            M = N = None
-            N = max(0, oldN, N, len(self.indptr) - 1)
-            if len(self.indices) > 0:
-                M = max(oldM, M, int(amax(self.indices)) + 1)
-            else:
-                # Matrix is completely empty
-                M = max(oldM, M)
-
-        self.shape = (M, N)
-
         self.check_format(full_check=False)
 
     def check_format(self,full_check=True):
@@ -1285,30 +1284,19 @@
         else:
             raise ValueError, "unrecognized form for csr_matrix constructor"
 
-
         # Read matrix dimensions given, if any
         if dims is not None:
-            try:
-                (M, N) = dims
-            except (TypeError, ValueError), e:
-                raise TypeError, "dimensions not understood"
+            self.shape = dims
         else:
-            # Read existing matrix dimensions
-            try:
-                (oldM, oldN) = self.shape
-            except:
-                oldM = oldN = None
+            if self.shape is None:                
+                # shape not already set, try to infer dimensions
+                try:
+                    M = len(self.indptr) - 1
+                    N = self.indices.max() + 1
+                    self.shape = (M,N)
+                except:
+                    raise ValueError,'unable to infer matrix dimensions'
 
-            M = N = None
-            M = max(0, oldM, M, len(self.indptr) - 1)
-            if len(self.indices) > 0:
-                N = max(oldN, N, int(amax(self.indices)) + 1)
-            else:
-                # Matrix is completely empty
-                N = max(oldN, N)
-
-        self.shape = (M, N)
-
         self.check_format(full_check=False)
 
     def check_format(self,full_check=True):




More information about the Scipy-svn mailing list