I am currently doing some studies on computing a 4th order tensor in numpy with the einsum function.
The tensor I am computing is written in Einstein notation and the function einsun does the work perfectly! But I would like to know what it is doing in the following case:
import numpy as np
a=np.array([[2,0,3],[0,1,0],[0, 0, 4]])
b= np.eye(3)
r1=np.einsum("ij,kl->ijkl", a, b)
r2=np.einsum("ik,jl->ijkl", a, b)
in r1 I am basically doing the standard tensor product (equivalent to np.tensordot(a,b,axes=0)).
What about in r2?
I know I can get the value by doing a[:,None,:,None]*b[None,:,None,:] but I do not know what the indexing is doing. Does this operation have a name?
Sorry if this is too basic!
r1andr2as an outer product. The 2nd just reorders the axes of the 4d result. Matrix product can do the same sort of multiplication, but it sums one or more axes (the shared index). In your case there's no summation of products.r1can also be written with broadcasting - just reorder theNone. - hpauljnp.tensordotreshapes and transposes the inputs till it can do a standard 2ddotproduct, and then transforms the result back. My memory is that the single number axes parameter is translated into an expanded tuple of axes. I find that theeinsumindexing is clearer, and more powerful. - hpauljNoneis doing. And it's creating an extra dimension of size 1 so that the arrays become compatible for multiplication. It's called broadcasting. - Ananda