Creating a tensor where each entry is a function of its index
I want to create a matrix D that is defined by D[i,j]=d(i-j) where d is some arbitrary function that I can choose.
It can easily be done with loops, but it is very slow. Is there an efficiant way of creating this matrix with torch or numpy?

You could apply the function (if vectorised) to numpy.indices:
import numpy as np

i, j = np.indices((n, m))
D = d(i - j)

