jason
jason

Reputation: 331

tensor product of matrices in Numpy/python

Is there a numpy function that does tensor product of two matrices ? That creates a 4x4 product matrix of two 2x2 matrices?

Upvotes: 18

Views: 27346

Answers (2)

Sahaj Raj Malla
Sahaj Raj Malla

Reputation: 479

If you're looking for tensor product, then it can be achieved by numpy.

import numpy as np

A = np.array([[1,3], [4,2]])
B = np.array([[2,1], [5,4]])

np.tensordot(A, B, axes=0)

Three common use cases are:

  1. axes = 0 : tensor product

  2. axes = 1 : tensor dot product

  3. axes = 2 : (default) tensor double contraction

Upvotes: 4

jengel
jengel

Reputation: 635

I believe what you're looking for is the Kronecker product

http://docs.scipy.org/doc/numpy/reference/generated/numpy.kron.html#numpy.kron

Example:

>>> np.kron(np.eye(2), np.ones((2,2)))
array([[ 1.,  1.,  0.,  0.],
       [ 1.,  1.,  0.,  0.],
       [ 0.,  0.,  1.,  1.],
       [ 0.,  0.,  1.,  1.]])

Upvotes: 31

Related Questions