bessel.cc 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 <complex>
  28. #include <cmath>
  29. #include <stdexcept>
  30. #include <vector>
  31. namespace nmie {
  32. // Implementation of Bessel functions from Bohren and Huffman book, pp. 86-87, eq 4.11
  33. // Calculate all orders of function from 0 to nmax (included) for argument rho
  34. std::vector< std::complex<double> > bessel_j(int nmax, std::complex<double> rho) {
  35. if (nmax < 0) throw std::invalid_argument("Bessel order should be >= 0 (nmie::bessel_j)\n");
  36. std::vector< std::complex<double> > j(nmax+1);
  37. j[0] = std::sin(rho)/rho;
  38. if (nmax == 0) return j;
  39. j[1] = std::sin(rho)/(rho*rho) - std::cos(rho)/rho;
  40. if (nmax == 1) return j;
  41. for (int i = 2; i < n+1; ++i) {
  42. int n = i - 1;
  43. j[n+1] = static_cast<double>(2*n+1)/rho*j[n] - j[n-1];
  44. }
  45. }
  46. // Implementation of Bessel functions from Bohren and Huffman book, pp. 86-87, eq 4.11
  47. // Calculate all orders of function from 0 to nmax (included) for argument rho
  48. std::vector< std::complex<double> > bessel_y(int nmax, std::complex<double> rho) {
  49. if (nmax < 0) throw std::invalid_argument("Bessel order should be >= 0 (nmie::bessel_j)\n");
  50. std::vector< std::complex<double> > j(nmax+1);
  51. j[0] = -std::cos(rho)/rho;
  52. if (nmax == 0) return j;
  53. j[1] = -std::cos(rho)/(rho*rho) - std::sin(rho)/rho;
  54. if (nmax == 1) return j;
  55. for (int i = 2; i < n+1; ++i) {
  56. int n = i - 1;
  57. j[n+1] = static_cast<double>(2*n+1)/rho*j[n] - j[n-1];
  58. }
  59. }
  60. } // end of namespace nmie