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

scipy-svn at scipy.org scipy-svn at scipy.org
Sun Dec 16 17:09:30 EST 2007


Author: wnbell
Date: 2007-12-16 16:08:50 -0600 (Sun, 16 Dec 2007)
New Revision: 3676

Modified:
   trunk/scipy/sparse/coo.py
   trunk/scipy/sparse/csc.py
   trunk/scipy/sparse/csr.py
Log:
updated some sparse docstrings


Modified: trunk/scipy/sparse/coo.py
===================================================================
--- trunk/scipy/sparse/coo.py	2007-12-16 04:35:42 UTC (rev 3675)
+++ trunk/scipy/sparse/coo.py	2007-12-16 22:08:50 UTC (rev 3676)
@@ -12,34 +12,68 @@
 from sputils import upcast, to_native, isshape, getdtype
 
 class coo_matrix(spmatrix):
-    """ A sparse matrix in coordinate list format.
+    """A sparse matrix in COOrdinate format.
+    Also known as the 'ijv' or 'triplet' format.
 
-    COO matrices are created either as:
-        A = coo_matrix( (m, n), [dtype])
-    for a zero matrix, or as:
-        A = coo_matrix(S)
-    where S is a sparse matrix, or as:
-        A = coo_matrix(M)
-    where M is a dense matrix or rank 2 ndarray, or as:
-        A = coo_matrix((obj, ij), [shape])
-    where the dimensions are optional.  If supplied, we set (M, N) = shape.
-    If not supplied, we infer these from the index arrays:
-        ij[0][:] and ij[1][:]
+    This can be instantiated in several ways:
+      - coo_matrix(D)
+        with a dense matrix D
 
-    The arguments 'obj' and 'ij' represent three arrays:
-        1. obj[:]    the entries of the matrix, in any order
-        2. ij[0][:]  the row indices of the matrix entries
-        3. ij[1][:]  the column indices of the matrix entries
+      - coo_matrix(S)
+        with another sparse matrix S (equivalent to S.tocoo())
 
