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

scipy-svn at scipy.org scipy-svn at scipy.org
Sun Mar 2 21:47:43 EST 2008


Author: wnbell
Date: 2008-03-02 20:47:39 -0600 (Sun, 02 Mar 2008)
New Revision: 3967

Modified:
   trunk/scipy/sparse/construct.py
   trunk/scipy/sparse/tests/test_construct.py
Log:
added sparse.vstack and sparse.hstack
addresses ticket #602


Modified: trunk/scipy/sparse/construct.py
===================================================================
--- trunk/scipy/sparse/construct.py	2008-03-01 00:44:34 UTC (rev 3966)
+++ trunk/scipy/sparse/construct.py	2008-03-03 02:47:39 UTC (rev 3967)
@@ -1,7 +1,8 @@
-""" Functions to construct sparse matrices
+"""Functions to construct sparse matrices
 """
 
-__all__ = [ 'spdiags', 'eye', 'identity', 'kron', 'kronsum', 'bmat' ]
+__all__ = [ 'spdiags', 'eye', 'identity', 'kron', 'kronsum', 
+            'hstack', 'vstack', 'bmat' ]
 
 
 from itertools import izip
@@ -199,15 +200,63 @@
     return (L+R).asformat(format) #since L + R is not always same format
 
 
+def hstack( blocks, format=None, dtype=None ):
+    """Stack sparse matrices horizontally (column wise)
 
+    Parameters
+    ==========
 
-def bmat( blocks, format=None, dtype=None ):
+    blocks -- sequence of sparse matrices with compatible shapes
+    format -- sparse format of the result (e.g. "csr")
+            -  by default an appropriate sparse matrix format is returned.
+               This choice is subject to change.
+   
+    Example
+    =======
+
+    >>> from scipy.sparse import coo_matrix, vstack
+    >>> A = coo_matrix([[1,2],[3,4]])
+    >>> B = coo_matrix([[5],[6]])
+    >>> hstack( [A,B] ).todense()
+    matrix([[1, 2, 5],
+            [3, 4, 6]])
+
+
     """
-    Build a sparse matrix from sparse sub-blocks
+    return bmat([blocks], format=format, dtype=dtype)
 
+def vstack( blocks, format=None, dtype=None ):
+    """Stack sparse matrices vertically (row wise)
+
     Parameters
     ==========
 
+    blocks -- sequence of sparse matrices with compatible shapes
+    format -- sparse format of the result (e.g. "csr")
+            -  by default an appropriate sparse matrix format is returned.
+               This choice is subject to change.
+   
+    Example
+    =======
+
+    >>> from scipy.sparse import coo_matrix, vstack
+    >>> A = coo_matrix([[1,2],[3,4]])
+    >>> B = coo_matrix([[5,6]])
+    >>> vstack( [A,B] ).todense()
+    matrix([[1, 2],
+            [3, 4],
+            [5, 6]])
+
+
+    """
+    return bmat([ [b] for b in blocks ], format=format, dtype=dtype)
+
+def bmat( blocks, format=None, dtype=None ):
+    """Build a sparse matrix from sparse sub-blocks
+
+    Parameters
+    ==========
+
     blocks -- grid of sparse matrices with compatible shapes
             - an entry of None implies an all-zero matrix
     format -- sparse format of the result (e.g. "csr")
@@ -263,7 +312,7 @@
                 if bcol_lengths[j] == 0:
                     bcol_lengths[j] = A.shape[1]
                 else:
-                    if bcol_lengths[j] != A.shape[0]:
+                    if bcol_lengths[j] != A.shape[1]:
                         raise ValueError('blocks[:,%d] has incompatible column dimensions' % j)
 
 

Modified: trunk/scipy/sparse/tests/test_construct.py
===================================================================
--- trunk/scipy/sparse/tests/test_construct.py	2008-03-01 00:44:34 UTC (rev 3966)
+++ trunk/scipy/sparse/tests/test_construct.py	2008-03-03 02:47:39 UTC (rev 3967)
@@ -124,7 +124,25 @@
                         numpy.kron(b, numpy.eye(len(a)))
                 assert_array_equal(result,expected)
 
+    def test_vstack(self):
 
+        A = coo_matrix([[1,2],[3,4]])
+        B = coo_matrix([[5,6]])
+
+        expected = matrix([[1, 2],
+                           [3, 4],
+                           [5, 6]])
+        assert_equal( vstack( [A,B] ).todense(), expected )
+    
+    def test_hstack(self):
+
+        A = coo_matrix([[1,2],[3,4]])
+        B = coo_matrix([[5],[6]])
+
+        expected = matrix([[1, 2, 5],
+                           [3, 4, 6]])
+        assert_equal( hstack( [A,B] ).todense(), expected )
+
     def test_bmat(self):
 
         A = coo_matrix([[1,2],[3,4]])




More information about the Scipy-svn mailing list