tensor product of matrices in Numpy/python
Asked Answered
M

2

17

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

Monosome answered 21/11, 2013 at 22:6 Comment(0)
R
30

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.]])
Rodeo answered 18/7, 2014 at 21:18 Comment(0)
C
3

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

Celisse answered 22/9, 2021 at 3:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.