[Scipy-svn] r2536 - trunk/Lib/sparse

scipy-svn at scipy.org scipy-svn at scipy.org
Thu Jan 11 09:53:23 CST 2007


Author: wnbell
Date: 2007-01-11 09:53:21 -0600 (Thu, 11 Jan 2007)
New Revision: 2536

Modified:
   trunk/Lib/sparse/sparse.py
Log:
coo_matrix._check() now checks for valid row/column indices


Modified: trunk/Lib/sparse/sparse.py
===================================================================
--- trunk/Lib/sparse/sparse.py	2007-01-11 09:43:18 UTC (rev 2535)
+++ trunk/Lib/sparse/sparse.py	2007-01-11 15:53:21 UTC (rev 2536)
@@ -6,7 +6,7 @@
 """
 
 from numpy import zeros, isscalar, real, imag, asarray, asmatrix, matrix, \
-                  ndarray, amax, rank, conj, searchsorted, ndarray,   \
+                  ndarray, amax, amin, rank, conj, searchsorted, ndarray,   \
                   less, where, greater, array, transpose, empty, ones, \
                   arange, shape, intc
 import numpy
@@ -2088,6 +2088,8 @@
             raise TypeError, "invalid input format"
         
         if dims is None:
+            if len(ij[0]) == 0 or len(ij[1]) == 0:
+                raise ValueError, "cannot infer dimensions from zero sized index arrays"
             M = int(amax(ij[0])) + 1
             N = int(amax(ij[1])) + 1
             self.shape = (M, N)
@@ -2095,6 +2097,7 @@
             # Use 2 steps to ensure dims has length 2.
             M, N = dims
             self.shape = (M, N)
+            
         self.row = asarray(ij[0], dtype=numpy.intc)
         self.col = asarray(ij[1], dtype=numpy.intc)
         self.data = asarray(obj, dtype=self.dtype)
@@ -2113,6 +2116,16 @@
         if (self.col.dtype != numpy.intc):
             self.col = self.col.astype(numpy.intc)
 
+        if nnz > 0:
+            if(amax(self.row) >= self.shape[0]):
+                raise ValueError, "row index exceedes matrix dimensions"
+            if(amax(self.col) >= self.shape[1]):
+                raise ValueError, "column index exceedes matrix dimensions"
+            if(amin(self.row) < 0):
+                raise ValueError, "negative row index found"
+            if(amin(self.col) < 0):
+                raise ValueError, "negative column index found"
+
         # some functions pass floats
         self.shape = tuple([int(x) for x in self.shape])
         self.nnz = nnz



More information about the Scipy-svn mailing list