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

scipy-svn at scipy.org scipy-svn at scipy.org
Fri Dec 28 17:02:23 EST 2007


Author: wnbell
Date: 2007-12-28 16:02:11 -0600 (Fri, 28 Dec 2007)
New Revision: 3739

Added:
   trunk/scipy/sparse/tests/test_spfuncs.py
Modified:
   trunk/scipy/sparse/base.py
   trunk/scipy/sparse/bsr.py
   trunk/scipy/sparse/compressed.py
   trunk/scipy/sparse/construct.py
   trunk/scipy/sparse/coo.py
   trunk/scipy/sparse/csc.py
   trunk/scipy/sparse/csr.py
   trunk/scipy/sparse/dia.py
   trunk/scipy/sparse/info.py
   trunk/scipy/sparse/lil.py
   trunk/scipy/sparse/spfuncs.py
   trunk/scipy/sparse/sputils.py
Log:
updated sparse docstrings


Modified: trunk/scipy/sparse/base.py
===================================================================
--- trunk/scipy/sparse/base.py	2007-12-28 20:42:45 UTC (rev 3738)
+++ trunk/scipy/sparse/base.py	2007-12-28 22:02:11 UTC (rev 3739)
@@ -174,13 +174,15 @@
     def asformat(self, format):
         """Return this matrix in a given sparse format
 
-        *Parameters*:
-            format : desired sparse matrix format
-                If format is None then no conversion is performed
-                Other possible values include:
-                    "csr" for csr_matrix format
-                    "csc" for csc_matrix format
-                    "dok" for dok_matrix format and so on
+        Parameters
+        ==========
+            - format : desired sparse matrix format
+              - If format is None then no conversion is performed
+              - Other possible values include:
+                -  "csr" for csr_matrix format
+                -  "csc" for csc_matrix format
+                -  "dok" for dok_matrix format and so on
+
         """
 
         if format is None or format == self.format:
@@ -369,11 +371,12 @@
     def rmatvec(self, other, conjugate=True):
         """Multiplies the vector 'other' by the sparse matrix, returning a
         dense vector as a result.
-
+        
         If 'conjugate' is True:
-            returns A.transpose().conj() * other
+            - returns A.transpose().conj() * other
         Otherwise:
-            returns A.transpose() * other.
+            - returns A.transpose() * other.
+        
         """
         return self.tocsr().rmatvec(other, conjugate=conjugate)
 

Modified: trunk/scipy/sparse/bsr.py
===================================================================
--- trunk/scipy/sparse/bsr.py	2007-12-28 20:42:45 UTC (rev 3738)
+++ trunk/scipy/sparse/bsr.py	2007-12-28 22:02:11 UTC (rev 3739)
@@ -1,5 +1,4 @@
-"""Compressed Block Sparse Row matrix format
-"""
+"""Compressed Block Sparse Row matrix format"""
 
 __all__ = ['bsr_matrix', 'isspmatrix_bsr']
 
@@ -16,44 +15,45 @@
 
     This can be instantiated in several ways:
       - bsr_matrix(D, [blocksize=(R,C)])
-        with a dense matrix or rank-2 ndarray D
+        - with a dense matrix or rank-2 ndarray D
 
       - bsr_matrix(S, [blocksize=(R,C)])
-        with another sparse matrix S (equivalent to S.tocsr())
+        - with another sparse matrix S (equivalent to S.tobsr())
 
       - bsr_matrix((M, N), [blocksize=(R,C), dtype])
-        to construct an empty matrix with shape (M, N)
-        dtype is optional, defaulting to dtype='d'.
+        - to construct an empty matrix with shape (M, N)
+        - dtype is optional, defaulting to dtype='d'.
 
       - bsr_matrix((data, ij), [blocksize=(R,C), shape=(M, N)])
-        where data, ij satisfy:
-            a[ij[0, k], ij[1, k]] = data[k]
+        - where data, ij satisfy:
+          - a[ij[0, k], ij[1, k]] = data[k]
 
       - bsr_matrix((data, indices, indptr), [shape=(M, N)])
