src.acoustools.SphericalHarmonics.Bessel

  1import scipy, torch, math
  2import numpy as np
  3import acoustools.Constants as c
  4
  5from line_profiler import profile
  6
  7
  8# _CACHE_J = {}
  9
 10@profile
 11#Seems like this is wrong? Go over this again
 12def bessel_J(x, alpha, M=20, depth=0, max_depth=3):
 13    
 14    # print(alpha, x)
 15    
 16    return scipy.special.jv(alpha, x)
 17    '''
 18    if type(x) == torch.Tensor:
 19        x_key = hash(str(torch.hash_tensor(x.real).item()) + str(x.shape))
 20        # x_key = hash(tuple(x.reshape(-1).real.tolist()))
 21
 22    else:
 23        x_key = x
 24    
 25    # print(x_key)
 26
 27    
 28    if (x_key, alpha) in _CACHE_J:
 29        J, d =  _CACHE_J[(x_key, alpha)]
 30    
 31    if alpha >= -1 and alpha <= 1:
 32        J =  scipy.special.jv(alpha, x)
 33        _CACHE_J[(x_key, alpha)] = (J, depth)
 34        return J
 35    
 36    if alpha < 0:
 37
 38        Jplus = bessel_J(x, alpha+1, M=M, depth=depth+1)
 39        _CACHE_J[(x_key, alpha+1)] = (Jplus, depth)
 40        
 41        Jplus_2 = bessel_J(x, alpha+2, M=M, depth=depth+1)
 42        _CACHE_J[(x_key, alpha+2)] = (Jplus_2, depth)
 43        
 44        J = (2*(alpha+1))/x * Jplus - Jplus_2
 45        _CACHE_J[(x_key, alpha)] = (J, depth)
 46    
 47    else:      
 48        Jminus = bessel_J(x, alpha-1, M=M, depth=depth+1)
 49        _CACHE_J[(x_key, alpha-1)] = (Jminus, depth)
 50        
 51        Jminus_2 = bessel_J(x, alpha-2, M=M, depth=depth+1)
 52        _CACHE_J[(x_key, alpha-2)] = (Jminus_2, depth)
 53        
 54        J = (2*(alpha-1))/x * Jminus - Jminus_2
 55        _CACHE_J[(x_key, alpha)] = (J, depth)
 56
 57    
 58    # print(alpha, ( J / scipy.special.jv(alpha, x)) )
 59    # print(len(_CACHE_J))
 60    # exit()
 61    return J
 62    '''
 63    
 64    
 65def bessel_Y(x, alpha, M=20):
 66    Y = (bessel_J(x, alpha, M) * np.cos(alpha * c.pi)  - bessel_J(x, -1 * alpha, M)) / np.sin(alpha * c.pi)
 67    return Y
 68
 69def bessel_H1(x, alpha, M=20):
 70    H1 = bessel_J(x,alpha,M) + 1j * bessel_Y(x, alpha, M)
 71    return H1
 72@profile
 73def spherical_bessel_j(x, alpha, M=20, spherical_x = None):
 74    if spherical_x is None: spherical_x = np.sqrt(c.pi / (2 * x)) 
 75    j = spherical_x * bessel_J(x, alpha+1/2, M)
 76    # print('j', j, scipy.special.spherical_jn(alpha, x))
 77    return j
 78
 79def spherical_bessel_j_grad(x, alpha, M=20):    
 80    # jgrad =  spherical_bessel_j(x, alpha -1, M=M) - (alpha+1)/x * spherical_bessel_j(x, alpha, M=M) 
 81    jgrad =   -1 * spherical_bessel_j(x, alpha+1, M) + (alpha / x) * spherical_bessel_j(x, alpha, M)
 82    # print(scipy.special.spherical_jn(alpha, x, True) / jgrad)
 83    
 84    return jgrad
 85@profile
 86def spherical_bessel_y(x, alpha, M=20, spherical_x = None):
 87    # if alpha <0: print('Y AAAH')
 88    if spherical_x is None:  spherical_x = np.sqrt(c.pi / (2 * x)) 
 89    y = spherical_x * bessel_Y(x, alpha+1/2, M)    
 90    # print('y', y, scipy.special.spherical_yn(alpha, x))
 91    return y
 92
 93def spherical_bessel_y_grad(x, alpha, M=20):
 94    # ygrad =  spherical_bessel_y(x, alpha -1, M=M) - (alpha+1)/x * spherical_bessel_y(x, alpha, M=M) 
 95    ygrad = -1 * spherical_bessel_y(x, alpha+1, M) + (alpha / x) * spherical_bessel_y(x, alpha, M)
 96
 97    return ygrad
 98
 99@profile
