0

Hello I have just recently started working with python 3d arrays and I am not too familiar with how to multiply each block in the 3d array with all other blocks in the 3d array. I am working with a np array with shape (31, 22, 16). Let say my numpy array is x with shape (31, 22, 16) I will like to get x[16:31,0,0] and multiply it with every other x[16:31,i,j]. For each pair of multiplication I will like to sum the result like so np.sum(x[16:31,i,j] * x[16:31,i,j]). The inefficient code uses two nested loops it becomes really slow when I have large data. Could someone please help me with a faster solution for computing the below code please? To be more exact I want to use np.einsum to do this but I am not sure how it works. Thanks!

llList = []
for gg in range(nrGates):
    for bb in range(nrBeams):
        llList.append((bb, gg))

# posInx = [16->31] contains numbers from 16 to 31
for ll in range(nCells):  # nCells = 352
    llAI  = llList[ll]  # contains tuples of (nrBeams, nrGates)
    spectL = currentData.spectrum[posInx, llAI[0], llAI[1]]  # shape (16,)
    # currentData.spectrum has shape (31, 22, 16)
    for mm in range(nCells):
        mmAI = llList[mm]
        spectM = currentData.spectrum[posInx, mmAI[0], mmAI[1]]
        currentData.Dlm[ll, mm] = np.sum(spectL * np.conj(spectM))
andthum
  • 87
  • 6
  • Maybe this nice explanation of `numpy.einsum` can help you: https://stackoverflow.com/a/33641428/20655817 – andthum Mar 24 '23 at 12:05

0 Answers0