gsl_sum__levin_utrunc.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* sum/levin_utrunc.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Gerard Jungman, 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. /* Author: G. Jungman */
  20. #include "gsl__config.h"
  21. #include "gsl_math.h"
  22. #include "gsl_test.h"
  23. #include "gsl_errno.h"
  24. #include "gsl_sum.h"
  25. int
  26. gsl_sum_levin_utrunc_accel (const double *array,
  27. const size_t array_size,
  28. gsl_sum_levin_utrunc_workspace * w,
  29. double *sum_accel, double *abserr_trunc)
  30. {
  31. return gsl_sum_levin_utrunc_minmax (array, array_size,
  32. 0, array_size - 1,
  33. w, sum_accel, abserr_trunc);
  34. }
  35. int
  36. gsl_sum_levin_utrunc_minmax (const double *array,
  37. const size_t array_size,
  38. const size_t min_terms,
  39. const size_t max_terms,
  40. gsl_sum_levin_utrunc_workspace * w,
  41. double *sum_accel, double *abserr_trunc)
  42. {
  43. if (array_size == 0)
  44. {
  45. *sum_accel = 0.0;
  46. *abserr_trunc = 0.0;
  47. w->sum_plain = 0.0;
  48. w->terms_used = 0;
  49. return GSL_SUCCESS;
  50. }
  51. else if (array_size == 1)
  52. {
  53. *sum_accel = array[0];
  54. *abserr_trunc = GSL_POSINF;
  55. w->sum_plain = array[0];
  56. w->terms_used = 1;
  57. return GSL_SUCCESS;
  58. }
  59. else
  60. {
  61. const double SMALL = 0.01;
  62. const size_t nmax = GSL_MAX (max_terms, array_size) - 1;
  63. double trunc_n = 0.0, trunc_nm1 = 0.0;
  64. double actual_trunc_n = 0.0, actual_trunc_nm1 = 0.0;
  65. double result_n = 0.0, result_nm1 = 0.0;
  66. size_t n;
  67. int better = 0;
  68. int before = 0;
  69. int converging = 0;
  70. double least_trunc = GSL_DBL_MAX;
  71. double result_least_trunc;
  72. /* Calculate specified minimum number of terms. No convergence
  73. tests are made, and no truncation information is stored. */
  74. for (n = 0; n < min_terms; n++)
  75. {
  76. const double t = array[n];
  77. result_nm1 = result_n;
  78. gsl_sum_levin_utrunc_step (t, n, w, &result_n);
  79. }
  80. /* Assume the result after the minimum calculation is the best. */
  81. result_least_trunc = result_n;
  82. /* Calculate up to maximum number of terms. Check truncation
  83. condition. */
  84. for (; n <= nmax; n++)
  85. {
  86. const double t = array[n];
  87. result_nm1 = result_n;
  88. gsl_sum_levin_utrunc_step (t, n, w, &result_n);
  89. /* Compute the truncation error directly */
  90. actual_trunc_nm1 = actual_trunc_n;
  91. actual_trunc_n = fabs (result_n - result_nm1);
  92. /* Average results to make a more reliable estimate of the
  93. real truncation error */
  94. trunc_nm1 = trunc_n;
  95. trunc_n = 0.5 * (actual_trunc_n + actual_trunc_nm1);
  96. /* Determine if we are in the convergence region. */
  97. better = (trunc_n < trunc_nm1 || trunc_n < SMALL * fabs (result_n));
  98. converging = converging || (better && before);
  99. before = better;
  100. if (converging)
  101. {
  102. if (trunc_n < least_trunc)
  103. {
  104. /* Found a low truncation point in the convergence
  105. region. Save it. */
  106. least_trunc = trunc_n;
  107. result_least_trunc = result_n;
  108. }
  109. if (fabs (trunc_n / result_n) < 10.0 * GSL_MACH_EPS)
  110. break;
  111. }
  112. }
  113. if (converging)
  114. {
  115. /* Stopped in the convergence region. Return result and
  116. error estimate. */
  117. *sum_accel = result_least_trunc;
  118. *abserr_trunc = least_trunc;
  119. w->terms_used = n;
  120. return GSL_SUCCESS;
  121. }
  122. else
  123. {
  124. /* Never reached the convergence region. Use the last
  125. calculated values. */
  126. *sum_accel = result_n;
  127. *abserr_trunc = trunc_n;
  128. w->terms_used = n;
  129. return GSL_SUCCESS;
  130. }
  131. }
  132. }
  133. int
  134. gsl_sum_levin_utrunc_step (const double term,
  135. const size_t n,
  136. gsl_sum_levin_utrunc_workspace * w, double *sum_accel)
  137. {
  138. if (term == 0.0)
  139. {
  140. /* This is actually harmless when treated in this way. A term
  141. which is exactly zero is simply ignored; the state is not
  142. changed. We return GSL_EZERODIV as an indicator that this
  143. occured. */
  144. return GSL_EZERODIV;
  145. }
  146. else if (n == 0)
  147. {
  148. *sum_accel = term;
  149. w->sum_plain = term;
  150. w->q_den[0] = 1.0 / term;
  151. w->q_num[0] = 1.0;
  152. return GSL_SUCCESS;
  153. }
  154. else
  155. {
  156. double factor = 1.0;
  157. double ratio = (double) n / (n + 1.0);
  158. int j;
  159. w->sum_plain += term;
  160. w->q_den[n] = 1.0 / (term * (n + 1.0) * (n + 1.0));
  161. w->q_num[n] = w->sum_plain * w->q_den[n];
  162. for (j = n - 1; j >= 0; j--)
  163. {
  164. double c = factor * (j + 1) / (n + 1);
  165. factor *= ratio;
  166. w->q_den[j] = w->q_den[j + 1] - c * w->q_den[j];
  167. w->q_num[j] = w->q_num[j + 1] - c * w->q_num[j];
  168. }
  169. *sum_accel = w->q_num[0] / w->q_den[0];
  170. return GSL_SUCCESS;
  171. }
  172. }