I'd like to sum a function f over unordered pairs (n,m) with n and m distinct, where n,m range from -N to N.
The function depends on a sequence of floats stored in a dictionary, indexed from -N to N. It looks something like:
def f(n,m):
return dict[n]*dict[m]*np.sin(c*(n-m)) / (n-m)
Naively, I'd write:
indices = [ (n,m) for n in range(-N,N+1) for m in range(-N,N+1) if m>n ]
sum([f(n,m) for (n,m) in indices])
Is there a way to vectorize this with numpy and make it more efficient? I'm using Python 3, in case it matters.