Home > Blockchain >  Translate a sum(...) to numpy
Translate a sum(...) to numpy

Time:09-29

I would like to do sum(ai*np.exp(bi*r2) for ai,bi in zip(a,b)) entirely in numpy, where a and b are 1d arrays, and r2 can be any shape and the result has the shape of r2. My broadcasting-fu is too weak!

CodePudding user response:

Try

  np.sum(a*np.exp(b*r2[...,None], axis=-1)

The idea is to broadcast with the a,b dimension at the end.

  • Related