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

scipy-svn@scip... scipy-svn@scip...
Sun Oct 26 22:56:27 CDT 2008


Author: wnbell
Date: 2008-10-26 22:56:22 -0500 (Sun, 26 Oct 2008)
New Revision: 4855

Modified:
   trunk/scipy/sparse/bsr.py
   trunk/scipy/sparse/compressed.py
   trunk/scipy/sparse/coo.py
   trunk/scipy/sparse/dia.py
   trunk/scipy/sparse/dok.py
   trunk/scipy/sparse/extract.py
   trunk/scipy/sparse/lil.py
   trunk/scipy/sparse/tests/test_base.py
   trunk/scipy/sparse/tests/test_extract.py
Log:
added scipy.sparse.find()
fixed handling of dtype= constructor argument in dense case


Modified: trunk/scipy/sparse/bsr.py
===================================================================
--- trunk/scipy/sparse/bsr.py	2008-10-27 03:08:05 UTC (rev 4854)
+++ trunk/scipy/sparse/bsr.py	2008-10-27 03:56:22 UTC (rev 4855)
@@ -126,7 +126,7 @@
             elif len(arg1) == 2:
                 # (data,(row,col)) format
                 from coo import coo_matrix
-                self._set_self( coo_matrix(arg1).tobsr(blocksize=blocksize) )
+                self._set_self( coo_matrix(arg1, dtype=dtype).tobsr(blocksize=blocksize) )
 
             elif len(arg1) == 3:
                 # (data,indices,indptr) format
@@ -144,7 +144,7 @@
                 raise ValueError("unrecognized form for" \
                         " %s_matrix constructor" % self.format)
             from coo import coo_matrix
-            arg1 = coo_matrix(arg1).tobsr(blocksize=blocksize)
+            arg1 = coo_matrix(arg1, dtype=dtype).tobsr(blocksize=blocksize)
             self._set_self( arg1 )
 
         if shape is not None:

Modified: trunk/scipy/sparse/compressed.py
===================================================================
--- trunk/scipy/sparse/compressed.py	2008-10-27 03:08:05 UTC (rev 4854)
+++ trunk/scipy/sparse/compressed.py	2008-10-27 03:56:22 UTC (rev 4855)
@@ -68,7 +68,7 @@
                 raise ValueError, "unrecognized %s_matrix constructor usage" % \
                         self.format
             from coo import coo_matrix
-            self._set_self( self.__class__(coo_matrix(arg1)) )
+            self._set_self( self.__class__(coo_matrix(arg1, dtype=dtype)) )
 
         # Read matrix dimensions given, if any
         if shape is not None:

Modified: trunk/scipy/sparse/coo.py
===================================================================
--- trunk/scipy/sparse/coo.py	2008-10-27 03:08:05 UTC (rev 4854)
+++ trunk/scipy/sparse/coo.py	2008-10-27 03:56:22 UTC (rev 4855)
@@ -172,6 +172,10 @@
                 self.row,self.col = (M != 0).nonzero()
                 self.data  = M[self.row,self.col]
 
+        if dtype is not None:
+            self.data = self.data.astype(dtype)
+
+
         self._check()
 
     def getnnz(self):

Modified: trunk/scipy/sparse/dia.py
===================================================================
--- trunk/scipy/sparse/dia.py	2008-10-27 03:08:05 UTC (rev 4854)
+++ trunk/scipy/sparse/dia.py	2008-10-27 03:56:22 UTC (rev 4855)
@@ -94,7 +94,7 @@
                 raise ValueError("unrecognized form for" \
                         " %s_matrix constructor" % self.format)
             from coo import coo_matrix
-            A = coo_matrix(arg1).todia()
+            A = coo_matrix(arg1, dtype=dtype).todia()
             self.data    = A.data
             self.offsets = A.offsets
             self.shape   = A.shape

Modified: trunk/scipy/sparse/dok.py
===================================================================
--- trunk/scipy/sparse/dok.py	2008-10-27 03:08:05 UTC (rev 4854)
+++ trunk/scipy/sparse/dok.py	2008-10-27 03:56:22 UTC (rev 4855)
@@ -72,7 +72,7 @@
                 raise TypeError('expected rank <=2 dense array or matrix')
 
             from coo import coo_matrix
-            self.update( coo_matrix(arg1).todok() )
+            self.update( coo_matrix(arg1, dtype=dtype).todok() )
             self.shape = arg1.shape
             self.dtype = arg1.dtype
 

Modified: trunk/scipy/sparse/extract.py
===================================================================
--- trunk/scipy/sparse/extract.py	2008-10-27 03:08:05 UTC (rev 4854)
+++ trunk/scipy/sparse/extract.py	2008-10-27 03:56:22 UTC (rev 4855)
@@ -3,11 +3,43 @@
 
 __docformat__ = "restructuredtext en"
 
