You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ABACUS_Spec_Fns.h 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**********************************************************
  2. This software is part of J.-S. Caux's ABACUS library.
  3. Copyright (c) J.-S. Caux.
  4. -----------------------------------------------------------
  5. File: ABACUS_Spec_Fns.h
  6. Purpose: Defines special math functions.
  7. ***********************************************************/
  8. #ifndef ABACUS_SPEC_FNS_H
  9. #define ABACUS_SPEC_FNS_H
  10. #include "ABACUS.h"
  11. namespace ABACUS {
  12. inline DP Cosine_Integral (DP x)
  13. {
  14. // Returns the Cosine integral -\int_x^\infty dt \frac{\cos t}{t}
  15. // Refer to GR[6] 8.23
  16. if (x <= 0.0) {
  17. std::cout << "Cosine_Integral called with real argument " << x << " <= 0, which is ill-defined because of the branch cut." << std::endl;
  18. ABACUSerror("");
  19. }
  20. else if (x < 15.0) { // Use power series expansion
  21. // Ci (x) = gamma + \ln x + \sum_{n=1}^\infty (-1)^n x^{2n}/(2n (2n)!).
  22. int n = 1;
  23. DP minonetothen = -1.0;
  24. DP logxtothetwon = 2.0 * log(x);
  25. DP twologx = 2.0 * log(x);
  26. DP logtwonfact = log(2.0);
  27. DP series = minonetothen * exp(logxtothetwon - log(2.0 * n) - logtwonfact);
  28. DP term_n;
  29. do {
  30. n += 1;
  31. minonetothen *= -1.0;
  32. logxtothetwon += twologx;
  33. logtwonfact += log((2.0 * n - 1.0) * 2.0 * n);
  34. term_n = minonetothen * exp(logxtothetwon - log(2.0 * n) - logtwonfact);
  35. series += term_n;
  36. } while (fabs(term_n) > 1.0e-16);
  37. return(Euler_Mascheroni + log(x) + series);
  38. }
  39. else { // Use high x power series
  40. // Ci (x) = \frac{\sin x}{x} \sum_{n=0}^\infty (-1)^n (2n)! x^{-2n} - \frac{\cos x}{x} \sum_{n=0}^\infty (-1)^n (2n+1)! x^{-2n-1}
  41. int n = 0;
  42. DP minonetothen = 1.0;
  43. DP logxtothetwon = 0.0;
  44. DP logxtothetwonplus1 = log(x);
  45. DP twologx = 2.0 * log(x);
  46. DP logtwonfact = 0.0;
  47. DP logtwonplus1fact = 0.0;
  48. DP series1 = minonetothen * exp(logtwonfact - logxtothetwon);
  49. DP series2 = minonetothen * exp(logtwonplus1fact - logxtothetwonplus1);
  50. do {
  51. n += 1;
  52. minonetothen *= -1.0;
  53. logxtothetwon += twologx;
  54. logxtothetwonplus1 += twologx;
  55. logtwonfact += log((2.0 * n - 1.0) * 2.0 * n);
  56. logtwonplus1fact += log(2.0 * n * (2.0 * n + 1));
  57. series1 += minonetothen * exp(logtwonfact - logxtothetwon);
  58. series2 += minonetothen * exp(logtwonplus1fact - logxtothetwonplus1);
  59. } while (n < 12);
  60. return((sin(x)/x) * series1 - (cos(x)/x) * series2);
  61. }
  62. return(log(-1.0));
  63. }
  64. /*********** Jacobi Theta functions *********/
  65. inline DP Jacobi_Theta_1_q (DP u, DP q) {
  66. // Uses the summation formula.
  67. // theta_1 (x) = 2 \sum_{n=1}^\infty (-1)^{n+1} q^{(n-1/2)^2} \sin (2n-1)u
  68. // in which q is the nome. (GR 8.180.1)
  69. // We always evaluate to numerical accuracy.
  70. if (q >= 1.0) ABACUSerror("Jacobi_Theta_1_q function called with q > 1.");
  71. DP answer = 0.0;
  72. DP contrib = 0.0;
  73. DP qtonminhalfsq = pow(q, 0.25); // this will be q^{(n-1/2)^2}
  74. DP qtotwon = pow(q, 2.0); // this will be q^{2n}
  75. DP qsq = q*q;
  76. int n = 1;
  77. do {
  78. contrib = (n % 2 ? 2.0 : -2.0) * qtonminhalfsq * sin((2.0*n - 1.0)*u);
  79. answer += contrib;
  80. qtonminhalfsq *= qtotwon;
  81. qtotwon *= qsq;
  82. n++;
  83. } while (fabs(contrib/answer) > MACHINE_EPS);
  84. return(answer);
  85. }
  86. inline std::complex<DP> ln_Jacobi_Theta_1_q (std::complex<DP> u, std::complex<DP> q) {
  87. // This uses the product representation
  88. // \theta_1 (x) = 2 q^{1/4} \sin{u} \prod_{n=1}^\infty (1 - 2 q^{2n} \cos 2u + q^{4n}) (1 - q^{2n})
  89. // (GR 8.181.2)
  90. std::complex<DP> contrib = 0.0;
  91. std::complex<DP> qtotwon = q*q; // this will be q^{2n}
  92. std::complex<DP> qsq = q*q;
  93. std::complex<DP> twocos2u = 2.0 * cos(2.0*u);
  94. int n = 1;
  95. std::complex<DP> answer = log(2.0 * sin(u)) + 0.25 * log(q);
  96. do {
  97. contrib = log((1.0 - twocos2u * qtotwon + qtotwon * qtotwon) * (1.0 - qtotwon));
  98. answer += contrib;
  99. qtotwon *= qsq;
  100. n++;
  101. } while (abs(contrib) > 1.0e-12);
  102. return(answer);
  103. }
  104. /************ Barnes function ************/
  105. inline DP ln_Gamma_for_Barnes_G_RE (Vect_DP args)
  106. {
  107. return(real(ln_Gamma(std::complex<double>(args[0]))));
  108. }
  109. inline DP ln_Barnes_G_RE (DP z)
  110. {
  111. // Implementation according to equation (28) of 2004_Adamchik_CPC_157
  112. // Restricted to real arguments.
  113. Vect_DP args (0.0, 2);
  114. DP req_rel_prec = 1.0e-6;
  115. DP req_abs_prec = 1.0e-6;
  116. int max_nr_pts = 10000;
  117. Integral_result integ_ln_Gamma = Integrate_optimal (ln_Gamma_for_Barnes_G_RE, args, 0, 0.0, z - 1.0, req_rel_prec, req_abs_prec, max_nr_pts);
  118. return(0.5 * (z - 1.0) * (2.0 - z + logtwoPI) + (z - 1.0) * real(ln_Gamma(std::complex<double>(z - 1.0))) - integ_ln_Gamma.integ_est);
  119. }
  120. } // namespace ABACUS
  121. #endif