Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tensor product of matrices in Numpy/python

Tags:

matrix

product

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

like image 242
jason Avatar asked Nov 21 '13 22:11

jason


People also ask

How do you take a tensor product in Python?

To compute the tensor dot product, use the numpy. tensordot() method in Python. The a, b parameters are Tensors to “dot”. The axes parameter, integer_like If an int N, sum over the last N axes of a and the first N axes of b in order.

What is Tensordot in Python?

Tensordot (also known as tensor contraction) sums the product of elements from a and b over the indices specified by axes . This operation corresponds to numpy. tensordot(a, b, axes) . Example 1: When a and b are matrices (order 2), the case axes=1 is equivalent to matrix multiplication.


1 Answers

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.]])
like image 187
jengel Avatar answered Sep 21 '22 08:09

jengel