-__all__ = ['tril', 'triu']
+__all__ = ['find', 'tril', 'triu']
 
 
 from coo import coo_matrix
 
+def find(A):
+    """Return the indices and values of the nonzero elements of a matrix
+
+    Parameters
+    ----------
+    A : dense or sparse matrix
+        Matrix whose nonzero elements are desired.
+
+    Returns
+    -------
+    (I,J,V) : tuple of arrays
+        I,J, and V contain the row indices, column indices, and values
+        of the nonzero matrix entries.
+
+
+    Example
+    -------
+    >>> from scipy.sparse import csr_matrix
+    >>> A = csr_matrix([[7.0, 8.0, 0],[0, 0, 9.0]])
+    >>> find(A)
+    (array([0, 0, 1], dtype=int32), array([0, 1, 2], dtype=int32), array([ 7.,  8.,  9.]))
+    
+    """
+
+    A = coo_matrix(A).tocsr()  #sums duplicates
+    A.eliminate_zeros()        #removes explicit zeros
+    A = A.tocoo(copy=False)    #(cheaply) convert to COO
+
+    return A.row,A.col,A.data
+
+
+
 def tril(A, k=0, format=None):
     """Return the lower triangular portion of a matrix in sparse format
 

Modified: trunk/scipy/sparse/lil.py
===================================================================
--- trunk/scipy/sparse/lil.py	2008-10-27 03:08:05 UTC (rev 4854)
+++ trunk/scipy/sparse/lil.py	2008-10-27 03:56:22 UTC (rev 4855)
@@ -91,7 +91,7 @@
                 raise TypeError('unsupported matrix type')
             else:
                 from csr import csr_matrix
-                A = csr_matrix(A).tolil()
+                A = csr_matrix(A, dtype=dtype).tolil()
 
                 self.shape = A.shape
                 self.dtype = A.dtype

Modified: trunk/scipy/sparse/tests/test_base.py
===================================================================
--- trunk/scipy/sparse/tests/test_base.py	2008-10-27 03:08:05 UTC (rev 4854)
+++ trunk/scipy/sparse/tests/test_base.py	2008-10-27 03:56:22 UTC (rev 4855)
@@ -151,14 +151,32 @@
     def test_from_array(self):
         A = array([[1,0,0],[2,3,4],[0,5,0],[0,0,0]])
         assert_array_equal(self.spmatrix(A).todense(), A)
+        
+        A = array([[1.0 + 3j,       0,      0],
+                   [       0, 2.0 + 5,      0],
+                   [       0,       0,      0]])
+        assert_array_equal(self.spmatrix(A).todense(), A)
+        assert_array_equal(self.spmatrix(A, dtype='int16').todense(), A.astype('int16'))
 
     def test_from_matrix(self):
         A = matrix([[1,0,0],[2,3,4],[0,5,0],[0,0,0]])
         assert_array_equal(self.spmatrix(A).todense(), A)
+        
+        A = matrix([[1.0 + 3j,       0,      0],
+                    [       0, 2.0 + 5,      0],
+                    [       0,       0,      0]])
+        assert_array_equal(self.spmatrix(A).todense(), A)
+        assert_array_equal(self.spmatrix(A, dtype='int16').todense(), A.astype('int16'))
 
     def test_from_list(self):
         A = [[1,0,0],[2,3,4],[0,5,0],[0,0,0]]
         assert_array_equal(self.spmatrix(A).todense(), A)
+        
+        A = [[1.0 + 3j,       0,      0],
+             [       0, 2.0 + 5,      0],
+             [       0,       0,      0]]
+        assert_array_equal(self.spmatrix(A).todense(), array(A))
+        assert_array_equal(self.spmatrix(A, dtype='int16').todense(), array(A).astype('int16'))
 
     #def test_array(self):
     #    """test array(A) where A is in sparse format"""

Modified: trunk/scipy/sparse/tests/test_extract.py
===================================================================
--- trunk/scipy/sparse/tests/test_extract.py	2008-10-27 03:08:05 UTC (rev 4854)
+++ trunk/scipy/sparse/tests/test_extract.py	2008-10-27 03:56:22 UTC (rev 4855)
@@ -26,6 +26,11 @@
 
         self.cases = cases
 
+    def find(self):
+        for A in self.cases:
+            I,J,V = find(A)
+            assert_equal( A.toarray(), csr_matrix(((I,J),V), shape=A.shape) )
+
     def test_tril(self):
         for A in self.cases:
             B = A.toarray()



More information about the Scipy-svn mailing list