【发布时间】:2015-04-11 01:57:00
【问题描述】:
我注意到scipy.special n 阶贝塞尔函数和参数 x jv(n,x) 在 x 中被矢量化:
In [14]: import scipy.special as sp
In [16]: sp.jv(1, range(3)) # n=1, [x=0,1,2]
Out[16]: array([ 0., 0.44005059, 0.57672481])
但是球面贝塞尔函数没有对应的矢量化形式,sp.sph_jn:
In [19]: sp.sph_jn(1,range(3))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-19-ea59d2f45497> in <module>()
----> 1 sp.sph_jn(1,range(3)) #n=1, 3 value array
/home/glue/anaconda/envs/fibersim/lib/python2.7/site-packages/scipy/special/basic.pyc in sph_jn(n, z)
262 """
263 if not (isscalar(n) and isscalar(z)):
--> 264 raise ValueError("arguments must be scalars.")
265 if (n != floor(n)) or (n < 0):
266 raise ValueError("n must be a non-negative integer.")
ValueError: arguments must be scalars.
此外,球面贝塞尔函数可以一次性计算 N 的所有阶数。因此,如果我想要 n=5 Bessel 函数作为参数 x=10,它会返回 n=1,2,3,4,5。它实际上一次返回 jn 及其导数:
In [21]: sp.sph_jn(5,10)
Out[21]:
(array([-0.05440211, 0.07846694, 0.07794219, -0.03949584, -0.10558929,
-0.05553451]),
array([-0.07846694, -0.0700955 , 0.05508428, 0.09374053, 0.0132988 ,
-0.07226858]))
为什么 API 中存在这种不对称性,有没有人知道一个库可以返回向量化的球形 Bessel 函数,或者至少更快(即在 cython 中)?
【问题讨论】:
-
这是 scipy.special 中的一个已知功能请求(关于如何实现这些矢量化版本的一些指导):github.com/scipy/scipy/issues/3113。
-
如果有人有开箱即用的实现,请发布!感谢分享
标签: python numpy scipy vectorization bessel-functions