gsl_integration__qelg.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* integration/qelg.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 extrapolation_table
  20. {
  21. size_t n;
  22. double rlist2[52];
  23. size_t nres;
  24. double res3la[3];
  25. };
  26. static void
  27. initialise_table (struct extrapolation_table *table);
  28. static void
  29. append_table (struct extrapolation_table *table, double y);
  30. static void
  31. initialise_table (struct extrapolation_table *table)
  32. {
  33. table->n = 0;
  34. table->nres = 0;
  35. }
  36. #ifdef JUNK
  37. static void
  38. initialise_table (struct extrapolation_table *table, double y)
  39. {
  40. table->n = 0;
  41. table->rlist2[0] = y;
  42. table->nres = 0;
  43. }
  44. #endif
  45. static void
  46. append_table (struct extrapolation_table *table, double y)
  47. {
  48. size_t n;
  49. n = table->n;
  50. table->rlist2[n] = y;
  51. table->n++;
  52. }
  53. /* static inline void
  54. qelg (size_t * n, double epstab[],
  55. double * result, double * abserr,
  56. double res3la[], size_t * nres); */
  57. static inline void
  58. qelg (struct extrapolation_table *table, double *result, double *abserr);
  59. static inline void
  60. qelg (struct extrapolation_table *table, double *result, double *abserr)
  61. {
  62. double *epstab = table->rlist2;
  63. double *res3la = table->res3la;
  64. const size_t n = table->n - 1;
  65. const double current = epstab[n];
  66. double absolute = GSL_DBL_MAX;
  67. double relative = 5 * GSL_DBL_EPSILON * fabs (current);
  68. const size_t newelm = n / 2;
  69. const size_t n_orig = n;
  70. size_t n_final = n;
  71. size_t i;
  72. const size_t nres_orig = table->nres;
  73. *result = current;
  74. *abserr = GSL_DBL_MAX;
  75. if (n < 2)
  76. {
  77. *result = current;
  78. *abserr = GSL_MAX_DBL (absolute, relative);
  79. return;
  80. }
  81. epstab[n + 2] = epstab[n];
  82. epstab[n] = GSL_DBL_MAX;
  83. for (i = 0; i < newelm; i++)
  84. {
  85. double res = epstab[n - 2 * i + 2];
  86. double e0 = epstab[n - 2 * i - 2];
  87. double e1 = epstab[n - 2 * i - 1];
  88. double e2 = res;
  89. double e1abs = fabs (e1);
  90. double delta2 = e2 - e1;
  91. double err2 = fabs (delta2);
  92. double tol2 = GSL_MAX_DBL (fabs (e2), e1abs) * GSL_DBL_EPSILON;
  93. double delta3 = e1 - e0;
  94. double err3 = fabs (delta3);
  95. double tol3 = GSL_MAX_DBL (e1abs, fabs (e0)) * GSL_DBL_EPSILON;
  96. double e3, delta1, err1, tol1, ss;
  97. if (err2 <= tol2 && err3 <= tol3)
  98. {
  99. /* If e0, e1 and e2 are equal to within machine accuracy,
  100. convergence is assumed. */
  101. *result = res;
  102. absolute = err2 + err3;
  103. relative = 5 * GSL_DBL_EPSILON * fabs (res);
  104. *abserr = GSL_MAX_DBL (absolute, relative);
  105. return;
  106. }
  107. e3 = epstab[n - 2 * i];
  108. epstab[n - 2 * i] = e1;
  109. delta1 = e1 - e3;
  110. err1 = fabs (delta1);
  111. tol1 = GSL_MAX_DBL (e1abs, fabs (e3)) * GSL_DBL_EPSILON;
  112. /* If two elements are very close to each other, omit a part of
  113. the table by adjusting the value of n */
  114. if (err1 <= tol1 || err2 <= tol2 || err3 <= tol3)
  115. {
  116. n_final = 2 * i;
  117. break;
  118. }
  119. ss = (1 / delta1 + 1 / delta2) - 1 / delta3;
  120. /* Test to detect irregular behaviour in the table, and
  121. eventually omit a part of the table by adjusting the value of
  122. n. */
  123. if (fabs (ss * e1) <= 0.0001)
  124. {
  125. n_final = 2 * i;
  126. break;
  127. }
  128. /* Compute a new element and eventually adjust the value of
  129. result. */
  130. res = e1 + 1 / ss;
  131. epstab[n - 2 * i] = res;
  132. {
  133. const double error = err2 + fabs (res - e2) + err3;
  134. if (error <= *abserr)
  135. {
  136. *abserr = error;
  137. *result = res;
  138. }
  139. }
  140. }
  141. /* Shift the table */
  142. {
  143. const size_t limexp = 50 - 1;
  144. if (n_final == limexp)
  145. {
  146. n_final = 2 * (limexp / 2);
  147. }
  148. }
  149. if (n_orig % 2 == 1)
  150. {
  151. for (i = 0; i <= newelm; i++)
  152. {
  153. epstab[1 + i * 2] = epstab[i * 2 + 3];
  154. }
  155. }
  156. else
  157. {
  158. for (i = 0; i <= newelm; i++)
  159. {
  160. epstab[i * 2] = epstab[i * 2 + 2];
  161. }
  162. }
  163. if (n_orig != n_final)
  164. {
  165. for (i = 0; i <= n_final; i++)
  166. {
  167. epstab[i] = epstab[n_orig - n_final + i];
  168. }
  169. }
  170. table->n = n_final + 1;
  171. if (nres_orig < 3)
  172. {
  173. res3la[nres_orig] = *result;
  174. *abserr = GSL_DBL_MAX;
  175. }
  176. else
  177. { /* Compute error estimate */
  178. *abserr = (fabs (*result - res3la[2]) + fabs (*result - res3la[1])
  179. + fabs (*result - res3la[0]));
  180. res3la[0] = res3la[1];
  181. res3la[1] = res3la[2];
  182. res3la[2] = *result;
  183. }
  184. /* In QUADPACK the variable table->nres is incremented at the top of
  185. qelg, so it increases on every call. This leads to the array
  186. res3la being accessed when its elements are still undefined, so I
  187. have moved the update to this point so that its value more
  188. useful. */
  189. table->nres = nres_orig + 1;
  190. *abserr = GSL_MAX_DBL (*abserr, 5 * GSL_DBL_EPSILON * fabs (*result));
  191. return;
  192. }