gsl_integration__qaws.c 5.7 KB

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