-    So the following holds:
-        A[ij[0][k], ij[1][k] = obj[k]
+      - coo_matrix((M, N), [dtype])
+        to construct an empty matrix with shape (M, N)
+        dtype is optional, defaulting to dtype='d'.
 
+      - coo_matrix((data, ij), [shape=(M, N)])
+        When shape is not specified, it is inferred from the index arrays:
+            ij[0][:] and ij[1][:]
+
+        The arguments 'data' and 'ij' represent three arrays:
+            1. data[:]   the entries of the matrix, in any order
+            2. ij[0][:]  the row indices of the matrix entries
+            3. ij[1][:]  the column indices of the matrix entries
+    
+        So the following holds:
+            A[ij[0][k], ij[1][k] = data[k]
+
     Note:
         When converting to CSR or CSC format, duplicate (i,j) entries
         will be summed together.  This facilitates efficient construction
         of finite element matrices and the like.
 
+    *Examples*
+    ----------
+
+    >>> from scipy.sparse import *
+    >>> from scipy import *
+    >>> coo_matrix( (3,4), dtype='i' ).todense()
+    matrix([[0, 0, 0, 0],
+            [0, 0, 0, 0],
+            [0, 0, 0, 0]])
+
+    >>> row  = array([0,3,1,0])
+    >>> col  = array([0,3,1,2])
+    >>> data = array([4,5,7,9])
+    >>> coo_matrix( (data,(row,col)), shape=(4,4) ).todense()
+    matrix([[4, 0, 9, 0],
+            [0, 7, 0, 0],
+            [0, 0, 0, 0],
+            [0, 0, 0, 5]])
+
+    >>> print "example with duplicates"
+    >>> row  = array([0,0,1,3,1,0,0])
+    >>> col  = array([0,2,1,3,1,0,0])
+    >>> data = array([1,1,1,1,1,1,1])
+    >>> coo_matrix( (data,(row,col)), shape=(4,4)).todense()
+    matrix([[3, 0, 1, 0],
+            [0, 2, 0, 0],
+            [0, 0, 0, 0],
+            [0, 0, 0, 1]])
+    
     """
+
     def __init__(self, arg1, shape=None, dtype=None, copy=False, dims=None):
         spmatrix.__init__(self)
 

Modified: trunk/scipy/sparse/csc.py
===================================================================
--- trunk/scipy/sparse/csc.py	2007-12-16 04:35:42 UTC (rev 3675)
+++ trunk/scipy/sparse/csc.py	2007-12-16 22:08:50 UTC (rev 3676)
@@ -18,26 +18,61 @@
 
 
 class csc_matrix(_cs_matrix):
-    """ Compressed sparse column matrix
-        This can be instantiated in several ways:
-          - csc_matrix(d)
-            with a dense matrix d
+    """Compressed Sparse Column matrix
 
-          - csc_matrix(s)
-            with another sparse matrix s (sugar for .tocsc())
+    This can be instantiated in several ways:
+      - csc_matrix(D)
+        with a dense matrix or rank-2 ndarray D
 
-          - csc_matrix((M, N), [dtype])
-            to construct a container, where (M, N) are dimensions and
-            dtype is optional, defaulting to dtype='d'.
+      - csc_matrix(S)
+        with another sparse matrix S (equivalent to S.tocsc())
 
-          - csc_matrix((data, ij), [(M, N)])
-            where data, ij satisfy:
-                a[ij[0, k], ij[1, k]] = data[k]
+      - csc_matrix((M, N), [dtype])
+        to construct an empty matrix with shape (M, N)
+        dtype is optional, defaulting to dtype='d'.
 
-          - csc_matrix((data, row, ptr), [(M, N)])
-            standard CSC representation
+      - csc_matrix((data, ij), [shape=(M, N)])
+        where data, ij satisfy:
+            a[ij[0, k], ij[1, k]] = data[k]
+
+      - csc_matrix((data, indices, indptr), [shape=(M, N)])
+        is the native CSC representation where:
+            the row indices for column i are stored in
+                indices[ indptr[i]: indices[i+1] ] 
+            and their corresponding values are stored in
+                data[ indptr[i]: indptr[i+1] ]
+        If the shape parameter is not supplied, the matrix dimensions
+        are inferred from the index arrays.
+
+
+    *Examples*
+    ----------
+
+    >>> from scipy.sparse import *
+    >>> from scipy import *
+    >>> csc_matrix( (3,4), dtype='i' ).todense()
+    matrix([[0, 0, 0, 0],
+            [0, 0, 0, 0],
+            [0, 0, 0, 0]])
+
+    >>> row = array([0,0,1,2,2,2])
+    >>> col = array([0,2,2,0,1,2])
+    >>> data = array([1,2,3,4,5,6])
+    >>> csc_matrix( (data,(row,col)), shape=(3,3) ).todense()
+    matrix([[1, 0, 2],
+            [0, 0, 3],
+            [4, 5, 6]])
+
+    >>> indptr = array([0,2,3,6])
+    >>> indices = array([0,2,2,0,1,2])
+    >>> data = array([1,4,6,2,3,5])
+    >>> csc_matrix( (data,indices,indptr), shape=(3,3) ).todense()
+    matrix([[1, 0, 2],
+            [0, 0, 3],
+            [4, 5, 6]])
+
     """
-    
+
     def __getattr__(self, attr):
         if attr == 'rowind':
             warn("rowind attribute no longer in use. Use .indices instead",

Modified: trunk/scipy/sparse/csr.py
===================================================================
--- trunk/scipy/sparse/csr.py	2007-12-16 04:35:42 UTC (rev 3675)
+++ trunk/scipy/sparse/csr.py	2007-12-16 22:08:50 UTC (rev 3676)
@@ -18,26 +18,65 @@
 from compressed import _cs_matrix,resize1d
 
 class csr_matrix(_cs_matrix):
-    """ Compressed sparse row matrix
-        This can be instantiated in several ways:
-          - csr_matrix(d)
-            with a dense matrix d
+    """Compressed Sparse Row matrix
 
-          - csr_matrix(s)
-            with another sparse matrix s (sugar for .tocsr())
+    This can be instantiated in several ways:
+      - csr_matrix(D)
+        with a dense matrix or rank-2 ndarray D
 
-          - csr_matrix((M, N), [dtype])
-            to construct a container, where (M, N) are dimensions and
-            dtype is optional, defaulting to dtype='d'.
+      - csr_matrix(S)
+        with another sparse matrix S (equivalent to S.tocsr())
 
-          - csr_matrix((data, ij), [shape=(M, N)])
-            where data, ij satisfy:
-                a[ij[0, k], ij[1, k]] = data[k]
+      - csr_matrix((M, N), [dtype])
+        to construct an empty matrix with shape (M, N)
+        dtype is optional, defaulting to dtype='d'.
 
-          - csr_matrix((data, col, ptr), [shape=(M, N)])
-            standard CSR representation
+      - csr_matrix((data, ij), [shape=(M, N)])
+        where data, ij satisfy:
+            a[ij[0, k], ij[1, k]] = data[k]
+
+      - csr_matrix((data, indices, indptr), [shape=(M, N)])
+        is the native CSR representation where:
+            the column indices for row i are stored in
+                indices[ indptr[i]: indices[i+1] ] 
+            and their corresponding values are stored in
+                data[ indptr[i]: indptr[i+1] ]
+        If the shape parameter is not supplied, the matrix dimensions
+        are inferred from the index arrays.
+
+
+    *Examples*
+    ----------
+
+    >>> from scipy.sparse import *
+    >>> from scipy import *
+    >>> csr_matrix( (3,4), dtype='i' ).todense()
+    matrix([[0, 0, 0, 0],
+            [0, 0, 0, 0],
+            [0, 0, 0, 0]])
+
+    >>> row = array([0,0,1,2,2,2])
+    >>> col = array([0,2,2,0,1,2])
+    >>> data = array([1,2,3,4,5,6])
+    >>> csr_matrix( (data,(row,col)), shape=(3,3) ).todense()
+    matrix([[1, 0, 2],
+            [0, 0, 3],
+            [4, 5, 6]])
+    
+    >>> indptr = array([0,2,3,6])
+    >>> indices = array([0,2,2,0,1,2])
+    >>> data = array([1,2,3,4,5,6])
+    >>> csr_matrix( (data,indices,indptr), shape=(3,3) ).todense()
+    matrix([[1, 0, 2],
+            [0, 0, 3],
+            [4, 5, 6]])
+
     """
 
+
+
+
+
     def __getattr__(self, attr):
         if attr == 'colind':
             warn("colind attribute no longer in use. Use .indices instead",




More information about the Scipy-svn mailing list