//======================================================================== //======================================================================== // // NAME: double bessel_2(int n, double arg) // // DESC: Calculates the Bessel function of the second kind (Yn). // // INPUT: // int n:: Order of Bessel function // double arg: Bessel function argument // // OUTPUT: // Yn:: Bessel function of the second kind of order n // // NOTES: 1) The Bessel function of the second kind is defined as: // Yn(x) = (2.0*Jn(x)/pi)*(ln(x/2) + gamma_e) // + sum{m=0, m=inf}[(((-1)^(m-1))*(hs(m) + // hs(m+n)) + x^(2*m))/(((2.0^(2*m+n))*m!*(m+n)!) // - sum{m=0, m=(n-1)}[((n-m-1)!*x^(2*m))/((2.0^(2*m-n))*m!) // // 2) Y-n = ((-1)^n)*Yn // //======================================================================== //======================================================================== double bessel_2(int n, double arg) { // get a tolerance for the "infinite" sum double tol = 100.0*depsilon(); int m, mmax; double Jn, Yn, sum1, sum1_prev, sum2; sum1 = sum1_prev = sum2 = 0.0; // Make sure that we calculate the positive order Bessel function int k = abs(n); // GET THE BESSEL FUNCTION OF THE FIRST KIND Jn = bessel_1(k, arg); // !!! my concern with this do loop is that if a certain term contributes 0 then the // loop may inappropriately exit mmax = static_cast<int>(2.0*arg) + 1; m = 0; // GET TERM SUM 1 do { sum1_prev = sum1; sum1 += pow((0.0-1.0), (m-1))*(h_s(m) + h_s(m+k))*pow(arg, (2*m))/(pow(2.0, (2*m+k))*dfactorial(m)*dfactorial(m+k)); m++; } while((fabs(sum1 - sum1_prev) > tol) || (m < mmax)); sum1 = pow(arg, k)*sum1/(1.0*PI); // GET TERM SUM 2 for(m = 0; m <= (k-1); ++m) { sum2 = sum2 + dfactorial(k-m-1)*pow(arg, (2*m))/(pow(2.0, (2*m-k))*dfactorial(m)); } sum2 = pow(arg, (0-k))*sum2/(1.0*PI); // NOW GET Yn Yn = (2.0*Jn/(1.0*PI))*(log(arg/2.0) + EULERC) + sum1 - sum2; // NOW WE WILL MAKE USE OF THE BESSEL FUNCTION RELATION FOR NEGATIVE n: // Y(n-1) = (-1)^n*Yn if(n < 0) { Yn = pow((0.0-1.0), k)*Yn; } return Yn; }
//======================================================================== //======================================================================== // // NAME: complex<double> bessel_1_complex(int n, complex<double> arg) // // DESC: Calculates the Bessel function of the first kind (Jn) for // complex arguments. // // INPUT: // int n:: Order of Bessel function // complex<double> arg: Bessel function argument // // OUTPUT: // Jn:: Bessel function of the first kind of order n // // NOTES: 1) The Bessel function of the first kind is defined as: // Jn(x) = ((x/2.0)^n)*sum{m=0, m=inf}[(((-x^2)/4.0)^m)/(m!*(n+m)!) // // 2) J-n = ((-1)^n)*Jn // // 3) The infinite series representing the Bessel function // converges for all arguments given that enough terms are taken: // k >> |arg| // //======================================================================== //======================================================================== complex<double> bessel_1_complex(int n, complex<double> arg) { complex<double> Jn(0.0, 0.0); complex<double> Jn_series(0.0, 0.0); complex<double> Jn_seriesm1(0.0, 0.0); double tolerance = 100.0*depsilon(); // Make sure that we calculate the positive order Bessel function int m = abs(n); // !!! I don't know if this will be large enough int k_max_r, k_max_im; k_max_r = static_cast<int>(real(arg)); k_max_im = static_cast<int>(imag(arg)); /* int k_max = 10*imax(k_max_r, k_max_im); for(int k = 0; k <= k_max; k++) { Jn_series = Jn_series + pow((0.0 - arg*arg/4.0), k)/(dfactorial(k)*dfactorial(k+m)); } */ // !!! my concern with this do loop is that if a certain term contributes 0 then the // loop may inappropriately exit int k_max2 = static_cast<int>(2.0*static_cast<double>(imax(k_max_r, k_max_im))) + 1; int k = 0; do { Jn_seriesm1 = Jn_series; Jn_series = Jn_series + pow((0.0 - arg*arg/4.0), k)/(dfactorial(k)*dfactorial(k+m)); k = k + 1; } while ((sqrt(real(Jn_series*conj(Jn_series) - Jn_seriesm1*conj(Jn_seriesm1))) > tolerance) || (k < k_max2)); Jn = pow((arg/2.0), m)*Jn_series; // NOW WE WILL MAKE USE OF THE BESSEL FUNCTION RELATION FOR NEGATIVE n: // J(n-1) = (-1)^n*Jn if(n < 0) { Jn = pow((0.0 - 1.0), m)*Jn; } return Jn; }
//======================================================================== //======================================================================== // // NAME: double bessel_1(int n, double arg) // // DESC: Calculates the Bessel function of the first kind (Jn) of order n. // // INPUT: // int n:: Order of Bessel function // double arg: Bessel function argument // // OUTPUT: // Jn:: Bessel function of the first kind of order n // // NOTES: 1) The Bessel function of the first kind is defined as: // Jn(x) = ((x/2.0)^n)*sum{m=0, m=inf}[(((-x^2)/4.0)^m)/(m!*(n+m)!) // // 2) J-n = ((-1)^n)*Jn // // 3) The infinite series representing the Bessel function // converges for all arguments given that enough terms are taken: // k >> |arg| // //======================================================================== //======================================================================== double bessel_1(int n, double arg) { double tolerance = 100.0*depsilon(); double Jn, Jn1; Jn = Jn1 = 0.0; // make sure we calculate the positive order Bessel function int m = abs(n); // !!! my concern with this do loop is that if a certain term contributes 0 then the // loop may inappropriately exit, and I don't know if this will be large enough int k_max2 = int(2.0*arg + 1.0); int k = 0; do { Jn1 = Jn; Jn += pow(-arg*arg/4.0, k)/(dfactorial(k)*dfactorial(k+m)); k++; } while ((fabs(Jn - Jn1) > tolerance) || (k < k_max2)); Jn *= pow((arg/2.0), m); // NOW WE WILL MAKE USE OF THE BESSEL FUNCTION RELATION FOR NEGATIVE n // J(n-1) = (-1)^n*Jn if(n < 0) { Jn *= pow(-1.0, m); } return Jn; }
double factorial(int n) { if(n>=20) return sqrt(TWOPI*(n+1.0))*exp(-(n+1.0))*pow(n+1.0, n)* (1.0+8.333333333333e-2/(n+1.0) +3.472222222222e-3/(n+1.0)/(n+1.0) -2.681327160494e-3/(n+1.0)/(n+1.0)/(n+1.0) -2.294720936214e-4/(n+1.0)/(n+1.0)/(n+1.0)/(n+1.0) +7.840392217201e-4/(n+1.0)/(n+1.0)/(n+1.0)/(n+1.0)/(n+1.0) +6.972813758366e-5/(n+1.0)/(n+1.0)/(n+1.0)/(n+1.0)/(n+1.0)/(n+1.0) -5.921664373537e-4/(n+1.0)/(n+1.0)/(n+1.0)/(n+1.0)/(n+1.0)/(n+1.0)/(n+1.0) ); return dfactorial( (double) n); }
//======================================================================== //======================================================================== // // NAME: complex<double> bessel_2_complex(int n, complex<double> arg) // // DESC: Calculates the Bessel function of the second kind (Yn) for // complex arguments. // // INPUT: // int n:: Order of Bessel function // complex<double> arg: Bessel function argument // // OUTPUT: // complex<double> Yn:: Bessel function of the second kind of // order n // // NOTES: 1) The Bessel function of the second kind is defined as: // Yn(x) = (2.0*Jn(x)/pi)*(ln(x/2) + gamma_e) // + sum{m=0, m=inf}[(((-1)^(m-1))*(hs(m) + // hs(m+n)) + x^(2*m))/(((2.0^(2*m+n))*m!*(m+n)!) // - sum{m=0, m=(n-1)}[((n-m-1)!*x^(2*m))/((2.0^(2*m-n))*m!) // // 2) Y-n = ((-1)^n)*Yn // //======================================================================== //======================================================================== complex<double> bessel_2_complex(int n, complex<double> arg) { complex<double> Yn(0.0, 0.0); // get a tolerance for the "infinite" sum double tolerance = 100.0*depsilon(); // make sure we calculate the positive order Bessel function int k = abs(n); // GET BESSEL FUNCTIONS OF THE FIRST KIND complex<double> Jn = bessel_1_complex(k, arg); // GET TERM SUM 1 complex<double> sum1(0.0,0.0), sum11(0.0,0.0); /* for(int i = 0; i <= 20; i++) { sum1 = sum1 + pow((0.0-1.0), (i-1))*(h_s(i) + h_s(i+k))*pow(arg, (2*i))/(pow(2.0, (2*i+k))*dfactorial(i)*dfactorial(i+k)); } */ // !!! my concern with this do loop is that if a certain term contributes 0 then the // loop may inappropriately exit int mm_max = static_cast<int>(2.0*real(arg)) + 1; int mm = 0; do { sum11 = sum1; sum1 = sum1 + pow((0.0-1.0), (mm-1))*(h_s(mm) + h_s(mm+k))*pow(arg, (2*mm))/(pow(2.0, (2*mm+k))*dfactorial(mm)*dfactorial(mm+k)); mm = mm + 1; } while((fabs(real(sqrt(sum1*conj(sum1) - sum11*conj(sum11)))) > tolerance) || (mm < mm_max)); sum1 = pow(arg, k)*sum1/(1.0*PI); // GET TERM SUM 2 complex<double> sum2(0.0,0.0); for(int m = 0; m <= (k-1); m++) { sum2 = sum2 + dfactorial((k-m-1))*pow(arg, (2*m))/(pow(2.0, (2*m-k))*dfactorial(m)); } sum2 = pow(arg, (0-k))*sum2/(1.0*PI); // NOW GET Yn Yn = (2.0*Jn/(1.0*PI))*(log(arg/2.0) + EULERC) + sum1 - sum2; // NOW WE WILL MAKE USE OF THE BESSEL FUNCTION RELATION FOR NEGATIVE n: // Y(n-1) = (-1)^n*Yn if(n < 0) { Yn = pow((0.0-1.0), k)*Yn; } return Yn; }
//======================================================================== //======================================================================== // // NAME: double dtwofac(int l) // DESC: Calculates the double factorial (2l-1)!!=1*3*5*...*(2l-1) for // argument l, which can be defined as: // // (2l-1)!!=(2l)!/((2^n)*l!) // // NOTES: i. !!! there is a better way to define this with the gamma // function // // //======================================================================== //======================================================================== double dtwofac(int n) { return ( dfactorial(2*n)/( pow(2.0, n)*dfactorial(n) ) ); }
//======================================================================== //======================================================================== // // NAME: int ifactorial(int n) // DESC: Calculates the factorial of n and returns the value as an // integer // // INPUT: // int n == integer // // OUTPUT: // int factorial == n! // // NOTES: 1) This will only works to a certain max n (max int) // // //======================================================================== //======================================================================== int ifactorial(int n) { return static_cast<int>(dfactorial(n)); }
//================================================================================================== // Factorials n! and Double factorial n!! //================================================================================================== double dfactorial(double dn) { if(dn<=1.0) return 1.0; return dn*dfactorial(dn-1.0); }