[Scipy-svn] r3256 - in trunk/scipy/sandbox: . multigrid

scipy-svn@scip... scipy-svn@scip...
Wed Aug 22 14:10:32 CDT 2007


Author: wnbell
Date: 2007-08-22 14:10:26 -0500 (Wed, 22 Aug 2007)
New Revision: 3256

Added:
   trunk/scipy/sandbox/multigrid/info.py
   trunk/scipy/sandbox/multigrid/setup.py
   trunk/scipy/sandbox/multigrid/utils.py
Modified:
   trunk/scipy/sandbox/multigrid/coarsen.py
   trunk/scipy/sandbox/setup.py
Log:
added config for multigrid subpackage



Modified: trunk/scipy/sandbox/multigrid/coarsen.py
===================================================================
--- trunk/scipy/sandbox/multigrid/coarsen.py	2007-08-22 18:35:07 UTC (rev 3255)
+++ trunk/scipy/sandbox/multigrid/coarsen.py	2007-08-22 19:10:26 UTC (rev 3256)
@@ -4,7 +4,7 @@
 import scipy
 import numpy
     
-from pydec import diag_sparse,inf_norm
+from utils import diag_sparse,inf_norm
 
 
 def rs_strong_connections(A,theta):

Added: trunk/scipy/sandbox/multigrid/info.py
===================================================================
--- trunk/scipy/sandbox/multigrid/info.py	2007-08-22 18:35:07 UTC (rev 3255)
+++ trunk/scipy/sandbox/multigrid/info.py	2007-08-22 19:10:26 UTC (rev 3256)
@@ -0,0 +1,5 @@
+"""
+TODO Describe AMG solvers
+"""
+
+postpone_import = 1

Added: trunk/scipy/sandbox/multigrid/setup.py
===================================================================
--- trunk/scipy/sandbox/multigrid/setup.py	2007-08-22 18:35:07 UTC (rev 3255)
+++ trunk/scipy/sandbox/multigrid/setup.py	2007-08-22 19:10:26 UTC (rev 3256)
@@ -0,0 +1,24 @@
+#!/usr/bin/env python
+
+from os.path import join
+import sys
+
+def configuration(parent_package='',top_path=None):
+    import numpy
+    from numpy.distutils.misc_util import Configuration
+
+    config = Configuration('multigrid',parent_package,top_path)
+
+    config.add_data_dir('tests')
+
+    # Adding a Python file as a "source" file for an extension is something of
+    # a hack, but it works to put it in the right place.
+    sources = [join('multigridtools', x) for x in ['multigridtools.py', 'multigridtools_wrap.cxx']]
+    config.add_extension('_multigridtools',
+                         sources=sources,
+                         include_dirs=['multigridtools'])
+    return config
+
+if __name__ == '__main__':
+    from numpy.distutils.core import setup
+    setup(**configuration(top_path='').todict())


Property changes on: trunk/scipy/sandbox/multigrid/setup.py
___________________________________________________________________
Name: svn:executable
   + *

Added: trunk/scipy/sandbox/multigrid/utils.py
===================================================================
--- trunk/scipy/sandbox/multigrid/utils.py	2007-08-22 18:35:07 UTC (rev 3255)
+++ trunk/scipy/sandbox/multigrid/utils.py	2007-08-22 19:10:26 UTC (rev 3256)
@@ -0,0 +1,57 @@
+__all__ =['inf_norm','diag_sparse']
+
+import numpy,scipy,scipy.sparse,scipy.weave
+from numpy import ravel,arange
+from scipy.sparse import isspmatrix,isspmatrix_csr,isspmatrix_csc, \
+                        csr_matrix,csc_matrix
+
+def inf_norm(A):
+    """
+    Infinity norm of a sparse matrix (maximum absolute row sum).  This serves 
+    as an upper bound on spectral radius.
+    """
+    
+    if not isspmatrix_csr(A):
+        return ValueError,'expected csr_matrix'
+    
+    abs_A = csr_matrix((abs(A.data),A.indices,A.indptr),dims=A.shape,check=False)
+    return (abs_A * numpy.ones(A.shape[1],dtype=A.dtype)).max()
+
+def diag_sparse(A):
+    """
+    If A is a sparse matrix (e.g. csr_matrix or csc_matrix)
+       - return the diagonal of A as an array
+
+    Otherwise
+       - return a csr_matrix with A on the diagonal
+    """
+    
+    if isspmatrix_csr(A) or isspmatrix_csc(A):
+        n_row = len(A.indptr) - 1
+        data,indices,indptr = A.data,A.indices,A.indptr
+
+        diag = numpy.zeros(n_row,dtype=A.dtype)
+        
+        code = """
+        #line 33 "sparse.py"
+
+        for(int i = 0; i < n_row; i++){
+          for(int jj = indptr(i); jj < indptr(i+1); jj++){
+            if(indices(jj) == i){
+              diag(i) = data(jj);
+            }
+          }
+        }
+        """
+
+        err = scipy.weave.inline(code,
+                                 ['data', 'indices', 'indptr', 'n_row', 'diag'],
+                                 type_converters = scipy.weave.converters.blitz,
+                                 compiler = 'gcc')
+        return diag
+    elif isspmatrix(A): 
+        return ravel(array([float(A[i,i]) for i in range(min(A.shape))]))
+    else:
+        return csr_matrix((A,arange(len(A)),arange(len(A)+1)),(len(A),len(A)))
+
+

Modified: trunk/scipy/sandbox/setup.py
===================================================================
--- trunk/scipy/sandbox/setup.py	2007-08-22 18:35:07 UTC (rev 3255)
+++ trunk/scipy/sandbox/setup.py	2007-08-22 19:10:26 UTC (rev 3256)
@@ -84,6 +84,10 @@
     # Radial basis functions package
     #config.add_subpackage('rbf')
 
+    # Multigrid Solvers
+    #config.add_subpackage('multigrid')
+
+
     return config
 
 if __name__ == '__main__':



More information about the Scipy-svn mailing list