-        is the standard BSR representation where:
-            the block column indices for row i are stored in
-                indices[ indptr[i]: indices[i+1] ] 
-            and their corresponding block 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.
+        - is the standard BSR representation where:
+          the block column indices for row i are stored in
+           - indices[ indptr[i]: indices[i+1] ] 
+          and their corresponding block 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.
 
 
-    *Notes*
-    -------
-        The blocksize (R,C) must evenly divide the shape of 
-        the matrix (M,N).  That is, R and C must satisfy the
-        relationship M % R = 0 and N % C = 0.
+    Notes
+    =====
+        
+        - The blocksize (R,C) must evenly divide the shape of 
+          the matrix (M,N).  That is, R and C must satisfy the
+          relationship M % R = 0 and N % C = 0.
     
-        The Block Compressed Row (BSR) format is very similar to the
-        Compressed Sparse Row (CSR) format.  BSR is appropriate for
-        sparse matrices with dense sub matrices like the last example
-        below.  Such matrices often arise, for instance, in finite
-        element discretizations.
+        - The Block Compressed Row (BSR) format is very similar to the
+          Compressed Sparse Row (CSR) format.  BSR is appropriate for
+          sparse matrices with dense sub matrices like the last example
+          below.  Such matrices often arise, for instance, in finite
+          element discretizations.
 
 
-    *Examples*
-    ----------
+    Examples
+    ========
 
     >>> from scipy.sparse import *
     >>> from scipy import *

Modified: trunk/scipy/sparse/compressed.py
===================================================================
--- trunk/scipy/sparse/compressed.py	2007-12-28 20:42:45 UTC (rev 3738)
+++ trunk/scipy/sparse/compressed.py	2007-12-28 22:02:11 UTC (rev 3739)
@@ -116,11 +116,13 @@
     def check_format(self, full_check=True):
         """check whether the matrix format is valid
 
-            *Parameters*:
-                full_check:
-                    True  - rigorous check, O(N) operations : default
-                    False - basic check, O(1) operations
+        Parameters
+        ==========
 
+            - full_check : {bool}
+                - True  - rigorous check, O(N) operations : default
+                - False - basic check, O(1) operations
+
         """
         #use _swap to determine proper bounds
         major_name,minor_name = self._swap(('row','column'))

Modified: trunk/scipy/sparse/construct.py
===================================================================
--- trunk/scipy/sparse/construct.py	2007-12-28 20:42:45 UTC (rev 3738)
+++ trunk/scipy/sparse/construct.py	2007-12-28 22:02:11 UTC (rev 3739)
@@ -24,19 +24,24 @@
 
     B = spdiags(diags, offsets, m, n)
 
-    *Parameters*:
-        data   : matrix whose rows contain the diagonal values
-        diags  : diagonals to set 
-                    k = 0 - the main diagonal
-                    k > 0 - the k-th upper diagonal
-                    k < 0 - the k-th lower diagonal
-        m, n   : dimensions of the result
-        format : format of the result (e.g. "csr")
-                    By default (format=None) an appropriate sparse matrix 
-                    format is returned.  This choice is subject to change.
+    Parameters
+    ==========
+        - data   : matrix whose rows contain the diagonal values
+        - diags  : diagonals to set 
+            - k = 0 - the main diagonal
+            - k > 0 - the k-th upper diagonal
+            - k < 0 - the k-th lower diagonal
+        - m, n   : dimensions of the result
+        - format : format of the result (e.g. "csr")
+            -  By default (format=None) an appropriate sparse matrix 
+               format is returned.  This choice is subject to change.
 
-    *Example*
-    -------
+    See Also
+    ========
+        The dia_matrix class which implements the DIAgonal format.
+
+    Example
+    =======
     >>> data = array([[1,2,3,4]]).repeat(3,axis=0)
     >>> diags = array([0,-1,2])
     >>> spdiags(data,diags,4,4).todense()
