[Numpy-discussion] Kronecker product

Konrad Hinsen hinsen at cnrs-orleans.fr
Wed Jul 18 10:20:20 CDT 2001

> I would appreciate it, if Numpy could handle the Kronecker-product of
> two matrices X, Y.
> kron(X,Y) is the Kronecker tensor product of X and Y.

import Numeric
def kron(x, y):
  return Numeric.multiply.outer(x, y)

> The result is a large matrix formed by taking all possible products
> between the elements of X and those of Y. If X is m-by-n
> and Y is p-by-q, then kron(X,Y) is m*p-by-n*q.

OK, there's one difference: the function shown about returns an array
of shape (m, n, p, q). If the input arrays are always 2D, the
following will do what you need:

import Numeric
def kron(x, y):
  z = Numeric.transpose(Numeric.multiply.outer(x, y), [0, 2, 1, 3])
  z.shape = (z.shape[0]*z.shape[1], z.shape[2]*z.shape[3])
  return z

Konrad Hinsen                            | E-Mail: hinsen at cnrs-orleans.fr
Centre de Biophysique Moleculaire (CNRS) | Tel.: +33-
Rue Charles Sadron                       | Fax:  +33-
45071 Orleans Cedex 2                    | Deutsch/Esperanto/English/
France                                   | Nederlands/Francais

More information about the Numpy-discussion mailing list