Use numpy ;
In [1]: import numpy as np In [2]: A = np.array([[1, 2], [3, 4]]) In [3]: B = np.array([[3, 1], [1, 2]]) In [4]: C = np.outer(A, B) In [5]: C Out[5]: array([[ 3, 1, 1, 2], [ 6, 2, 2, 4], [ 9, 3, 3, 6], [12, 4, 4, 8]])
After you get the desired result, you can use numpy.reshape() to form it in almost any form,
In [6]: C.reshape([4,2,2]) Out[6]: array([[[ 3, 1], [ 1, 2]], [[ 6, 2], [ 2, 4]], [[ 9, 3], [ 3, 6]], [[12, 4], [ 4, 8]]])
Roland Smith
source share