Suppose I have an N x N x N dimensional numpy array X with entries X[i,j,k]. I want to use X to define an N x N x N x N dimensional numpy array Y defined as follows:
Y[i,j,k,k] = X[i,j,k]
Y[i,j,k,l] = 0 when k != l
My idea is to use numpy.einsum to accomplish this task via the following code:
Y = np.einsum('ijk->ijkk', X).
However, this doesn't work since I get the following error
ValueError: einstein sum subscripts string includes output subscript 'k' multiple times
Is there a way to accomplish this straightforwardly without having to use for loops?
einsumcan do this? Just because 'ii->i' takes a diagonal, you think the "reverse" can add one? Is that documented?Yis a 4d array of the right shape, thenz=np.einsum('ijkk->ijk',Y)is a 3dview. You could copy yourXto that.