[Scipy-svn] r3999 - trunk/scipy/sparse
scipy-svn at scipy.org
scipy-svn at scipy.org
Thu Mar 6 19:34:16 EST 2008
Author: wnbell
Date: 2008-03-06 18:33:56 -0600 (Thu, 06 Mar 2008)
New Revision: 3999
Modified:
trunk/scipy/sparse/bsr.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/dok.py
trunk/scipy/sparse/info.py
trunk/scipy/sparse/lil.py
trunk/scipy/sparse/sputils.py
Log:
updated sparse docstrings
Modified: trunk/scipy/sparse/bsr.py
===================================================================
--- trunk/scipy/sparse/bsr.py 2008-03-05 14:38:29 UTC (rev 3998)
+++ trunk/scipy/sparse/bsr.py 2008-03-07 00:33:56 UTC (rev 3999)
@@ -1,5 +1,7 @@
"""Compressed Block Sparse Row matrix format"""
+__docformat__ = "restructuredtext en"
+
__all__ = ['bsr_matrix', 'isspmatrix_bsr']
from warnings import warn
@@ -20,46 +22,49 @@
"""Block Sparse Row matrix
This can be instantiated in several ways:
- - bsr_matrix(D, [blocksize=(R,C)])
- - with a dense matrix or rank-2 ndarray D
+ bsr_matrix(D, [blocksize=(R,C)])
+ with a dense matrix or rank-2 ndarray D
- - bsr_matrix(S, [blocksize=(R,C)])
- - with another sparse matrix S (equivalent to S.tobsr())
+ bsr_matrix(S, [blocksize=(R,C)])
+ 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'.
+ bsr_matrix((M, N), [blocksize=(R,C), dtype])
+ 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]
+ bsr_matrix((data, ij), [blocksize=(R,C), shape=(M, N)])
+ where ``data`` and ``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.
+ 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.
+ Notes
+ -----
- 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.
-
+ Summary
- 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 vector-valued
- finite element discretizations.
+ below. Block matrices often arise in vector-valued finite
+ element discretizations. In such cases, BSR is considerably
+ more efficient than CSR and CSC for many sparse arithmetic
+ operations.
+ Blocksize
+ - 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.
+ - If no blocksize is specified, a simple heuristic is applied
+ to determine an appropriate blocksize.
+
+
Examples
- ========
+ --------
>>> from scipy.sparse import *
>>> from scipy import *
Modified: trunk/scipy/sparse/construct.py
===================================================================
--- trunk/scipy/sparse/construct.py 2008-03-05 14:38:29 UTC (rev 3998)
+++ trunk/scipy/sparse/construct.py 2008-03-07 00:33:56 UTC (rev 3999)
@@ -1,6 +1,8 @@
"""Functions to construct sparse matrices
"""
+__docformat__ = "restructuredtext en"
+
__all__ = [ 'spdiags', 'eye', 'identity', 'kron', 'kronsum',
'hstack', 'vstack', 'bmat' ]
@@ -27,10 +29,8 @@
def spdiags(data, diags, m, n, format=None):
"""Return a sparse matrix given its diagonals.
- B = spdiags(diags, offsets, m, n)
-
Parameters
- ==========
+ ----------
- data : matrix whose rows contain the diagonal values
- diags : diagonals to set
- k = 0 - the main diagonal
@@ -42,11 +42,12 @@
format is returned. This choice is subject to change.
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()
@@ -85,18 +86,17 @@
"""kronecker product of sparse matrices A and B
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.
+ ----------
+ A,B : dense or sparse matrices
+ format : format of the result (e.g. "csr")
Returns
- =======
- kronecker product in a sparse matrix format
+ -------
+ kronecker product in a sparse matrix format
+
Examples
- ========
+ --------
>>> A = csr_matrix(array([[0,2],[5,0]]))
>>> B = csr_matrix(array([[1,2],[3,4]]))
@@ -168,11 +168,9 @@
of shape (m,m) and (n,n) respectively.
Parameters
- ==========
- A,B : squared 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.
+ ----------
+ A,B : square dense or sparse matrices
+ format : format of the result (e.g. "csr")
Returns
=======
@@ -204,15 +202,16 @@
"""Stack sparse matrices horizontally (column 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.
+ 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]])
@@ -229,15 +228,16 @@
"""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.
+ 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]])
@@ -255,17 +255,17 @@
"""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")
- - by default an appropriate sparse matrix format is returned.
- This choice is subject to change.
-
+ 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")
+ by default an appropriate sparse matrix format is returned.
+ This choice is subject to change.
Example
- =======
+ -------
>>> from scipy.sparse import coo_matrix, bmat
>>> A = coo_matrix([[1,2],[3,4]])
@@ -370,14 +370,15 @@
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.
+ ----------
+
+ 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." \
@@ -392,19 +393,19 @@
"""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.
+ ----------
+ 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
- =======
+ -------
>>> 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 2008-03-05 14:38:29 UTC (rev 3998)
+++ trunk/scipy/sparse/coo.py 2008-03-07 00:33:56 UTC (rev 3999)
@@ -1,5 +1,7 @@
""" A sparse matrix in COOrdinate or 'triplet' format"""
+__docformat__ = "restructuredtext en"
+
__all__ = ['coo_matrix', 'isspmatrix_coo']
from itertools import izip
@@ -17,56 +19,58 @@
class coo_matrix(_data_matrix):
"""A sparse matrix in COOrdinate format.
+
Also known as the 'ijv' or 'triplet' format.
This can be instantiated in several ways:
- - coo_matrix(D)
- - with a dense matrix D
+ coo_matrix(D)
+ with a dense matrix D
- - coo_matrix(S)
- - with another sparse matrix S (equivalent to S.tocoo())
+ coo_matrix(S)
+ 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'.
+ 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][:]
+ coo_matrix((data, ij), [shape=(M, N)])
+ 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
- - 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]
+ Where ``A[ij[0][k], ij[1][k] = data[k]``. When shape is
+ not specified, it is inferred from the index arrays
+
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)
+ -----
+ Advantages of the COO format
+ - facilitates fast conversion among sparse formats
+ - permits duplicate entries (see example)
+ - very fast conversion to and from CSR/CSC formats
+
+ Disadvantages of the COO format
+ - does not directly support:
+ + arithmetic operations
+ + slicing
+ + matrix vector products
+
+
+ Intended 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
- ========
+ --------
>>> from scipy.sparse import *
>>> from scipy import *
Modified: trunk/scipy/sparse/csc.py
===================================================================
--- trunk/scipy/sparse/csc.py 2008-03-05 14:38:29 UTC (rev 3998)
+++ trunk/scipy/sparse/csc.py 2008-03-07 00:33:56 UTC (rev 3999)
@@ -1,5 +1,7 @@
"""Compressed Sparse Column matrix format"""
+__docformat__ = "restructuredtext en"
+
__all__ = ['csc_matrix', 'isspmatrix_csc']
from warnings import warn
@@ -21,41 +23,37 @@
"""Compressed Sparse Column matrix
This can be instantiated in several ways:
- - csc_matrix(D)
- - with a dense matrix or rank-2 ndarray D
+ csc_matrix(D)
+ with a dense matrix or rank-2 ndarray D
- - csc_matrix(S)
- - with another sparse matrix S (equivalent to S.tocsc())
+ csc_matrix(S)
+ 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'.
+ csc_matrix((M, N), [dtype])
+ 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]
+ csc_matrix((data, ij), [shape=(M, N)])
+ where ``data`` and ``ij`` satisfy ``a[ij[0, k], ij[1, k]] = data[k]``
- - csc_matrix((data, indices, indptr), [shape=(M, N)])
- - 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.
+ csc_matrix((data, indices, indptr), [shape=(M, N)])
+ 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)
+ -----
+ 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 (consider CSR)
+ - changes to the sparsity structure are expensive (consider LIL or DOK)
Examples
Modified: trunk/scipy/sparse/csr.py
===================================================================
--- trunk/scipy/sparse/csr.py 2008-03-05 14:38:29 UTC (rev 3998)
+++ trunk/scipy/sparse/csr.py 2008-03-07 00:33:56 UTC (rev 3999)
@@ -1,5 +1,7 @@
"""Compressed Sparse Row matrix format"""
+__docformat__ = "restructuredtext en"
+
__all__ = ['csr_matrix', 'isspmatrix_csr']
@@ -23,47 +25,39 @@
"""Compressed Sparse Row matrix
This can be instantiated in several ways:
- - csr_matrix(D)
- - with a dense matrix or rank-2 ndarray D
+ csr_matrix(D)
+ with a dense matrix or rank-2 ndarray D
- - csr_matrix(S)
- - with another sparse matrix S (equivalent to S.tocsr())
+ csr_matrix(S)
+ 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'.
+ csr_matrix((M, N), [dtype])
+ 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]
+ csr_matrix((data, ij), [shape=(M, N)])
+ where ``data`` and ``ij`` satisfy ``a[ij[0, k], ij[1, k]] = data[k]``
- - csr_matrix((data, indices, indptr), [shape=(M, N)])
- - 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.
+ csr_matrix((data, indices, indptr), [shape=(M, N)])
+ 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.
-
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)
+ -----
+ 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 (consider CSC)
+ - changes to the sparsity structure are expensive (consider LIL or DOK)
-
-
Examples
- ========
+ --------
>>> from scipy.sparse import *
>>> from scipy import *
Modified: trunk/scipy/sparse/dia.py
===================================================================
--- trunk/scipy/sparse/dia.py 2008-03-05 14:38:29 UTC (rev 3998)
+++ trunk/scipy/sparse/dia.py 2008-03-07 00:33:56 UTC (rev 3999)
@@ -1,5 +1,7 @@
"""Sparse DIAgonal format"""
+__docformat__ = "restructuredtext en"
+
__all__ = ['dia_matrix','isspmatrix_dia']
from numpy import asarray, asmatrix, matrix, zeros, arange, array, \
@@ -14,23 +16,23 @@
"""Sparse matrix with DIAgonal storage
This can be instantiated in several ways:
- - dia_matrix(D)
- - with a dense matrix
+ dia_matrix(D)
+ with a dense matrix
- - dia_matrix(S)
- - with another sparse matrix S (equivalent to S.todia())
+ dia_matrix(S)
+ 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'.
+ dia_matrix((M, N), [dtype])
+ 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)
+ dia_matrix((data, diags), shape=(M, N))
+ where the ``data[k,:]`` stores the diagonal entries for
+ diagonal ``diag[k]`` (See example below)
Examples
- ========
+ --------
>>> from scipy.sparse import *
>>> from scipy import *
Modified: trunk/scipy/sparse/dok.py
===================================================================
--- trunk/scipy/sparse/dok.py 2008-03-05 14:38:29 UTC (rev 3998)
+++ trunk/scipy/sparse/dok.py 2008-03-07 00:33:56 UTC (rev 3999)
@@ -1,5 +1,7 @@
"""Dictionary Of Keys based matrix"""
+__docformat__ = "restructuredtext en"
+
__all__ = ['dok_matrix', 'isspmatrix_dok']
import operator
Modified: trunk/scipy/sparse/info.py
===================================================================
--- trunk/scipy/sparse/info.py 2008-03-05 14:38:29 UTC (rev 3998)
+++ trunk/scipy/sparse/info.py 2008-03-07 00:33:56 UTC (rev 3999)
@@ -1,6 +1,6 @@
"""
-Sparse matrix
-=============
+Sparse Matrices
+---------------
Scipy 2D sparse matrix module.
@@ -14,7 +14,7 @@
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
+ 7. dia_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
@@ -29,67 +29,70 @@
All conversions among the CSR, CSC, and COO formats are efficient,
linear-time operations.
-Example
-=======
- Construct a 1000x1000 lil_matrix and add some values to it:
+Example 1
+---------
+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
- >>> A = sparse.lil_matrix((1000, 1000))
- >>> A[0, :100] = rand(100)
- >>> A[1, 100:200] = A[0, :100]
- >>> A.setdiag(rand(1000))
+>>> from scipy import sparse, linsolve
+>>> from numpy import linalg
+>>> from numpy.random import rand
+>>> A = sparse.lil_matrix((1000, 1000))
+>>> A[0, :100] = rand(100)
+>>> A[1, 100:200] = A[0, :100]
+>>> A.setdiag(rand(1000))
- Now convert it to CSR format and solve A x = b for x:
+Now convert it to CSR format and solve A x = b for x:
- >>> A = A.tocsr()
- >>> b = rand(1000)
- >>> x = linsolve.spsolve(A, b)
+>>> A = A.tocsr()
+>>> b = rand(1000)
+>>> x = linsolve.spsolve(A, b)
- Convert it to a dense matrix and solve, and check that the result
- is the same:
+Convert it to a dense matrix and solve, and check that the result
+is the same:
- >>> x_ = linalg.solve(A.todense(), b)
+>>> x_ = linalg.solve(A.todense(), b)
- Now we can compute norm of the error with:
+Now we can compute norm of the error with:
- >>> err = linalg.norm(x-x_)
- >>> err < 1e-10
- True
+>>> err = linalg.norm(x-x_)
+>>> err < 1e-10
+True
- It should be small :)
+It should be small :)
-Example
-=======
- Construct a matrix in COO format:
- >>> from scipy import sparse
- >>> from numpy import array
- >>> I = array([0,3,1,0])
- >>> J = array([0,3,1,2])
- >>> V = array([4,5,7,9])
- >>> A = sparse.coo_matrix((V,(I,J)),shape=(4,4))
+Example 2
+---------
- Notice that the indices do not need to be sorted.
+Construct a matrix in COO format:
- Duplicate (i,j) entries are summed when converting to CSR or CSC.
+>>> from scipy import sparse
+>>> from numpy import array
+>>> I = array([0,3,1,0])
+>>> J = array([0,3,1,2])
+>>> V = array([4,5,7,9])
+>>> A = sparse.coo_matrix((V,(I,J)),shape=(4,4))
- >>> 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])
- >>> B = sparse.coo_matrix((V,(I,J)),shape=(4,4)).tocsr()
+Notice that the indices do not need to be sorted.
- This is useful for constructing finite-element stiffness and
- mass matrices.
+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])
+>>> B = sparse.coo_matrix((V,(I,J)),shape=(4,4)).tocsr()
+
+This is useful for constructing finite-element stiffness and mass matrices.
+
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 required (e.g. when passing data to other libraries).
+CSR column indices are not necessarily sorted. Likewise for CSC row
+indices. Use the .sorted_indices() and .sort_indices() methods when
+sorted indices are required (e.g. when passing data to other libraries).
"""
+__docformat__ = "restructuredtext en"
+
postpone_import = 1
Modified: trunk/scipy/sparse/lil.py
===================================================================
--- trunk/scipy/sparse/lil.py 2008-03-05 14:38:29 UTC (rev 3998)
+++ trunk/scipy/sparse/lil.py 2008-03-07 00:33:56 UTC (rev 3999)
@@ -1,8 +1,8 @@
"""LInked List sparse matrix class
-
-Original code by Ed Schofield.
"""
+__docformat__ = "restructuredtext en"
+
__all__ = ['lil_matrix','isspmatrix_lil']
import copy
@@ -16,32 +16,48 @@
from sputils import getdtype,isshape,issequence,isscalarlike
class lil_matrix(spmatrix):
- """Row-based linked list matrix,
+ """Row-based linked list matrix
- 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.
+
+ This can be instantiated in several ways:
+ csc_matrix(D)
+ with a dense matrix or rank-2 ndarray D
+ csc_matrix(S)
+ 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'.
+
+ csc_matrix((data, ij), [shape=(M, N)])
+ where ``data`` and ``ij`` satisfy ``a[ij[0, k], ij[1, k]] = data[k]``
+
Notes
- =====
- Advantages of the LIL format
- ----------------------------
- - supports flexible slicing
- - changes to the matrix sparsity structure are efficient
+ -----
+
+ 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
+ Disadvantages of the LIL format
+ - arithmetic operations LIL + LIL are slow (consider CSR or CSC)
+ - slow column slicing (consider CSC)
+ - 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
-
+ Intended 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
+
+ Data Structure
+ - An array (``self.rows``) of rows, each of which is a sorted
+ list of column indices of non-zero elements.
+ - The corresponding nonzero values are stored in similar
+ fashion in ``self.data``.
+
+
"""
def __init__(self, A=None, shape=None, dtype=None, copy=False):
Modified: trunk/scipy/sparse/sputils.py
===================================================================
--- trunk/scipy/sparse/sputils.py 2008-03-05 14:38:29 UTC (rev 3998)
+++ trunk/scipy/sparse/sputils.py 2008-03-07 00:33:56 UTC (rev 3999)
@@ -21,8 +21,8 @@
upcast(t0, t1, ..., tn) -> T where T is a supported dtype
- Example
- =======
+ Examples
+ --------
>>> upcast('int32')
<type 'numpy.int32'>
More information about the Scipy-svn
mailing list