Parameters to numpy’s fromfunction

The documentation is very misleading in that respect. It’s just as you note: instead of performing f(0,0), f(0,1), f(1,0), f(1,1), numpy performs

f([[0., 0.], [0., 1.]], [[1., 0.], [1., 1.]])

Using ndarrays rather than the promised integer coordinates is quite frustrating when you try and use something likelambda i: l[i], where l is another array or list (though really, there are probably better ways to do this in numpy).

The numpy vectorize function fixes this. Where you have

m = fromfunction(f, shape)

Try using

g = vectorize(f)
m = fromfunction(g, shape)

Leave a Comment