1

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.

4

1 回答 1

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.

于 2015-02-09T17:55:12.933 回答