gsl_integration__qawc.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* integration/qawc.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. #include "gsl__config.h"
  20. #include <math.h>
  21. #include <float.h>
  22. #include "gsl_math.h"
  23. #include "gsl_errno.h"
  24. #include "gsl_integration.h"
  25. #include "gsl_integration__initialise.c"
  26. #include "gsl_integration__set_initial.c"
  27. #include "gsl_integration__qpsrt.c"
  28. #include "gsl_integration__util.c"
  29. #include "gsl_integration__qc25c.c"
  30. int
  31. gsl_integration_qawc (gsl_function * f,
  32. const double a, const double b, const double c,
  33. const double epsabs, const double epsrel,
  34. const size_t limit,
  35. gsl_integration_workspace * workspace,
  36. double *result, double *abserr)
  37. {
  38. double area, errsum;
  39. double result0, abserr0;
  40. double tolerance;
  41. size_t iteration = 0;
  42. int roundoff_type1 = 0, roundoff_type2 = 0, error_type = 0;
  43. int err_reliable;
  44. int sign = 1;
  45. double lower, higher;
  46. /* Initialize results */
  47. *result = 0;
  48. *abserr = 0;
  49. if (limit > workspace->limit)
  50. {
  51. GSL_ERROR ("iteration limit exceeds available workspace", GSL_EINVAL) ;
  52. }
  53. if (b < a)
  54. {
  55. lower = b ;
  56. higher = a ;
  57. sign = -1 ;
  58. }
  59. else
  60. {
  61. lower = a;
  62. higher = b;
  63. }
  64. initialise (workspace, lower, higher);
  65. if (epsabs <= 0 && (epsrel < 50 * GSL_DBL_EPSILON || epsrel < 0.5e-28))
  66. {
  67. GSL_ERROR ("tolerance cannot be acheived with given epsabs and epsrel",
  68. GSL_EBADTOL);
  69. }
  70. if (c == a || c == b)
  71. {
  72. GSL_ERROR ("cannot integrate with singularity on endpoint", GSL_EINVAL);
  73. }
  74. /* perform the first integration */
  75. qc25c (f, lower, higher, c, &result0, &abserr0, &err_reliable);
  76. set_initial_result (workspace, result0, abserr0);
  77. /* Test on accuracy, use 0.01 relative error as an extra safety
  78. margin on the first iteration (ignored for subsequent iterations) */
  79. tolerance = GSL_MAX_DBL (epsabs, epsrel * fabs (result0));
  80. if (abserr0 < tolerance && abserr0 < 0.01 * fabs(result0))
  81. {
  82. *result = sign * result0;
  83. *abserr = abserr0;
  84. return GSL_SUCCESS;
  85. }
  86. else if (limit == 1)
  87. {
  88. *result = sign * result0;
  89. *abserr = abserr0;
  90. GSL_ERROR ("a maximum of one iteration was insufficient", GSL_EMAXITER);
  91. }
  92. area = result0;
  93. errsum = abserr0;
  94. iteration = 1;
  95. do
  96. {
  97. double a1, b1, a2, b2;
  98. double a_i, b_i, r_i, e_i;
  99. double area1 = 0, area2 = 0, area12 = 0;
  100. double error1 = 0, error2 = 0, error12 = 0;
  101. int err_reliable1, err_reliable2;
  102. /* Bisect the subinterval with the largest error estimate */
  103. retrieve (workspace, &a_i, &b_i, &r_i, &e_i);
  104. a1 = a_i;
  105. b1 = 0.5 * (a_i + b_i);
  106. a2 = b1;
  107. b2 = b_i;
  108. if (c > a1 && c <= b1)
  109. {
  110. b1 = 0.5 * (c + b2) ;
  111. a2 = b1;
  112. }
  113. else if (c > b1 && c < b2)
  114. {
  115. b1 = 0.5 * (a1 + c) ;
  116. a2 = b1;
  117. }
  118. qc25c (f, a1, b1, c, &area1, &error1, &err_reliable1);
  119. qc25c (f, a2, b2, c, &area2, &error2, &err_reliable2);
  120. area12 = area1 + area2;
  121. error12 = error1 + error2;
  122. errsum += (error12 - e_i);
  123. area += area12 - r_i;
  124. if (err_reliable1 && err_reliable2)
  125. {
  126. double delta = r_i - area12;
  127. if (fabs (delta) <= 1.0e-5 * fabs (area12) && error12 >= 0.99 * e_i)
  128. {
  129. roundoff_type1++;
  130. }
  131. if (iteration >= 10 && error12 > e_i)
  132. {
  133. roundoff_type2++;
  134. }
  135. }
  136. tolerance = GSL_MAX_DBL (epsabs, epsrel * fabs (area));
  137. if (errsum > tolerance)
  138. {
  139. if (roundoff_type1 >= 6 || roundoff_type2 >= 20)
  140. {
  141. error_type = 2; /* round off error */
  142. }
  143. /* set error flag in the case of bad integrand behaviour at
  144. a point of the integration range */
  145. if (subinterval_too_small (a1, a2, b2))
  146. {
  147. error_type = 3;
  148. }
  149. }
  150. update (workspace, a1, b1, area1, error1, a2, b2, area2, error2);
  151. retrieve (workspace, &a_i, &b_i, &r_i, &e_i);
  152. iteration++;
  153. }
  154. while (iteration < limit && !error_type && errsum > tolerance);
  155. *result = sign * sum_results (workspace);
  156. *abserr = errsum;
  157. if (errsum <= tolerance)
  158. {
  159. return GSL_SUCCESS;
  160. }
  161. else if (error_type == 2)
  162. {
  163. GSL_ERROR ("roundoff error prevents tolerance from being achieved",
  164. GSL_EROUND);
  165. }
  166. else if (error_type == 3)
  167. {
  168. GSL_ERROR ("bad integrand behavior found in the integration interval",
  169. GSL_ESING);
  170. }
  171. else if (iteration == limit)
  172. {
  173. GSL_ERROR ("maximum number of subdivisions reached", GSL_EMAXITER);
  174. }
  175. else
  176. {
  177. GSL_ERROR ("could not integrate function", GSL_EFAILED);
  178. }
  179. }