Python 如何在tensorflow中实现BesselJ函数

Python 如何在tensorflow中实现BesselJ函数,python,tensorflow,bessel-functions,Python,Tensorflow,Bessel Functions,tensorflow内部函数支持Bessel函数BesselI 但是,它没有贝塞尔函数(特别是贝塞尔函数[2,x]) 如果我要近似这个函数,我必须在tensorflow中使用If,这是没有效率的 tensorflow是否有任何可推荐的替代品或其他方法?经过多次搜索,我找到了一个令人满意的解决方案: def BesselJ0(x): PI=3.1415926 temp = tf.cast(tf.less(x,tf.ones_like(x)*4.7),dtype=tf.float32

tensorflow内部函数支持Bessel函数BesselI

但是,它没有贝塞尔函数(特别是贝塞尔函数[2,x])

如果我要近似这个函数,我必须在tensorflow中使用
If
,这是没有效率的


tensorflow是否有任何可推荐的替代品或其他方法?

经过多次搜索,我找到了一个令人满意的解决方案:

def BesselJ0(x):
    PI=3.1415926
    temp = tf.cast(tf.less(x,tf.ones_like(x)*4.7),dtype=tf.float32)
    temp1 = tf.abs(temp-1)
    left_part = tf.multiply(1-x**2/4+x**4/64-x**6/2304+x**8/147456-x**10/14745600,temp)
    right_part = tf.multiply(tf.sqrt(2/PI/x)*tf.cos(x-PI/4),temp1)
    return left_part+right_part
def BesselJ1(x):
    PI=3.1415926
    temp = tf.cast(tf.less(x,tf.ones_like(x)*3.8),dtype=tf.float32)
    temp1 = tf.abs(temp-1)
    left_part = tf.multiply(x/2-x**3/16+x**5/384-x**7/18432+x**9/1474560-x**11/176947200+x**13/29727129600,temp)
    right_part = tf.multiply(tf.sqrt(2/PI/x)*tf.cos(x-PI/2-PI/4),temp1)
    return left_part+right_part
但这并不是很快