100def spherical_bessel_h1(x, alpha, M=20):
101    spherical_x = np.sqrt(c.pi / (2 * x)) 
102    h1 = spherical_bessel_j(x, alpha, M, spherical_x=spherical_x) + 1j * spherical_bessel_y(x, alpha, M, spherical_x=spherical_x)
103    return h1
104    
105def spherical_bessel_h1_grad(x, alpha, M=20):
106    
107    
108    # h1grad_ = spherical_bessel_h1(x, alpha -1, M=M) - (alpha+1)/x * spherical_bessel_h1(x, alpha, M=M) 
109    h1grad = -1 * spherical_bessel_h1(x, alpha+1, M) + (alpha / x) * spherical_bessel_h1(x, alpha, M)
110    
111    return h1grad
112    
@profile
def bessel_J(x, alpha, M=20, depth=0, max_depth=3):
11@profile
12#Seems like this is wrong? Go over this again
13def bessel_J(x, alpha, M=20, depth=0, max_depth=3):
14    
15    # print(alpha, x)
16    
17    return scipy.special.jv(alpha, x)
18    '''
19    if type(x) == torch.Tensor:
20        x_key = hash(str(torch.hash_tensor(x.real).item()) + str(x.shape))
21        # x_key = hash(tuple(x.reshape(-1).real.tolist()))
22
23    else:
24        x_key = x
25    
26    # print(x_key)
27
28    
29    if (x_key, alpha) in _CACHE_J:
30        J, d =  _CACHE_J[(x_key, alpha)]
31    
32    if alpha >= -1 and alpha <= 1:
33        J =  scipy.special.jv(alpha, x)
34        _CACHE_J[(x_key, alpha)] = (J, depth)
35        return J
36    
37    if alpha < 0:
38
39        Jplus = bessel_J(x, alpha+1, M=M, depth=depth+1)
40        _CACHE_J[(x_key, alpha+1)] = (Jplus, depth)
41        
42        Jplus_2 = bessel_J(x, alpha+2, M=M, depth=depth+1)
43        _CACHE_J[(x_key, alpha+2)] = (Jplus_2, depth)
44        
45        J = (2*(alpha+1))/x * Jplus - Jplus_2
46        _CACHE_J[(x_key, alpha)] = (J, depth)
47    
48    else:      
49        Jminus = bessel_J(x, alpha-1, M=M, depth=depth+1)
50        _CACHE_J[(x_key, alpha-1)] = (Jminus, depth)
51        
52        Jminus_2 = bessel_J(x, alpha-2, M=M, depth=depth+1)
53        _CACHE_J[(x_key, alpha-2)] = (Jminus_2, depth)
54        
55        J = (2*(alpha-1))/x * Jminus - Jminus_2
56        _CACHE_J[(x_key, alpha)] = (J, depth)
57
58    
59    # print(alpha, ( J / scipy.special.jv(alpha, x)) )
60    # print(len(_CACHE_J))
61    # exit()
62    return J
63    '''
def bessel_Y(x, alpha, M=20):
66def bessel_Y(x, alpha, M=20):
67    Y = (bessel_J(x, alpha, M) * np.cos(alpha * c.pi)  - bessel_J(x, -1 * alpha, M)) / np.sin(alpha * c.pi)
68    return Y
def bessel_H1(x, alpha, M=20):
70def bessel_H1(x, alpha, M=20):
71    H1 = bessel_J(x,alpha,M) + 1j * bessel_Y(x, alpha, M)
72    return H1
@profile
def spherical_bessel_j(x, alpha, M=20, spherical_x=None):
73@profile
74def spherical_bessel_j(x, alpha, M=20, spherical_x = None):
75    if spherical_x is None: spherical_x = np.sqrt(c.pi / (2 * x)) 
76    j = spherical_x * bessel_J(x, alpha+1/2, M)
77    # print('j', j, scipy.special.spherical_jn(alpha, x))
78    return j
def spherical_bessel_j_grad(x, alpha, M=20):
80def spherical_bessel_j_grad(x, alpha, M=20):    
81    # jgrad =  spherical_bessel_j(x, alpha -1, M=M) - (alpha+1)/x * spherical_bessel_j(x, alpha, M=M) 
82    jgrad =   -1 * spherical_bessel_j(x, alpha+1, M) + (alpha / x) * spherical_bessel_j(x, alpha, M)
83    # print(scipy.special.spherical_jn(alpha, x, True) / jgrad)
84    
85    return jgrad
@profile
def spherical_bessel_y(x, alpha, M=20, spherical_x=None):
86@profile
87def spherical_bessel_y(x, alpha, M=20, spherical_x = None):
88    # if alpha <0: print('Y AAAH')
89    if spherical_x is None:  spherical_x = np.sqrt(c.pi / (2 * x)) 
90    y = spherical_x * bessel_Y(x, alpha+1/2, M)    
91    # print('y', y, scipy.special.spherical_yn(alpha, x))
92    return y
def spherical_bessel_y_grad(x, alpha, M=20):
94def spherical_bessel_y_grad(x, alpha, M=20):
95    # ygrad =  spherical_bessel_y(x, alpha -1, M=M) - (alpha+1)/x * spherical_bessel_y(x, alpha, M=M) 
96    ygrad = -1 * spherical_bessel_y(x, alpha+1, M) + (alpha / x) * spherical_bessel_y(x, alpha, M)
97
98    return ygrad
@profile
def spherical_bessel_h1(x, alpha, M=20):
100@profile
101def spherical_bessel_h1(x, alpha, M=20):
102    spherical_x = np.sqrt(c.pi / (2 * x)) 
103    h1 = spherical_bessel_j(x, alpha, M, spherical_x=spherical_x) + 1j * spherical_bessel_y(x, alpha, M, spherical_x=spherical_x)
104    return h1
def spherical_bessel_h1_grad(x, alpha, M=20):
106def spherical_bessel_h1_grad(x, alpha, M=20):
107    
108    
109    # h1grad_ = spherical_bessel_h1(x, alpha -1, M=M) - (alpha+1)/x * spherical_bessel_h1(x, alpha, M=M) 
110    h1grad = -1 * spherical_bessel_h1(x, alpha+1, M) + (alpha / x) * spherical_bessel_h1(x, alpha, M)
111    
112    return h1grad