bessel.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. //**********************************************************************************//
  2. // Copyright (C) 2009-2015 Ovidio Pena <ovidio@bytesfall.com> //
  3. // Copyright (C) 2013-2015 Konstantin Ladutenko <kostyfisik@gmail.com> //
  4. // //
  5. // This file is part of scattnlay //
  6. // //
  7. // This program is free software: you can redistribute it and/or modify //
  8. // it under the terms of the GNU General Public License as published by //
  9. // the Free Software Foundation, either version 3 of the License, or //
  10. // (at your option) any later version. //
  11. // //
  12. // This program is distributed in the hope that it will be useful, //
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of //
  14. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
  15. // GNU General Public License for more details. //
  16. // //
  17. // The only additional remark is that we expect that all publications //
  18. // describing work using this software, or all commercial products //
  19. // using it, cite the following reference: //
  20. // [1] O. Pena and U. Pal, "Scattering of electromagnetic radiation by //
  21. // a multilayered sphere," Computer Physics Communications, //
  22. // vol. 180, Nov. 2009, pp. 2348-2354. //
  23. // //
  24. // You should have received a copy of the GNU General Public License //
  25. // along with this program. If not, see <http://www.gnu.org/licenses/>. //
  26. //**********************************************************************************//
  27. #include "bessel.h"
  28. #include <algorithm>
  29. #include <complex>
  30. #include <cmath>
  31. #include <limits>
  32. #include <stdexcept>
  33. #include <vector>
  34. namespace nmie {
  35. namespace bessel {
  36. void calcZeta( std::vector< std::complex<double> >& Zeta,
  37. std::vector< std::complex<double> >& dZeta,
  38. int n, std::complex<double>z) {
  39. std::vector< std::complex<double> > csj, cdj, csy, cdy;
  40. int nm;
  41. csphjy (n, z, nm, csj, cdj, csy, cdy );
  42. Zeta.resize(n+1);
  43. dZeta.resize(n+1);
  44. auto c_i = std::complex<double>(0.0,1.0);
  45. for (int i = 0; i < n+1; ++i) {
  46. Zeta[i]=z*(csj[i] + c_i*csy[i]);
  47. dZeta[i]=z*(cdj[i] + c_i*cdy[i])+csj[i] + c_i*csy[i];
  48. }
  49. } // end of calcZeta()
  50. // !*****************************************************************************80
  51. //
  52. // C++ port of fortran code
  53. //
  54. // !! CSPHJY: spherical Bessel functions jn(z) and yn(z) for complex argument.
  55. // !
  56. // ! Discussion:
  57. // !
  58. // ! This procedure computes spherical Bessel functions jn(z) and yn(z)
  59. // ! and their derivatives for a complex argument.
  60. // !
  61. // ! Licensing:
  62. // !
  63. // ! This routine is copyrighted by Shanjie Zhang and Jianming Jin. However,
  64. // ! they give permission to incorporate this routine into a user program
  65. // ! provided that the copyright is acknowledged.
  66. // !
  67. // ! Modified:
  68. // !
  69. // ! 01 August 2012
  70. // !
  71. // ! Author:
  72. // !
  73. // ! Shanjie Zhang, Jianming Jin
  74. // !
  75. // ! Reference:
  76. // !
  77. // ! Shanjie Zhang, Jianming Jin,
  78. // ! Computation of Special Functions,
  79. // ! Wiley, 1996,
  80. // ! ISBN: 0-471-11963-6,
  81. // ! LC: QA351.C45.
  82. // !
  83. // ! Parameters:
  84. // !
  85. // ! Input, integer ( kind = 4 ) N, the order of jn(z) and yn(z).
  86. // !
  87. // ! Input, complex ( kind = 8 ) Z, the argument.
  88. // !
  89. // ! Output, integer ( kind = 4 ) NM, the highest order computed.
  90. // !
  91. // ! Output, complex ( kind = 8 ) CSJ(0:N0, CDJ(0:N), CSY(0:N), CDY(0:N),
  92. // ! the values of jn(z), jn'(z), yn(z), yn'(z).
  93. // !
  94. void csphjy (int n, std::complex<double>z, int& nm,
  95. std::vector< std::complex<double> >& csj,
  96. std::vector< std::complex<double> >& cdj,
  97. std::vector< std::complex<double> >& csy,
  98. std::vector< std::complex<double> >& cdy ) {
  99. double a0;
  100. csj.resize(n+1);
  101. cdj.resize(n+1);
  102. csy.resize(n+1);
  103. cdy.resize(n+1);
  104. std::complex<double> cf, cf0, cf1, cs, csa, csb;
  105. int m;
  106. a0 = std::abs(z);
  107. nm = n;
  108. if (a0 < 1.0e-60) {
  109. for (int k = 0; k < n+1; ++k) {
  110. csj[k] = 0.0;
  111. cdj[k] = 0.0;
  112. csy[k] = -1.0e+300;
  113. cdy[k] = 1.0e+300;
  114. }
  115. csj[0] = std::complex<double>( 1.0, 0.0);
  116. cdj[1] = std::complex<double>( 0.3333333333333333, 0.0);
  117. return;
  118. }
  119. csj[0] = std::sin ( z ) / z;
  120. csj[1] = ( csj[0] - std::cos ( z ) ) / z;
  121. if ( 2 <= n ) {
  122. csa = csj[0];
  123. csb = csj[1];
  124. m = msta1 ( a0, 200 );
  125. if ( m < n ) nm = m;
  126. else m = msta2 ( a0, n, 15 );
  127. cf0 = 0.0;
  128. cf1 = 1.0e-100;
  129. for (int k = m; k>=0; --k) {
  130. cf = ( 2.0 * k + 3.0 ) * cf1 / z - cf0;
  131. if ( k <= nm ) csj[k] = cf;
  132. cf0 = cf1;
  133. cf1 = cf;
  134. }
  135. if ( std::abs ( csa ) <= std::abs ( csb ) ) cs = csb / cf0;
  136. else cs = csa / cf;
  137. for (int k = 0; k <= nm; ++k) {
  138. csj[k] = cs * csj[k];
  139. }
  140. }
  141. cdj[0] = ( std::cos ( z ) - std::sin ( z ) / z ) / z;
  142. for (int k = 1; k <=nm; ++k) {
  143. cdj[k] = csj[k-1] - ( k + 1.0 ) * csj[k] / z;
  144. }
  145. csy[0] = - std::cos ( z ) / z;
  146. csy[1] = ( csy[0] - std::sin ( z ) ) / z;
  147. cdy[0] = ( std::sin ( z ) + std::cos ( z ) / z ) / z;
  148. cdy[1] = ( 2.0 * cdy[0] - std::cos ( z ) ) / z;
  149. for (int k = 2; k<=nm; ++k) {
  150. if ( std::abs ( csj[k-2] ) < std::abs ( csj[k-1] ) ) {
  151. csy[k] = ( csj[k] * csy[k-1] - 1.0 / ( z * z ) ) / csj[k-1];
  152. } else {
  153. csy[k] = ( csj[k] * csy[k-2] - ( 2.0 * k - 1.0 ) / std::pow(z,3) )
  154. / csj[k-2];
  155. }
  156. }
  157. for (int k = 2; k<=nm; ++k) {
  158. cdy[k] = csy[k-1] - ( k + 1.0 ) * csy[k] / z;
  159. }
  160. return;
  161. }
  162. // function msta2 ( x, n, mp )
  163. // !*****************************************************************************80
  164. // !
  165. // !! MSTA2 determines a backward recurrence starting point for Jn(x).
  166. // !
  167. // ! Discussion:
  168. // !
  169. // ! This procedure determines the starting point for a backward
  170. // ! recurrence such that all Jn(x) has MP significant digits.
  171. // !
  172. // ! Licensing:
  173. // !
  174. // ! This routine is copyrighted by Shanjie Zhang and Jianming Jin. However,
  175. // ! they give permission to incorporate this routine into a user program
  176. // ! provided that the copyright is acknowledged.
  177. // !
  178. // ! Modified:
  179. // !
  180. // ! 08 July 2012
  181. // !
  182. // ! Author:
  183. // !
  184. // ! Shanjie Zhang, Jianming Jin
  185. // !
  186. // ! Reference:
  187. // !
  188. // ! Shanjie Zhang, Jianming Jin,
  189. // ! Computation of Special Functions,
  190. // ! Wiley, 1996,
  191. // ! ISBN: 0-471-11963-6,
  192. // ! LC: QA351.C45.
  193. // !
  194. // ! Parameters:
  195. // !
  196. // ! Input, real ( kind = 8 ) X, the argument of Jn(x).
  197. // !
  198. // ! Input, integer ( kind = 4 ) N, the order of Jn(x).
  199. // !
  200. // ! Input, integer ( kind = 4 ) MP, the number of significant digits.
  201. // !
  202. // ! Output, integer ( kind = 4 ) MSTA2, the starting point.
  203. // !
  204. int msta2 ( double x, int n, int mp ) {
  205. double a0, ejn, f, f0, f1, hmp;
  206. int n0, n1, nn;
  207. double obj;
  208. a0 = std::abs ( x );
  209. hmp = 0.5 * mp;
  210. ejn = envj ( n, a0 );
  211. if ( ejn <= hmp ) {
  212. obj = mp;
  213. n0 = static_cast<int> ( 1.1 * a0 );
  214. } else {
  215. obj = hmp + ejn;
  216. n0 = n;
  217. }
  218. f0 = envj ( n0, a0 ) - obj;
  219. n1 = n0 + 5;
  220. f1 = envj ( n1, a0 ) - obj;
  221. for (int it = 1; it < 21; ++it) {
  222. nn = n1 - ( n1 - n0 ) / ( 1.0 - f0 / f1 );
  223. f = envj ( nn, a0 ) - obj;
  224. if ( std::abs ( nn - n1 ) < 1 ) break;
  225. n0 = n1;
  226. f0 = f1;
  227. n1 = nn;
  228. f1 = f;
  229. }
  230. return nn + 10;
  231. }
  232. // !*****************************************************************************80
  233. // !
  234. // !! MSTA1 determines a backward recurrence starting point for Jn(x).
  235. // !
  236. // ! Discussion:
  237. // !
  238. // ! This procedure determines the starting point for backward
  239. // ! recurrence such that the magnitude of
  240. // ! Jn(x) at that point is about 10^(-MP).
  241. // !
  242. // ! Licensing:
  243. // !
  244. // ! This routine is copyrighted by Shanjie Zhang and Jianming Jin. However,
  245. // ! they give permission to incorporate this routine into a user program
  246. // ! provided that the copyright is acknowledged.
  247. // !
  248. // ! Modified:
  249. // !
  250. // ! 08 July 2012
  251. // !
  252. // ! Author:
  253. // !
  254. // ! Shanjie Zhang, Jianming Jin
  255. // !
  256. // ! Reference:
  257. // !
  258. // ! Shanjie Zhang, Jianming Jin,
  259. // ! Computation of Special Functions,
  260. // ! Wiley, 1996,
  261. // ! ISBN: 0-471-11963-6,
  262. // ! LC: QA351.C45.
  263. // !
  264. // ! Parameters:
  265. // !
  266. // ! Input, real ( kind = 8 ) X, the argument.
  267. // !
  268. // ! Input, integer ( kind = 4 ) MP, the negative logarithm of the
  269. // ! desired magnitude.
  270. // !
  271. // ! Output, integer ( kind = 4 ) MSTA1, the starting point.
  272. // !
  273. int msta1 ( double x, int mp ) {
  274. double a0, f, f0, f1;
  275. int n0, n1, nn;
  276. a0 = std::abs ( x );
  277. n0 = static_cast<int> (1.1 * a0 ) + 1;
  278. f0 = envj ( n0, a0 ) - mp;
  279. n1 = n0 + 5;
  280. f1 = envj ( n1, a0 ) - mp;
  281. for (int it = 1; it <= 20; ++it) {
  282. nn = n1 - ( n1 - n0 ) / ( 1.0 - f0 / f1 );
  283. f = envj ( nn, a0 ) - mp;
  284. if ( abs ( nn - n1 ) < 1 ) break;
  285. n0 = n1;
  286. f0 = f1;
  287. n1 = nn;
  288. f1 = f;
  289. }
  290. return nn;
  291. }
  292. // function envj ( n, x )
  293. // !*****************************************************************************80
  294. // !
  295. // !! ENVJ is a utility function used by MSTA1 and MSTA2.
  296. // !
  297. // ! Licensing:
  298. // !
  299. // ! This routine is copyrighted by Shanjie Zhang and Jianming Jin. However,
  300. // ! they give permission to incorporate this routine into a user program
  301. // ! provided that the copyright is acknowledged.
  302. // !
  303. // ! Modified:
  304. // !
  305. // ! 14 March 2012
  306. // !
  307. // ! Author:
  308. // !
  309. // ! Shanjie Zhang, Jianming Jin
  310. // !
  311. // ! Reference:
  312. // !
  313. // ! Shanjie Zhang, Jianming Jin,
  314. // ! Computation of Special Functions,
  315. // ! Wiley, 1996,
  316. // ! ISBN: 0-471-11963-6,
  317. // ! LC: QA351.C45.
  318. // !
  319. // ! Parameters:
  320. // !
  321. // ! Input, integer ( kind = 4 ) N, ?
  322. // !
  323. // ! Input, real ( kind = 8 ) X, ?
  324. // !
  325. // ! Output, real ( kind = 8 ) ENVJ, ?
  326. // !
  327. double envj (int n, double x ) {
  328. return 0.5 * std::log10(6.28 * n) - n * std::log10(1.36 * x / static_cast<double>(n) );
  329. }
  330. } // end of namespace bessel
  331. } // end of namespace nmie