2

I would like to convert an mpmath function to a function that can work on numpy arrays. Let's say I have for example the following

A=np.linspace(0,1,100)
besseli_vec = numpy.frompyfunc(mpmath.besseli, 2, 1)
Y=besseli_vec(0, A)

However, now the values in the array A are of the mpmath type mpf. So what is the fastest/best way to take a function in mpmath, and convert it to a function that can act on numpy arrays, but that returns standard float and not mpmath floats? Or just convert an array of mpf to numpy floats? the function float() works to convert numbers, but not arrays.

Jonathan Lindgren
  • 1,192
  • 3
  • 14
  • 31

1 Answers1

1
besseli_vec = numpy.frompyfunc(lambda *a: float(mpmath.besseli(*a)), 2, 1)

should do it. The mpmath computation will probably be much slower than the lambda, so the speed impact will probably be approximately just the float conversion.

pv.
  • 33,875
  • 8
  • 55
  • 49
  • Thanks but I am not sure i understand your answer. Will the float conversion significantly alter the speed compared to only evaluateing the mpmath function? – Jonathan Lindgren Feb 09 '15 at 18:49
  • Most likely its effect on speed is minuscule, and you will in any case need to do it if you want floats from mpf objects. (Since Numpy arrays don't know about mpf objects, they cannot speed up these operations.) – pv. Feb 09 '15 at 20:22