[SciPy-dev] Kronecker sum of sparse matrices

Nils Wagner nwagner at iam.uni-stuttgart.de
Sat Feb 9 13:21:01 EST 2008


Hi all,

I have written a function for the Kronecker sum
of sparse matrices. It is meant for
http://projects.scipy.org/scipy/scipy/ticket/313


def spkronsum(a,b):
     """kronecker sum of a and b

     Kronecker sum of two sparse matrices is a sum
     of two Kronecker products kron(I_m,a)+kron(b,I_n)

     Inputs:

       a -- An n x n sparse matrix.
       b -- An m x m sparse matrix
     """
     m,n = b.get_shape()
     p,q = a.get_shape()

     if p != q:
         raise ValueError, 'expected square matrix'

     if m != n:
         raise ValueError, 'expected square matrix'

     return spkron(speye(m,n),a)+spkron(b,speye(p,q))


Cheers,
           Nils


>>> B = coo_matrix([[1.+1j,2],[3,4-1j]])
>>> print B
   (0, 0)        (1.0+1.0j)
   (0, 1)        (2.0+0.0j)
   (1, 0)        (3.0+0.0j)
   (1, 1)        (4.0+-1.0j)


The last entry in B contains a typo "+-"





More information about the SciPy-Dev mailing list