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

scipy-svn@scip... scipy-svn@scip...
Wed Aug 1 12:08:23 CDT 2007


Author: stefan
Date: 2007-08-01 12:07:49 -0500 (Wed, 01 Aug 2007)
New Revision: 3220

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


Modified: trunk/Lib/sparse/sparse.py
===================================================================
--- trunk/Lib/sparse/sparse.py	2007-08-01 16:18:38 UTC (rev 3219)
+++ trunk/Lib/sparse/sparse.py	2007-08-01 17:07:49 UTC (rev 3220)
@@ -9,7 +9,7 @@
             'lil_matrix','dok_matrix', 
             'spdiags','speye','spidentity', 
             'isspmatrix','issparse','isspmatrix_csc','isspmatrix_csr',
-            'isspmatrix_lil','isspmatrix_dok', 'lil_eye' ]
+            'isspmatrix_lil','isspmatrix_dok', 'lil_eye', 'lil_diags' ]
 
 import warnings
 
@@ -2674,6 +2674,51 @@
         out.data[c-k].append(1)
     return out
 
+def lil_diags(diags,offsets,(m,n),dtype=float):
+    """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.
+
+    Example:
+    -------
+
+    >>> lil_diags([[1,2,3],[4,5],[6]],[0,1,2],(3,3)).todense()
+    matrix([[ 1.,  4.,  6.],
+            [ 0.,  2.,  5.],
+            [ 0.,  0.,  3.]])
+
+    """
+    offsets_unsorted = list(offsets)
+    diags_unsorted = list(diags)
+    if len(diags) != len(offsets):
+        raise ValueError("Number of diagonals provided should "
+                         "agree with offsets.")
+
+    sort_indices = numpy.argsort(offsets_unsorted)
+    diags = [diags_unsorted[k] for k in sort_indices]
+    offsets = [offsets_unsorted[k] for k in sort_indices]
+
+    for i,k in enumerate(offsets):
+        if len(diags[i]) < m-abs(k):
+            raise ValueError("Not enough values specified to fill "
+                             "diagonal %s." % k)
+
+    out = lil_matrix((m,n),dtype=dtype)
+    for k,diag in itertools.izip(offsets,diags):
+        for ix,c in enumerate(xrange(clip(k,0,n),clip(m+k,0,n))):
+            out.rows[c-k].append(c)
+            out.data[c-k].append(diag[ix])
+    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 16:18:38 UTC (rev 3219)
+++ trunk/Lib/sparse/tests/test_sparse.py	2007-08-01 17:07:49 UTC (rev 3220)
@@ -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, lil_eye
+     spidentity, speye, lil_matrix, lil_eye, lil_diags
 from scipy.linsolve import splu
 restore_path()
 
@@ -32,7 +32,7 @@
         self.dat = matrix([[1,0,0,2],[3,0,1,0],[0,2,0,0]],'d')
         self.datsp = self.spmatrix(self.dat)
 
-    def check_getelement(self):        
+    def check_getelement(self):
         assert_equal(self.datsp[0,0],1)
         assert_equal(self.datsp[0,1],0)
         assert_equal(self.datsp[1,0],3)
@@ -872,7 +872,37 @@
                 assert_array_equal(lil_eye(dim,k).todense(),
                                    speye(r,c,k).todense())
 
+    def check_lil_diags(self):
+        assert_array_equal(lil_diags([[1,2,3],[4,5],[6]],
+                                     [0,1,2],(3,3)).todense(),
+                           [[1,4,6],
+                            [0,2,5],
+                            [0,0,3]])
 
+        assert_array_equal(lil_diags([[6],[4,5],[1,2,3]],
+                                     [2,1,0],(3,3)).todense(),
+                           [[1,4,6],
+                            [0,2,5],
+                            [0,0,3]])
+
+        assert_array_equal(lil_diags([[6,7,8],[4,5],[1,2,3]],
+                                     [2,1,0],(3,3)).todense(),
+                           [[1,4,6],
+                            [0,2,5],
+                            [0,0,3]])
+
+        assert_array_equal(lil_diags([[1,2,3],[4,5],[6]],
+                                     [0,-1,-2],(3,3)).todense(),
+                           [[1,0,0],
+                            [4,2,0],
+                            [6,5,3]])
+
+        assert_array_equal(lil_diags([[6,7,8],[4,5]],
+                                     [-2,-1],(3,3)).todense(),
+                           [[0,0,0],
+                            [4,0,0],
+                            [6,5,0]])
+
 class test_construct_utils(NumpyTestCase):
     def check_identity(self):
         a = spidentity(3)



More information about the Scipy-svn mailing list