@@ -75,16 +80,19 @@
 def spkron(A, B, format=None):
     """kronecker product of sparse matrices A and B
 
-    *Parameters*:
-        A,B : sparse matrices
-            E.g. csr_matrix, csc_matrix, coo_matrix, etc.
+    Parameters
+    ==========
+        A,B    : dense or sparse matrices
+        format : format of the result (e.g. "csr")
+            -  By default (format=None) an appropriate sparse matrix 
+               format is returned.  This choice is subject to change.
 
-    *Returns*:
-        coo_matrix
-            kronecker product in COOrdinate format
+    Returns
+    =======
+        kronecker product in a sparse matrix format
 
-    *Example*:
-    -------
+    Examples
+    ========
 
     >>> A = csr_matrix(array([[0,2],[5,0]]))
     >>> B = csr_matrix(array([[1,2],[3,4]]))
@@ -94,8 +102,14 @@
             [  5.,  10.,   0.,   0.],
             [ 15.,  20.,   0.,   0.]])
 
+    >>> spkron(A,[[1,2],[3,4]]).todense()
+    matrix([[  0.,   0.,   2.,   4.],
+            [  0.,   0.,   6.,   8.],
+            [  5.,  10.,   0.,   0.],
+            [ 15.,  20.,   0.,   0.]])
+
     """
-    #TODO optimize for small dense B and CSR A
+    #TODO optimize for small dense B and CSR A -> BSR
     A,B = coo_matrix(A),coo_matrix(B)
     output_shape = (A.shape[0]*B.shape[0],A.shape[1]*B.shape[1])
 
