pythontensorflowbessel-functions

How do I implement BesselJ functions in tensorflow


tensorflow inner functions supports the Bessel function BesselI tensorflow besseli0.

However, it does not have BesselJ functions (especially BesselJ[2,x]).

If I were to approximate this function, I would have to use If in tensorflow, which is not efficient.

Does tensorflow have any replacement or any other methods that can be recommended?


Solution

  • After many searches, I found a just-satisfactory solution:

    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
    

    This is not very fast, though.