bessel.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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(int n, std::complex<double>z, std::vector< std::complex<double> >& Zeta,
  37. std::vector< std::complex<double> >& dZeta) {
  38. std::vector< std::complex<double> > csj, cdj, csy, cdy;
  39. int nm;
  40. csphjy (n, z, nm, csj, cdj, csy, cdy );
  41. Zeta.resize(n+1);
  42. dZeta.resize(n+1);
  43. auto c_i = std::complex<double>(0.0,1.0);
  44. for (int i = 0; i < n+1; ++i) {
  45. Zeta[i]=z*(csj[i] + c_i*csy[i]);
  46. dZeta[i]=z*(cdj[i] + c_i*cdy[i])+csj[i] + c_i*csy[i];
  47. }
  48. } // end of calcZeta()
  49. void calcPsi(int n, std::complex<double>z, std::vector< std::complex<double> >& Psi,
  50. std::vector< std::complex<double> >& dPsi) {
  51. std::vector< std::complex<double> > csj, cdj, csy, cdy;
  52. int nm;
  53. csphjy (n, z, nm, csj, cdj, csy, cdy );
  54. Psi.resize(n+1);
  55. dPsi.resize(n+1);
  56. for (int i = 0; i < n+1; ++i) {
  57. Psi[i]=z*(csj[i]);
  58. dPsi[i]=z*(cdj[i])+csj[i];
  59. }
  60. } // end of calcPsi()
  61. // !*****************************************************************************80
  62. //
  63. // C++ port of fortran code
  64. //
  65. // !! CSPHJY: spherical Bessel functions jn(z) and yn(z) for complex argument.
  66. // !
  67. // ! Discussion:
  68. // !
  69. // ! This procedure computes spherical Bessel functions jn(z) and yn(z)
  70. // ! and their derivatives for a complex argument.
  71. // !
  72. // ! Licensing:
  73. // !
  74. // ! This routine is copyrighted by Shanjie Zhang and Jianming Jin. However,
  75. // ! they give permission to incorporate this routine into a user program
  76. // ! provided that the copyright is acknowledged.
  77. // !
  78. // ! Modified:
  79. // !
  80. // ! 01 August 2012
  81. // !
  82. // ! Author:
  83. // !
  84. // ! Shanjie Zhang, Jianming Jin
  85. // !
  86. // ! Reference:
  87. // !
  88. // ! Shanjie Zhang, Jianming Jin,
  89. // ! Computation of Special Functions,
  90. // ! Wiley, 1996,
  91. // ! ISBN: 0-471-11963-6,
  92. // ! LC: QA351.C45.
  93. // !
  94. // ! Parameters:
  95. // !
  96. // ! Input, integer ( kind = 4 ) N, the order of jn(z) and yn(z).
  97. // !
  98. // ! Input, complex ( kind = 8 ) Z, the argument.
  99. // !
  100. // ! Output, integer ( kind = 4 ) NM, the highest order computed.
  101. // !
  102. // ! Output, complex ( kind = 8 ) CSJ(0:N0, CDJ(0:N), CSY(0:N), CDY(0:N),
  103. // ! the values of jn(z), jn'(z), yn(z), yn'(z).
  104. // !
  105. void csphjy (int n, std::complex<double>z, int& nm,
  106. std::vector< std::complex<double> >& csj,
  107. std::vector< std::complex<double> >& cdj,
  108. std::vector< std::complex<double> >& csy,
  109. std::vector< std::complex<double> >& cdy ) {
  110. double a0;
  111. csj.resize(n+1);
  112. cdj.resize(n+1);
  113. csy.resize(n+1);
  114. cdy.resize(n+1);
  115. std::complex<double> cf, cf0, cf1, cs, csa, csb;
  116. int m;
  117. a0 = std::abs(z);
  118. nm = n;
  119. if (a0 < 1.0e-60) {
  120. for (int k = 0; k < n+1; ++k) {
  121. csj[k] = 0.0;
  122. cdj[k] = 0.0;
  123. csy[k] = -1.0e+300;
  124. cdy[k] = 1.0e+300;
  125. }
  126. csj[0] = std::complex<double>( 1.0, 0.0);
  127. cdj[1] = std::complex<double>( 0.3333333333333333, 0.0);
  128. return;
  129. }
  130. csj[0] = std::sin ( z ) / z;
  131. csj[1] = ( csj[0] - std::cos ( z ) ) / z;
  132. if ( 2 <= n ) {
  133. csa = csj[0];
  134. csb = csj[1];
  135. m = msta1 ( a0, 200 );
  136. if ( m < n ) nm = m;
  137. else m = msta2 ( a0, n, 15 );
  138. cf0 = 0.0;
  139. cf1 = 1.0e-100;
  140. for (int k = m; k>=0; --k) {
  141. cf = ( 2.0 * k + 3.0 ) * cf1 / z - cf0;
  142. if ( k <= nm ) csj[k] = cf;
  143. cf0 = cf1;
  144. cf1 = cf;
  145. }
  146. if ( std::abs ( csa ) <= std::abs ( csb ) ) cs = csb / cf0;
  147. else cs = csa / cf;
  148. for (int k = 0; k <= nm; ++k) {
  149. csj[k] = cs * csj[k];
  150. }
  151. }
  152. cdj[0] = ( std::cos ( z ) - std::sin ( z ) / z ) / z;
  153. for (int k = 1; k <=nm; ++k) {
  154. cdj[k] = csj[k-1] - ( k + 1.0 ) * csj[k] / z;
  155. }
  156. csy[0] = - std::cos ( z ) / z;
  157. csy[1] = ( csy[0] - std::sin ( z ) ) / z;
  158. cdy[0] = ( std::sin ( z ) + std::cos ( z ) / z ) / z;
  159. cdy[1] = ( 2.0 * cdy[0] - std::cos ( z ) ) / z;
  160. for (int k = 2; k<=nm; ++k) {
  161. if ( std::abs ( csj[k-2] ) < std::abs ( csj[k-1] ) ) {
  162. csy[k] = ( csj[k] * csy[k-1] - 1.0 / ( z * z ) ) / csj[k-1];
  163. } else {
  164. csy[k] = ( csj[k] * csy[k-2] - ( 2.0 * k - 1.0 ) / std::pow(z,3) )
  165. / csj[k-2];
  166. }
  167. }
  168. for (int k = 2; k<=nm; ++k) {
  169. cdy[k] = csy[k-1] - ( k + 1.0 ) * csy[k] / z;
  170. }
  171. return;
  172. }
  173. // function msta2 ( x, n, mp )
  174. // !*****************************************************************************80
  175. // !
  176. // !! MSTA2 determines a backward recurrence starting point for Jn(x).
  177. // !
  178. // ! Discussion:
  179. // !
  180. // ! This procedure determines the starting point for a backward
  181. // ! recurrence such that all Jn(x) has MP significant digits.
  182. // !
  183. // ! Licensing:
  184. // !
  185. // ! This routine is copyrighted by Shanjie Zhang and Jianming Jin. However,
  186. // ! they give permission to incorporate this routine into a user program
  187. // ! provided that the copyright is acknowledged.
  188. // !
  189. // ! Modified:
  190. // !
  191. // ! 08 July 2012
  192. // !
  193. // ! Author:
  194. // !
  195. // ! Shanjie Zhang, Jianming Jin
  196. // !
  197. // ! Reference:
  198. // !
  199. // ! Shanjie Zhang, Jianming Jin,
  200. // ! Computation of Special Functions,
  201. // ! Wiley, 1996,
  202. // ! ISBN: 0-471-11963-6,
  203. // ! LC: QA351.C45.
  204. // !
  205. // ! Parameters:
  206. // !
  207. // ! Input, real ( kind = 8 ) X, the argument of Jn(x).
  208. // !
  209. // ! Input, integer ( kind = 4 ) N, the order of Jn(x).
  210. // !
  211. // ! Input, integer ( kind = 4 ) MP, the number of significant digits.
  212. // !
  213. // ! Output, integer ( kind = 4 ) MSTA2, the starting point.
  214. // !
  215. int msta2 ( double x, int n, int mp ) {
  216. double a0, ejn, f, f0, f1, hmp;
  217. int n0, n1, nn;
  218. double obj;
  219. a0 = std::abs ( x );
  220. hmp = 0.5 * mp;
  221. ejn = envj ( n, a0 );
  222. if ( ejn <= hmp ) {
  223. obj = mp;
  224. n0 = static_cast<int> ( 1.1 * a0 );
  225. } else {
  226. obj = hmp + ejn;
  227. n0 = n;
  228. }
  229. f0 = envj ( n0, a0 ) - obj;
  230. n1 = n0 + 5;
  231. f1 = envj ( n1, a0 ) - obj;
  232. for (int it = 1; it < 21; ++it) {
  233. nn = n1 - ( n1 - n0 ) / ( 1.0 - f0 / f1 );
  234. f = envj ( nn, a0 ) - obj;
  235. if ( std::abs ( nn - n1 ) < 1 ) break;
  236. n0 = n1;
  237. f0 = f1;
  238. n1 = nn;
  239. f1 = f;
  240. }
  241. return nn + 10;
  242. }
  243. // !*****************************************************************************80
  244. // !
  245. // !! MSTA1 determines a backward recurrence starting point for Jn(x).
  246. // !
  247. // ! Discussion:
  248. // !
  249. // ! This procedure determines the starting point for backward
  250. // ! recurrence such that the magnitude of
  251. // ! Jn(x) at that point is about 10^(-MP).
  252. // !
  253. // ! Licensing:
  254. // !
  255. // ! This routine is copyrighted by Shanjie Zhang and Jianming Jin. However,
  256. // ! they give permission to incorporate this routine into a user program
  257. // ! provided that the copyright is acknowledged.
  258. // !
  259. // ! Modified:
  260. // !
  261. // ! 08 July 2012
  262. // !
  263. // ! Author:
  264. // !
  265. // ! Shanjie Zhang, Jianming Jin
  266. // !
  267. // ! Reference:
  268. // !
  269. // ! Shanjie Zhang, Jianming Jin,
  270. // ! Computation of Special Functions,
  271. // ! Wiley, 1996,
  272. // ! ISBN: 0-471-11963-6,
  273. // ! LC: QA351.C45.
  274. // !
  275. // ! Parameters:
  276. // !
  277. // ! Input, real ( kind = 8 ) X, the argument.
  278. // !
  279. // ! Input, integer ( kind = 4 ) MP, the negative logarithm of the
  280. // ! desired magnitude.
  281. // !
  282. // ! Output, integer ( kind = 4 ) MSTA1, the starting point.
  283. // !
  284. int msta1 ( double x, int mp ) {
  285. double a0, f, f0, f1;
  286. int n0, n1, nn;
  287. a0 = std::abs ( x );
  288. n0 = static_cast<int> (1.1 * a0 ) + 1;
  289. f0 = envj ( n0, a0 ) - mp;
  290. n1 = n0 + 5;
  291. f1 = envj ( n1, a0 ) - mp;
  292. for (int it = 1; it <= 20; ++it) {
  293. nn = n1 - ( n1 - n0 ) / ( 1.0 - f0 / f1 );
  294. f = envj ( nn, a0 ) - mp;
  295. if ( abs ( nn - n1 ) < 1 ) break;
  296. n0 = n1;
  297. f0 = f1;
  298. n1 = nn;
  299. f1 = f;
  300. }
  301. return nn;
  302. }
  303. // function envj ( n, x )
  304. // !*****************************************************************************80
  305. // !
  306. // !! ENVJ is a utility function used by MSTA1 and MSTA2.
  307. // !
  308. // ! Licensing:
  309. // !
  310. // ! This routine is copyrighted by Shanjie Zhang and Jianming Jin. However,
  311. // ! they give permission to incorporate this routine into a user program
  312. // ! provided that the copyright is acknowledged.
  313. // !
  314. // ! Modified:
  315. // !
  316. // ! 14 March 2012
  317. // !
  318. // ! Author:
  319. // !
  320. // ! Shanjie Zhang, Jianming Jin
  321. // !
  322. // ! Reference:
  323. // !
  324. // ! Shanjie Zhang, Jianming Jin,
  325. // ! Computation of Special Functions,
  326. // ! Wiley, 1996,
  327. // ! ISBN: 0-471-11963-6,
  328. // ! LC: QA351.C45.
  329. // !
  330. // ! Parameters:
  331. // !
  332. // ! Input, integer ( kind = 4 ) N, ?
  333. // !
  334. // ! Input, real ( kind = 8 ) X, ?
  335. // !
  336. // ! Output, real ( kind = 8 ) ENVJ, ?
  337. // !
  338. double envj (int n, double x ) {
  339. return 0.5 * std::log10(6.28 * n) - n * std::log10(1.36 * x / static_cast<double>(n) );
  340. }
  341. } // end of namespace bessel
  342. } // end of namespace nmie