gsl_integration__qc25s.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /* integration/qc25s.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_qaws_params
  20. {
  21. gsl_function *function;
  22. double a;
  23. double b;
  24. gsl_integration_qaws_table *table;
  25. };
  26. static double fn_qaws (double t, void *params);
  27. static double fn_qaws_L (double x, void *params);
  28. static double fn_qaws_R (double x, void *params);
  29. static void
  30. compute_result (const double * r, const double * cheb12, const double * cheb24,
  31. double * result12, double * result24);
  32. static void
  33. qc25s (gsl_function * f, double a, double b, double a1, double b1,
  34. gsl_integration_qaws_table * t,
  35. double *result, double *abserr, int *err_reliable);
  36. static void
  37. qc25s (gsl_function * f, double a, double b, double a1, double b1,
  38. gsl_integration_qaws_table * t,
  39. double *result, double *abserr, int *err_reliable)
  40. {
  41. gsl_function weighted_function;
  42. struct fn_qaws_params fn_params;
  43. fn_params.function = f;
  44. fn_params.a = a;
  45. fn_params.b = b;
  46. fn_params.table = t;
  47. weighted_function.params = &fn_params;
  48. if (a1 == a && (t->alpha != 0.0 || t->mu != 0))
  49. {
  50. double cheb12[13], cheb24[25];
  51. double factor = pow(0.5 * (b1 - a1), t->alpha + 1.0);
  52. weighted_function.function = &fn_qaws_R;
  53. gsl_integration_qcheb (&weighted_function, a1, b1, cheb12, cheb24);
  54. if (t->mu == 0)
  55. {
  56. double res12 = 0, res24 = 0;
  57. double u = factor;
  58. compute_result (t->ri, cheb12, cheb24, &res12, &res24);
  59. *result = u * res24;
  60. *abserr = fabs(u * (res24 - res12));
  61. }
  62. else
  63. {
  64. double res12a = 0, res24a = 0;
  65. double res12b = 0, res24b = 0;
  66. double u = factor * log(b1 - a1);
  67. double v = factor;
  68. compute_result (t->ri, cheb12, cheb24, &res12a, &res24a);
  69. compute_result (t->rg, cheb12, cheb24, &res12b, &res24b);
  70. *result = u * res24a + v * res24b;
  71. *abserr = fabs(u * (res24a - res12a)) + fabs(v * (res24b - res12b));
  72. }
  73. *err_reliable = 0;
  74. return;
  75. }
  76. else if (b1 == b && (t->beta != 0.0 || t->nu != 0))
  77. {
  78. double cheb12[13], cheb24[25];
  79. double factor = pow(0.5 * (b1 - a1), t->beta + 1.0);
  80. weighted_function.function = &fn_qaws_L;
  81. gsl_integration_qcheb (&weighted_function, a1, b1, cheb12, cheb24);
  82. if (t->nu == 0)
  83. {
  84. double res12 = 0, res24 = 0;
  85. double u = factor;
  86. compute_result (t->rj, cheb12, cheb24, &res12, &res24);
  87. *result = u * res24;
  88. *abserr = fabs(u * (res24 - res12));
  89. }
  90. else
  91. {
  92. double res12a = 0, res24a = 0;
  93. double res12b = 0, res24b = 0;
  94. double u = factor * log(b1 - a1);
  95. double v = factor;
  96. compute_result (t->rj, cheb12, cheb24, &res12a, &res24a);
  97. compute_result (t->rh, cheb12, cheb24, &res12b, &res24b);
  98. *result = u * res24a + v * res24b;
  99. *abserr = fabs(u * (res24a - res12a)) + fabs(v * (res24b - res12b));
  100. }
  101. *err_reliable = 0;
  102. return;
  103. }
  104. else
  105. {
  106. double resabs, resasc;
  107. weighted_function.function = &fn_qaws;
  108. gsl_integration_qk15 (&weighted_function, a1, b1, result, abserr,
  109. &resabs, &resasc);
  110. if (*abserr == resasc)
  111. {
  112. *err_reliable = 0;
  113. }
  114. else
  115. {
  116. *err_reliable = 1;
  117. }
  118. return;
  119. }
  120. }
  121. static double
  122. fn_qaws (double x, void *params)
  123. {
  124. struct fn_qaws_params *p = (struct fn_qaws_params *) params;
  125. gsl_function *f = p->function;
  126. gsl_integration_qaws_table *t = p->table;
  127. double factor = 1.0;
  128. if (t->alpha != 0.0)
  129. factor *= pow(x - p->a, t->alpha);
  130. if (t->beta != 0.0)
  131. factor *= pow(p->b - x, t->beta);
  132. if (t->mu == 1)
  133. factor *= log(x - p->a);
  134. if (t->nu == 1)
  135. factor *= log(p->b - x);
  136. return factor * GSL_FN_EVAL (f, x);
  137. }
  138. static double
  139. fn_qaws_L (double x, void *params)
  140. {
  141. struct fn_qaws_params *p = (struct fn_qaws_params *) params;
  142. gsl_function *f = p->function;
  143. gsl_integration_qaws_table *t = p->table;
  144. double factor = 1.0;
  145. if (t->alpha != 0.0)
  146. factor *= pow(x - p->a, t->alpha);
  147. if (t->mu == 1)
  148. factor *= log(x - p->a);
  149. return factor * GSL_FN_EVAL (f, x);
  150. }
  151. static double
  152. fn_qaws_R (double x, void *params)
  153. {
  154. struct fn_qaws_params *p = (struct fn_qaws_params *) params;
  155. gsl_function *f = p->function;
  156. gsl_integration_qaws_table *t = p->table;
  157. double factor = 1.0;
  158. if (t->beta != 0.0)
  159. factor *= pow(p->b - x, t->beta);
  160. if (t->nu == 1)
  161. factor *= log(p->b - x);
  162. return factor * GSL_FN_EVAL (f, x);
  163. }
  164. static void
  165. compute_result (const double * r, const double * cheb12, const double * cheb24,
  166. double * result12, double * result24)
  167. {
  168. size_t i;
  169. double res12 = 0;
  170. double res24 = 0;
  171. for (i = 0; i < 13; i++)
  172. {
  173. res12 += r[i] * cheb12[i];
  174. }
  175. for (i = 0; i < 25; i++)
  176. {
  177. res24 += r[i] * cheb24[i];
  178. }
  179. *result12 = res12;
  180. *result24 = res24;
  181. }