[Scipy-svn] r3219 - in trunk/Lib/sparse: . tests

scipy-svn@scip... scipy-svn@scip...
Wed Aug 1 11:19:15 CDT 2007


Author: stefan
Date: 2007-08-01 11:18:38 -0500 (Wed, 01 Aug 2007)
New Revision: 3219

Modified:
   trunk/Lib/sparse/sparse.py
   trunk/Lib/sparse/tests/test_sparse.py
Log:
Add lil_eye.


Modified: trunk/Lib/sparse/sparse.py
===================================================================
--- trunk/Lib/sparse/sparse.py	2007-08-01 15:50:42 UTC (rev 3218)
+++ trunk/Lib/sparse/sparse.py	2007-08-01 16:18:38 UTC (rev 3219)
@@ -9,14 +9,14 @@
             'lil_matrix','dok_matrix', 
             'spdiags','speye','spidentity', 
             'isspmatrix','issparse','isspmatrix_csc','isspmatrix_csr',
-            'isspmatrix_lil','isspmatrix_dok' ]
+            'isspmatrix_lil','isspmatrix_dok', 'lil_eye' ]
 
 import warnings
 
 from numpy import zeros, isscalar, real, imag, asarray, asmatrix, matrix, \
                   ndarray, amax, amin, rank, conj, searchsorted, ndarray,   \
                   less, where, greater, array, transpose, empty, ones, \
-                  arange, shape, intc
+                  arange, shape, intc, clip
 import numpy
 from scipy.sparse.sparsetools import cscmux, csrmux, \
      cootocsr, csrtocoo, cootocsc, csctocoo, csctocsr, csrtocsc, \
@@ -2654,7 +2654,26 @@
     diags = ones((1, n), dtype = dtype)
     return spdiags(diags, k, n, m)
 
+def lil_eye((r,c), k=0, dtype=float):
+    """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.
+
+    """
+    out = lil_matrix((r,c),dtype=dtype)
+    for c in xrange(clip(k,0,c),clip(r+k,0,c)):
+        out.rows[c-k].append(c)
+        out.data[c-k].append(1)
+    return out
+
 def issequence(t):
     return isinstance(t, (list, tuple))
 

Modified: trunk/Lib/sparse/tests/test_sparse.py
===================================================================
--- trunk/Lib/sparse/tests/test_sparse.py	2007-08-01 15:50:42 UTC (rev 3218)
+++ trunk/Lib/sparse/tests/test_sparse.py	2007-08-01 16:18:38 UTC (rev 3219)
@@ -22,7 +22,7 @@
 from numpy.testing import *
 set_package_path()
 from scipy.sparse import csc_matrix, csr_matrix, dok_matrix, coo_matrix, \
-     spidentity, speye, lil_matrix
+     spidentity, speye, lil_matrix, lil_eye
 from scipy.linsolve import splu
 restore_path()
 
@@ -865,7 +865,14 @@
                             [0,0,9],
                             [0,16,0]])
 
+    def check_lil_eye(self):
+        for dim in [(3,5),(5,3)]:
+            for k in range(-5,5):
+                r,c = dim
+                assert_array_equal(lil_eye(dim,k).todense(),
+                                   speye(r,c,k).todense())
 
+
 class test_construct_utils(NumpyTestCase):
     def check_identity(self):
         a = spidentity(3)



More information about the Scipy-svn mailing list