Another thing to add, the python example code is a written in quite a weird (and in my opinion difficult to read way).
n = len(a) M = np.exp(1jk(np.tile(a,(n,1))*2 + np.tile(a.reshape(n,1),(1,n))*2))
is equivalent to
M = np.exp(1jk(a[:,None]*2 + a[None,:]*2))
which I find very easy to grasp.