mpmath_riccati_bessel.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/env python3
  2. import mpmath as mp
  3. # APPLIED OPTICS / Vol. 53, No. 31 / 1 November 2014, eq(13)
  4. def LeRu_cutoff(z):
  5. x = mp.fabs(z)
  6. return int(x + 11 * x**(1/3) + 1)
  7. # Wu, Wang, Radio Science, Volume 26, Number 6, Pages 1393-1401, November-December 1991,
  8. # after eq 13f.
  9. # Riccati-Bessel z*j_n(z)
  10. def psi(n,z):
  11. return mp.sqrt( (mp.pi * z)/2 ) * mp.autoprec(mp.besselj)(n+1/2,z)
  12. # Riccati-Bessel -z*y_n(z)
  13. def xi(n,z):
  14. return -mp.sqrt( (mp.pi * z)/2 ) * mp.autoprec(mp.bessely)(n+1/2,z)
  15. # Riccati-Bessel psi - i* xi
  16. def ksi(n,z):
  17. return psi(n,z) - 1.j * xi(n,z)
  18. def psi_div_ksi(n,z):
  19. return psi(n,z)/ksi(n,z)
  20. def psi_mul_ksi(n,z):
  21. return psi(n,z)*ksi(n,z)
  22. def psi_div_xi(n,z):
  23. return psi(n,z)/xi(n,z)
  24. # to compare r(n,z) with Wolfram Alpha
  25. # n=49, z=1.3-2.1i, SphericalBesselJ[n-1,z]/SphericalBesselJ[n,z]
  26. def r(n,z):
  27. if n > 0:
  28. return psi(n-1,z)/psi(n,z)
  29. return mp.cos(z)/mp.sin(z)
  30. def D(n, z, f):
  31. return f(n-1,z)/f(n,z) - n/z
  32. def D1(n,z):
  33. if n == 0: return mp.cos(z)/mp.sin(z)
  34. return D(n, z, psi)
  35. # Wolfram Alpha example D2(10, 10-10j): SphericalBesselY[9, 10-10i]/SphericalBesselY[10,10-10i]-10/(10-10i)
  36. def D2(n,z):
  37. if n == 0: return -mp.sin(z)/mp.cos(z)
  38. return D(n, z, xi)
  39. def D3(n,z):
  40. if n == 0: return 1j
  41. return D(n, z, ksi)
  42. # bulk sphere
  43. # Ovidio
  44. def an(n, x, m):
  45. # print(f'D1 = {D1(n, m*x)}\n\
  46. # psi_n = {psi(n,x)}\n\
  47. # psi_nm1 = {psi(n-1,x)}\n\
  48. # ksi_n = {ksi(n,x)}\n\
  49. # ksi_nm1 = {ksi(n-1,x)}\n\
  50. # ')
  51. return (
  52. ( ( D1(n, m*x)/m + n/x )*psi(n,x) - psi(n-1,x) ) /
  53. ( ( D1(n, m*x)/m + n/x )*ksi(n,x) - ksi(n-1,x) )
  54. )
  55. def bn(n, x, m):
  56. # print(f'D1 = {D1(n, m*x)}\n\
  57. # psi_n = {psi(n,x)}\n\
  58. # psi_nm1 = {psi(n-1,x)}\n\
  59. # ksi_n = {ksi(n,x)}\n\
  60. # ksi_nm1 = {ksi(n-1,x)}\n\
  61. # ')
  62. return (
  63. ( ( D1(n, m*x)*m + n/x ) * psi(n,x) - psi(n-1,x) ) /
  64. ( ( D1(n, m*x)*m + n/x ) * ksi(n,x) - ksi(n-1,x) )
  65. )
  66. # # Du
  67. # def an(n, x, m):
  68. # mx = m*x
  69. # return (
  70. # ((r(n,mx)/m + n*(1-1/m**2)/x)*psi(n,x)-psi(n-1,x))/
  71. # ((r(n,mx)/m + n*(1-1/m**2)/x)*ksi(n,x)-ksi(n-1,x))
  72. # )
  73. # def bn(n,x,m):
  74. # mx = m*x
  75. # return (
  76. # (r(n,mx)*m*psi(n,x)-psi(n-1,x))/
  77. # (r(n,mx)*m*ksi(n,x)-ksi(n-1,x))
  78. # )
  79. def Qext_diff(n,x,m):
  80. return ((2*n +1)*2/x**2) * (an(n,x,m) + bn(n,x,m)).real
  81. def Qsca_diff(n,x,m):
  82. return ((2*n +1)*2/x**2) * (abs(an(n,x,m))**2 + abs(bn(n,x,m))**2)
  83. def Qany(x, m, nmax, output_dps, Qdiff):
  84. Qany = 0
  85. Qlist = []
  86. Qprev=""
  87. convergence_max_length = 35
  88. i = 0
  89. for n in range(1, nmax+1):
  90. diff = Qdiff(n,x,m)
  91. Qany += diff
  92. Qnext = mp.nstr(Qany, output_dps)
  93. Qlist.append(Qany)
  94. i += 1
  95. if Qprev !=Qnext: i = 0
  96. Qprev = Qnext
  97. print(n, ' ', end='', flush=True)
  98. if i >= convergence_max_length: break
  99. print('')
  100. Qlist.append(Qany)
  101. return Qlist
  102. def Qsca(x, m, nmax, output_dps):
  103. return Qany(x, m, nmax, output_dps, Qsca_diff)
  104. def Qext(x, m, nmax, output_dps):
  105. return Qany(x, m, nmax, output_dps, Qext_diff)