@@ -130,14 +144,15 @@
     """Generate a lil_matrix of dimensions (r,c) with the k-th
     diagonal set to 1.
 
-    :Parameters:
-        r,c : int
-            Row and column-dimensions of the output.
-        k : int
-            Diagonal offset.  In the output matrix,
-            out[m,m+k] == 1 for all m.
-        dtype : dtype
-            Data-type of the output array.
+    Parameters
+    ==========
+        - r,c : int
+            - row and column-dimensions of the output.
+        - k : int
+            - diagonal offset.  In the output matrix,
+            - out[m,m+k] == 1 for all m.
+        - dtype : dtype
+            - data-type of the output array.
 
     """
     warn("lil_eye is deprecated. use speye(... , format='lil') instead", \
@@ -150,19 +165,20 @@
 def lil_diags(diags,offsets,(m,n),dtype='d'):
     """Generate a lil_matrix with the given diagonals.
 
-    :Parameters:
-        diags : list of list of values e.g. [[1,2,3],[4,5]]
-            Values to be placed on each indicated diagonal.
-        offsets : list of ints
-            Diagonal offsets.  This indicates the diagonal on which
-            the given values should be placed.
-        (r,c) : tuple of ints
-            Row and column dimensions of the output.
-        dtype : dtype
-           Output data-type.
+    Parameters
+    ==========
+        - diags : list of list of values e.g. [[1,2,3],[4,5]]
+            - values to be placed on each indicated diagonal.
+        - offsets : list of ints
+            - diagonal offsets.  This indicates the diagonal on which
+              the given values should be placed.
+        - (r,c) : tuple of ints
+            - row and column dimensions of the output.
+        - dtype : dtype
+            - output data-type.
 
-    Example:
-    -------
+    Example
+    =======
 
     >>> lil_diags([[1,2,3],[4,5],[6]],[0,1,2],(3,3)).todense()
     matrix([[ 1.,  4.,  6.],

Modified: trunk/scipy/sparse/coo.py
===================================================================
--- trunk/scipy/sparse/coo.py	2007-12-28 20:42:45 UTC (rev 3738)
+++ trunk/scipy/sparse/coo.py	2007-12-28 22:02:11 UTC (rev 3739)
@@ -1,4 +1,4 @@
-""" A sparse matrix in COOrdinate format """
+""" A sparse matrix in COOrdinate or 'triplet' format"""
 
 __all__ = ['coo_matrix', 'isspmatrix_coo']
 
@@ -21,34 +21,52 @@
 
     This can be instantiated in several ways:
       - coo_matrix(D)
-        with a dense matrix D
+        - with a dense matrix D
 
       - coo_matrix(S)
-        with another sparse matrix S (equivalent to S.tocoo())
+        - with another sparse matrix S (equivalent to S.tocoo())
 
       - coo_matrix((M, N), [dtype])
-        to construct an empty matrix with shape (M, N)
-        dtype is optional, defaulting to dtype='d'.
+        - 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][:]
+        - 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]
+        - 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.
+    Notes
+    =====
+        Advantages of the COO format
+        ----------------------------
+          - facilitates fast conversion among sparse formats
+          - permits duplicate entries (see example)
+          - faster conversion to CSR/CSC than LIL
+        
+        Disadvantages of the COO format
+        -------------------------------
+          - does not currently support (forces COO->CSR conversion) 
+            - arithmetic operations
+            - slicing
+            - matrix vector products
+        
+        Usage
+        -----
+          - COO is a fast format for constructing sparse matrices
+          - once a matrix has been constructed, convert to CSR or 
+            CSC format for fast arithmetic and matrix vector operations
+          - By default 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. (see example)
 
-    *Examples*
-    ----------
+    Examples
+    ========
 
     >>> from scipy.sparse import *
     >>> from scipy import *

Modified: trunk/scipy/sparse/csc.py
===================================================================
--- trunk/scipy/sparse/csc.py	2007-12-28 20:42:45 UTC (rev 3738)
+++ trunk/scipy/sparse/csc.py	2007-12-28 22:02:11 UTC (rev 3739)
@@ -1,5 +1,4 @@
-"""Compressed Sparse Column matrix format
-"""
+"""Compressed Sparse Column matrix format"""
 
 __all__ = ['csc_matrix', 'isspmatrix_csc']
 
@@ -23,32 +22,45 @@
 
     This can be instantiated in several ways:
       - csc_matrix(D)
-        with a dense matrix or rank-2 ndarray D
+        - with a dense matrix or rank-2 ndarray D
 
       - csc_matrix(S)
-        with another sparse matrix S (equivalent to S.tocsc())
+        - with another sparse matrix S (equivalent to S.tocsc())
 
       - csc_matrix((M, N), [dtype])
-        to construct an empty matrix with shape (M, N)
-        dtype is optional, defaulting to dtype='d'.
+        - to construct an empty matrix with shape (M, N)
+        - dtype is optional, defaulting to dtype='d'.
 
       - csc_matrix((data, ij), [shape=(M, N)])
-        where data, ij satisfy:
-            a[ij[0, k], ij[1, k]] = data[k]
+        - 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.
+         - is the standard 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.
 
+    Notes
+    =====
+        Advantages of the CSC format
+        ----------------------------
+          - efficient arithmetic operations CSC + CSC, CSC * CSC, etc.
+          - efficient column slicing
+          - fast matrix vector products (CSR,BSR may be faster)
+        
+        Disadvantages of the CSC format
+        -------------------------------
+          - slow row slicing operations (prefer CSR)
+          - changes to the sparsity structure are expensive (prefer LIL, DOK)
 
-    *Examples*
-    ----------
 
+    Examples
+    ========
+
     >>> from scipy.sparse import *
     >>> from scipy import *
     >>> csc_matrix( (3,4), dtype='i' ).todense()
@@ -183,9 +195,9 @@
     def get_submatrix( self, slice0, slice1 ):
         """Return a submatrix of this matrix (new matrix is created).
         Contigous range of rows and columns can be selected using:
-        1. a slice object
-        2. a tuple (from, to)
-        3. a scalar for single row/column selection."""
+          1. a slice object
+          2. a tuple (from, to)
+          3. a scalar for single row/column selection."""
         aux = _cs_matrix._get_submatrix( self, self.shape[1], self.shape[0],
                                          slice1, slice0 )
         nr, nc = aux[3:]

Modified: trunk/scipy/sparse/csr.py
===================================================================
--- trunk/scipy/sparse/csr.py	2007-12-28 20:42:45 UTC (rev 3738)
+++ trunk/scipy/sparse/csr.py	2007-12-28 22:02:11 UTC (rev 3739)
@@ -1,5 +1,4 @@
-"""Compressed Sparse Row matrix format
-"""
+"""Compressed Sparse Row matrix format"""
 
 __all__ = ['csr_matrix', 'isspmatrix_csr']
 
@@ -23,32 +22,47 @@
 
     This can be instantiated in several ways:
       - csr_matrix(D)
-        with a dense matrix or rank-2 ndarray D
+        - with a dense matrix or rank-2 ndarray D
 
       - csr_matrix(S)
-        with another sparse matrix S (equivalent to S.tocsr())
+        - with another sparse matrix S (equivalent to S.tocsr())
 
       - csr_matrix((M, N), [dtype])
-        to construct an empty matrix with shape (M, N)
-        dtype is optional, defaulting to dtype='d'.
+        - to construct an empty matrix with shape (M, N)
+        - dtype is optional, defaulting to dtype='d'.
 
       - csr_matrix((data, ij), [shape=(M, N)])
-        where data, ij satisfy:
-            a[ij[0, k], ij[1, k]] = data[k]
+        - 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.
+        - is the standard 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*
-    ----------
+    Notes
+    =====
+        Advantages of the CSR format
+        ----------------------------
+          - efficient arithmetic operations CSR + CSR, CSR * CSR, etc.
+          - efficient row slicing
+          - fast matrix vector products
+        
+        Disadvantages of the CSR format
+        -------------------------------
+          - slow column slicing operations (prefer CSC)
+          - changes to the sparsity structure are expensive (prefer LIL, DOK)
 
+
+
+    Examples
+    ========    
+
     >>> from scipy.sparse import *
     >>> from scipy import *
     >>> csr_matrix( (3,4), dtype='i' ).todense()
@@ -190,10 +204,12 @@
     def get_submatrix( self, slice0, slice1 ):
         """Return a submatrix of this matrix (new matrix is created).
         Contigous range of rows and columns can be selected using:
-        1. a slice object
-        2. a tuple (from, to)
-        3. a scalar for single row/column selection."""
+          1. a slice object
+          2. a tuple (from, to)
+          3. a scalar for single row/column selection.
 
+        """
+
         aux = _cs_matrix._get_submatrix( self, self.shape[0], self.shape[1],
                                          slice0, slice1 )
         nr, nc = aux[3:]

Modified: trunk/scipy/sparse/dia.py
===================================================================
--- trunk/scipy/sparse/dia.py	2007-12-28 20:42:45 UTC (rev 3738)
+++ trunk/scipy/sparse/dia.py	2007-12-28 22:02:11 UTC (rev 3739)
@@ -15,23 +15,24 @@
 
     This can be instantiated in several ways:
       - dia_matrix(D)
-        with a dense matrix
+        - with a dense matrix
 
       - dia_matrix(S)
-        with another sparse matrix S (equivalent to S.todia())
+        - with another sparse matrix S (equivalent to S.todia())
 
       - dia_matrix((M, N), [dtype])
-        to construct an empty matrix with shape (M, N)
-        dtype is optional, defaulting to dtype='d'.
+        - to construct an empty matrix with shape (M, N)
+          dtype is optional, defaulting to dtype='d'.
 
       - dia_matrix((data, diags), shape=(M, N))
-        where the data[k,:] stores the diagonal entries for
-        diagonal diag[k] (See example below)
+        - where the data[k,:] stores the diagonal entries for
+          diagonal diag[k] (See example below)
 
 
-    *Examples*
-    ----------
+    Examples
+    ========
 
+
     >>> from scipy.sparse import *
     >>> from scipy import *
     >>> dia_matrix( (3,4), dtype='i').todense()

Modified: trunk/scipy/sparse/info.py
===================================================================
--- trunk/scipy/sparse/info.py	2007-12-28 20:42:45 UTC (rev 3738)
+++ trunk/scipy/sparse/info.py	2007-12-28 22:02:11 UTC (rev 3739)
@@ -7,12 +7,14 @@
 Original code by Travis Oliphant.
 Modified and extended by Ed Schofield, Robert Cimrman, and Nathan Bell.
 
-There are five available sparse matrix types:
-    (1) csc_matrix: Compressed Sparse Column format
-    (2) csr_matrix: Compressed Sparse Row format
-    (3) lil_matrix: List of Lists format
-    (4) dok_matrix: Dictionary of Keys format
-    (5) coo_matrix: COOrdinate format (aka IJV, triplet format)
+There are seven available sparse matrix types:
+    1. csc_matrix: Compressed Sparse Column format
+    2. csr_matrix: Compressed Sparse Row format
+    3. bsr_matrix: Block Sparse Row format
+    4. lil_matrix: List of Lists format
+    5. dok_matrix: Dictionary of Keys format
+    6. coo_matrix: COOrdinate format (aka IJV, triplet format)
+    7. dig_matrix: DIAgonal format
 
 To construct a matrix efficiently, use either lil_matrix (recommended) or
 dok_matrix. The lil_matrix class supports basic slicing and fancy
@@ -27,8 +29,10 @@
 All conversions among the CSR, CSC, and COO formats are efficient,
 linear-time operations.
 
-Example:
+Example
+=======
     Construct a 1000x1000 lil_matrix and add some values to it:
+
     >>> from scipy import sparse, linsolve
     >>> from numpy import linalg
     >>> from numpy.random import rand
@@ -38,22 +42,28 @@
     >>> A.setdiag(rand(1000))
 
     Now convert it to CSR format and solve (A A^T) x = b for x:
+
     >>> A = A.tocsr()
     >>> b = rand(1000)
     >>> x = linsolve.spsolve(A * A.T, b)
 
     Convert it to a dense matrix and solve, and check that the result
     is the same:
+
     >>> A_ = A.todense()
     >>> x_ = linalg.solve(A_ * A_.T, b)
     >>> err = linalg.norm(x-x_)
 
     Now we can print the error norm with:
-        print "Norm error =", err
+
+    >>> print "Norm error =", err
+
     It should be small :)
 
-Example:
+Example
+=======
     Construct a matrix in COO format:
+
     >>> from scipy import sparse
     >>> from numpy import array
     >>> I = array([0,3,1,0])
@@ -64,6 +74,7 @@
     Notice that the indices do not need to be sorted.
 
     Duplicate (i,j) entries are summed when converting to CSR or CSC.
+
     >>> I = array([0,0,1,3,1,0,0])
     >>> J = array([0,2,1,3,1,0,0])
     >>> V = array([1,1,1,1,1,1,1])
@@ -72,13 +83,13 @@
     This is useful for constructing finite-element stiffness and
     mass matrices.
 
-Further Details:
+Further Details
+===============
+
     CSR column indices are not necessarily sorted.  Likewise for CSC row
     indices.  Use the .sorted_indices() and .sort_indices() methods when 
-    sorted indices are necessary.  Note that there is no expectation for 
-    sorted indices in the sparsetools module.  Furthermore some sparsetools 
-    functions produce matrices with unsorted indices even when sorted 
-    input is given.
+    sorted indices are required (e.g. when passing data to other libraries). 
+
 """
 
 postpone_import = 1

Modified: trunk/scipy/sparse/lil.py
===================================================================
--- trunk/scipy/sparse/lil.py	2007-12-28 20:42:45 UTC (rev 3738)
+++ trunk/scipy/sparse/lil.py	2007-12-28 22:02:11 UTC (rev 3739)
@@ -21,6 +21,27 @@
     This contains a list (self.rows) of rows, each of which is a sorted
     list of column indices of non-zero elements. It also contains a list
     (self.data) of lists of these elements.
+
+    Notes
+    =====
+        Advantages of the LIL format
+        ----------------------------
+          - supports flexible slicing
+          - changes to the matrix sparsity structure are efficient
+        
+        Disadvantages of the LIL format
+        -------------------------------
+          - arithmetic operations LIL + LIL are slower than CSR/CSC
+          - slow column slicing
+          - matrix vector products are slower than CSR/CSC
+        
+        Usage
+        -----
+          - LIL is a convenient format for constructing sparse matrices 
+          - once a matrix has been constructed, convert to CSR or 
+            CSC format for fast arithmetic and matrix vector operations
+          - consider using the COO format when constructing large matrices
+        
     """
 
     def __init__(self, A=None, shape=None, dtype=None, copy=False):

Modified: trunk/scipy/sparse/spfuncs.py
===================================================================
--- trunk/scipy/sparse/spfuncs.py	2007-12-28 20:42:45 UTC (rev 3738)
+++ trunk/scipy/sparse/spfuncs.py	2007-12-28 22:02:11 UTC (rev 3739)
@@ -14,9 +14,7 @@
 from sparsetools import csr_count_blocks
 
 def extract_diagonal(A):
-    """
-    extract_diagonal(A) returns the main diagonal of A.
-    """
+    """extract_diagonal(A) returns the main diagonal of A."""
     #TODO extract k-th diagonal
     if isspmatrix_csr(A) or isspmatrix_csc(A):
         fn = getattr(sparsetools, A.format + "_diagonal")
@@ -33,7 +31,7 @@
     """Attempt to determine the blocksize of a sparse matrix
 
     Returns a blocksize=(r,c) such that
-        A.nnz / A.tobsr( (r,c) ).nnz > efficiency
+        - A.nnz / A.tobsr( (r,c) ).nnz > efficiency
     """
     if not (isspmatrix_csr(A) or isspmatrix_csc(A)):
         A = csr_matrix(A)

Modified: trunk/scipy/sparse/sputils.py
===================================================================
--- trunk/scipy/sparse/sputils.py	2007-12-28 20:42:45 UTC (rev 3738)
+++ trunk/scipy/sparse/sputils.py	2007-12-28 22:02:11 UTC (rev 3739)
@@ -17,8 +17,8 @@
 
     upcast(t0, t1, ..., tn) -> T  where T is a supported dtype
 
-    *Example*
-    -------
+    Example
+    =======
 
     >>> upcast('int32')
     <type 'numpy.int32'>

Added: trunk/scipy/sparse/tests/test_spfuncs.py
===================================================================
--- trunk/scipy/sparse/tests/test_spfuncs.py	2007-12-28 20:42:45 UTC (rev 3738)
+++ trunk/scipy/sparse/tests/test_spfuncs.py	2007-12-28 22:02:11 UTC (rev 3739)
@@ -0,0 +1,64 @@
+from numpy import array, kron
+from numpy.testing import *
+
+set_package_path()
+from scipy.sparse.spfuncs import *
+from scipy.sparse import csr_matrix, csc_matrix
+restore_path()
+
+class TestSparseFunctions(NumpyTestCase):
+    def check_estimate_blocksize(self):
+
+        mats = []
+        mats.append( [[0,1],[1,0]] )
+        mats.append( [[1,1,0],[0,0,1],[1,0,1]] )
+        mats.append( [[0],[0],[1]] )
+        mats = [array(x) for x in mats]
+    
+        blks = []
+        blks.append( [[1]] )
+        blks.append( [[1,1],[1,1]] )
+        blks.append( [[1,1],[0,1]] )
+        blks.append( [[1,1,0],[1,0,1],[1,1,1]] )
+        blks = [array(x) for x in blks]
+
+        for A in mats:
+            for B in blks:
+                X = kron(A,B)
+                r,c = estimate_blocksize(X)
+                assert(r >= B.shape[0])
+                assert(c >= B.shape[1])
+
+    def check_count_blocks(self):
+        def gold(A,bs):
+            R,C = bs
+            I,J = A.nonzero()
+            return len( set( zip(I/R,J/C) ) )
+        
+        mats = []
+        mats.append( [[0]] ) 
+        mats.append( [[1]] ) 
+        mats.append( [[1,0]] ) 
+        mats.append( [[1,1]] ) 
+        mats.append( [[0,1],[1,0]] )
+        mats.append( [[1,1,0],[0,0,1],[1,0,1]] )
+        mats.append( [[0],[0],[1]] )
+
+        for A in mats:
+            for B in mats:
+                X = kron(A,B)
+                Y = csr_matrix(X)
+                for R in range(1,6):
+                    for C in range(1,6):
+                        assert_equal(count_blocks(Y,(R,C)),gold(X,(R,C)))
+        
+        X = kron([[1,1,0],[0,0,1],[1,0,1]],[[1,1]])
+        Y = csc_matrix(X)
+        assert_equal(count_blocks(X,(1,2)),gold(X,(1,2)))
+        assert_equal(count_blocks(Y,(1,2)),gold(X,(1,2)))
+
+
+
+if __name__ == "__main__":
+    NumpyTest().run()
+




More information about the Scipy-svn mailing list