gsl_integration__qc25c.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* integration/qc25c.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Brian Gough
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. struct fn_cauchy_params
  20. {
  21. gsl_function *function;
  22. double singularity;
  23. };
  24. static double fn_cauchy (double t, void *params);
  25. static void compute_moments (double cc, double *moment);
  26. static void
  27. qc25c (gsl_function * f, double a, double b, double c,
  28. double *result, double *abserr, int *err_reliable);
  29. static void
  30. qc25c (gsl_function * f, double a, double b, double c,
  31. double *result, double *abserr, int *err_reliable)
  32. {
  33. double cc = (2 * c - b - a) / (b - a);
  34. if (fabs (cc) > 1.1)
  35. {
  36. double resabs, resasc;
  37. gsl_function weighted_function;
  38. struct fn_cauchy_params fn_params;
  39. fn_params.function = f;
  40. fn_params.singularity = c;
  41. weighted_function.function = &fn_cauchy;
  42. weighted_function.params = &fn_params;
  43. gsl_integration_qk15 (&weighted_function, a, b, result, abserr,
  44. &resabs, &resasc);
  45. if (*abserr == resasc)
  46. {
  47. *err_reliable = 0;
  48. }
  49. else
  50. {
  51. *err_reliable = 1;
  52. }
  53. return;
  54. }
  55. else
  56. {
  57. double cheb12[13], cheb24[25], moment[25];
  58. double res12 = 0, res24 = 0;
  59. size_t i;
  60. gsl_integration_qcheb (f, a, b, cheb12, cheb24);
  61. compute_moments (cc, moment);
  62. for (i = 0; i < 13; i++)
  63. {
  64. res12 += cheb12[i] * moment[i];
  65. }
  66. for (i = 0; i < 25; i++)
  67. {
  68. res24 += cheb24[i] * moment[i];
  69. }
  70. *result = res24;
  71. *abserr = fabs(res24 - res12) ;
  72. *err_reliable = 0;
  73. return;
  74. }
  75. }
  76. static double
  77. fn_cauchy (double x, void *params)
  78. {
  79. struct fn_cauchy_params *p = (struct fn_cauchy_params *) params;
  80. gsl_function *f = p->function;
  81. double c = p->singularity;
  82. return GSL_FN_EVAL (f, x) / (x - c);
  83. }
  84. static void
  85. compute_moments (double cc, double *moment)
  86. {
  87. size_t k;
  88. double a0 = log (fabs ((1.0 - cc) / (1.0 + cc)));
  89. double a1 = 2 + a0 * cc;
  90. moment[0] = a0;
  91. moment[1] = a1;
  92. for (k = 2; k < 25; k++)
  93. {
  94. double a2;
  95. if ((k % 2) == 0)
  96. {
  97. a2 = 2.0 * cc * a1 - a0;
  98. }
  99. else
  100. {
  101. const double km1 = k - 1.0;
  102. a2 = 2.0 * cc * a1 - a0 - 4.0 / (km1 * km1 - 1.0);
  103. }
  104. moment[k] = a2;
  105. a0 = a1;
  106. a1 = a2;
  107. }
  108. }