[SciPy-User] Vectorized spherical Bessel functions

Oleksandr Huziy guziy.sasha at gmail.com
Mon Dec 2 19:44:30 EST 2013


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]))

Cheers



2013/12/2 Ondřej Čertík <ondrej.certik at gmail.com>

> Hi,
>
> I need to apply spherical bessel function (values) to a vector. The
> current functions accept a scalar and return two arrays of values and
> derivatives as follows:
>
> >>> from scipy.special import sph_jn
> >>> sph_jn(0, 5.)
> (array([-0.19178485]), array([ 0.09508941]))
>
>
> So in order to vectorize it, I use:
>
> def j0(x):
>     res = empty(len(x), dtype="double")
>     for i in range(len(x)):
>         res[i] = sph_jn(0, x[i])[0][0]
>     return res
>
> Which is really slow for larger vectors... Any ideas how to quickly
> get an array of values?
>
> I can use Cython, etc. but I was wondering whether there is some
> obvious way to do this from Python using current SciPy.
>
> Ondrej
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>



-- 
Sasha
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20131202/565d1596/attachment.html>


More information about the SciPy-User mailing list