1

I need to vectorize this for loop for a loss function in pytorch.

nlr = 461
for j in np.arange(nlr-1,-1,-1): 
    ed = np.exp(1j * (d[j]/v[j])*om) #I know this can be done using outer product
    reverb = 1 / (1 - ru[j]*ed*rdhat*ed)
    rdhat = rd[j] + tu[j]*ed*rdhat*ed*reverb*td[j]
    tdhat = tdhat*ed*reverb*td[j]

d and v are arrays, and they both have shape of (461,1)

om has shape of (600,1)

ru has a shape of (461,1)

rdhat has a shape of (600,1)

tdhat has a shape of (600,1)

Some help please?

Zack
  • 21
  • 2
  • Vectorizing recursive operations (in which elements of an array depend on previous ones, as `rdhat` and `tdhat` in your problem) is pretty much impossible by definition. In your case, you can take the `ed` line out of the loop, but other than that, you will have to resort to Numba or other resources (see this question: https://stackoverflow.com/q/4407984/8345628) – Lith Sep 22 '21 at 06:42

0 Answers0