[SciPy-User] Vectorized spherical Bessel functions

Ondřej Čertík ondrej.certik at gmail.com
Mon Dec 2 23:20:54 EST 2013


Hi Oleksandr,

On Mon, Dec 2, 2013 at 5:44 PM, Oleksandr Huziy <guziy.sasha at gmail.com> wrote:
> Hi:
>
> have you tried numpy.vectorize?
>
> In [3]: import numpy as np
>
> In [4]: jn_vect = np.vectorize(sph_jn)
>
>
> In [9]: jn_vect(0, [0.1, 0.2, 0.3, 0.5])
> Out[9]:
> (array([ 0.99833417,  0.99334665,  0.98506736,  0.95885108]),
>  array([-0.03330001, -0.06640038, -0.09910289, -0.16253703]))
>
> In [10]: jn_vect([0] * 4, [0.1, 0.2, 0.3, 0.5])
> Out[10]:
> (array([ 0.99833417,  0.99334665,  0.98506736,  0.95885108]),
>  array([-0.03330001, -0.06640038, -0.09910289, -0.16253703]))

Unfortunately, the performance of vectorize() is described in it's docstring:

    The `vectorize` function is provided primarily for convenience, not for
    performance. The implementation is essentially a for loop.

So it doesn't fix the problem that it's slow. Thanks for the tip
though --- at least it has a nice syntax,
so I'll be using that. The jn0(x) function is just sin(x)/x, so
compared to the intrinsic sin(x) it's just slow.

It looks like the only faster option is something like Cython or Numba.

Ondrej



More information about the SciPy-User mailing list