3

This thread successfully answers how to use numpy to calculate the tensor product of two matrices in Python. However, I want the output to be in a proper matrix format so that further calculations can be performed on it without my manually editing it.

For instance,

I=matrix([[1,0],[0,1]])
print np.tensordot(I, I, axes=0)

Gives the output:

[[[[1 0]
   [0 1]]

  [[0 0]
   [0 0]]]


 [[[0 0]
   [0 0]]

  [[1 0]
   [0 1]]]]

I want it in the form:

[[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]]

so that I can perform further operations on it within the program itself.

How do I do this?

Community
  • 1
  • 1
  • 1
    The Kronecker tensor product can be calculated using [numpy.kron](http://docs.scipy.org/doc/numpy/reference/generated/numpy.kron.html), if that is what you are searching for. If it isn't, could you give a definition of the product you're after? – Phillip May 11 '14 at 12:11
  • @Phillip it seems you found the solution, you could post your comment as an answer... – Saullo G. P. Castro May 11 '14 at 13:18
  • Oh, perfect! This is exactly what I needed! Thank you so much. I was hoping the answer was a simple one that I may have overlooked. – user3625380 May 11 '14 at 13:48
  • 1
    Good to hear. I've reposted my comment as an answer. – Phillip May 11 '14 at 13:58

1 Answers1

6

The Kronecker tensor product can be calculated using numpy.kron.

Phillip
  • 13,448
  • 29
  • 41