gsl_sum__levin_u.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* sum/levin_u.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. #include "gsl__config.h"
  20. #include "gsl_math.h"
  21. #include "gsl_test.h"
  22. #include "gsl_errno.h"
  23. #include "gsl_sum.h"
  24. int
  25. gsl_sum_levin_u_accel (const double *array, const size_t array_size,
  26. gsl_sum_levin_u_workspace * w,
  27. double *sum_accel, double *abserr)
  28. {
  29. return gsl_sum_levin_u_minmax (array, array_size,
  30. 0, array_size - 1, w, sum_accel, abserr);
  31. }
  32. int
  33. gsl_sum_levin_u_minmax (const double *array, const size_t array_size,
  34. const size_t min_terms, const size_t max_terms,
  35. gsl_sum_levin_u_workspace * w,
  36. double *sum_accel, double *abserr)
  37. {
  38. if (array_size == 0)
  39. {
  40. *sum_accel = 0.0;
  41. *abserr = 0.0;
  42. w->sum_plain = 0.0;
  43. w->terms_used = 0;
  44. return GSL_SUCCESS;
  45. }
  46. else if (array_size == 1)
  47. {
  48. *sum_accel = array[0];
  49. *abserr = 0.0;
  50. w->sum_plain = array[0];
  51. w->terms_used = 1;
  52. return GSL_SUCCESS;
  53. }
  54. else
  55. {
  56. const double SMALL = 0.01;
  57. const size_t nmax = GSL_MAX (max_terms, array_size) - 1;
  58. double noise_n = 0.0, noise_nm1 = 0.0;
  59. double trunc_n = 0.0, trunc_nm1 = 0.0;
  60. double actual_trunc_n = 0.0, actual_trunc_nm1 = 0.0;
  61. double result_n = 0.0, result_nm1 = 0.0;
  62. double variance = 0;
  63. size_t n;
  64. unsigned int i;
  65. int better = 0;
  66. int before = 0;
  67. int converging = 0;
  68. double least_trunc = GSL_DBL_MAX;
  69. double least_trunc_noise = GSL_DBL_MAX;
  70. double least_trunc_result;
  71. /* Calculate specified minimum number of terms. No convergence
  72. tests are made, and no truncation information is stored. */
  73. for (n = 0; n < min_terms; n++)
  74. {
  75. const double t = array[n];
  76. result_nm1 = result_n;
  77. gsl_sum_levin_u_step (t, n, nmax, w, &result_n);
  78. }
  79. least_trunc_result = result_n;
  80. variance = 0;
  81. for (i = 0; i < n; i++)
  82. {
  83. double dn = w->dsum[i] * GSL_MACH_EPS * array[i];
  84. variance += dn * dn;
  85. }
  86. noise_n = sqrt (variance);
  87. /* Calculate up to maximum number of terms. Check truncation
  88. condition. */
  89. for (; n <= nmax; n++)
  90. {
  91. const double t = array[n];
  92. result_nm1 = result_n;
  93. gsl_sum_levin_u_step (t, n, nmax, w, &result_n);
  94. /* Compute the truncation error directly */
  95. actual_trunc_nm1 = actual_trunc_n;
  96. actual_trunc_n = fabs (result_n - result_nm1);
  97. /* Average results to make a more reliable estimate of the
  98. real truncation error */
  99. trunc_nm1 = trunc_n;
  100. trunc_n = 0.5 * (actual_trunc_n + actual_trunc_nm1);
  101. noise_nm1 = noise_n;
  102. variance = 0;
  103. for (i = 0; i <= n; i++)
  104. {
  105. double dn = w->dsum[i] * GSL_MACH_EPS * array[i];
  106. variance += dn * dn;
  107. }
  108. noise_n = sqrt (variance);
  109. /* Determine if we are in the convergence region. */
  110. better = (trunc_n < trunc_nm1 || trunc_n < SMALL * fabs (result_n));
  111. converging = converging || (better && before);
  112. before = better;
  113. if (converging)
  114. {
  115. if (trunc_n < least_trunc)
  116. {
  117. /* Found a low truncation point in the convergence
  118. region. Save it. */
  119. least_trunc_result = result_n;
  120. least_trunc = trunc_n;
  121. least_trunc_noise = noise_n;
  122. }
  123. if (noise_n > trunc_n / 3.0)
  124. break;
  125. if (trunc_n < 10.0 * GSL_MACH_EPS * fabs (result_n))
  126. break;
  127. }
  128. }
  129. if (converging)
  130. {
  131. /* Stopped in the convergence region. Return result and
  132. error estimate. */
  133. *sum_accel = least_trunc_result;
  134. *abserr = GSL_MAX_DBL (least_trunc, least_trunc_noise);
  135. w->terms_used = n;
  136. return GSL_SUCCESS;
  137. }
  138. else
  139. {
  140. /* Never reached the convergence region. Use the last
  141. calculated values. */
  142. *sum_accel = result_n;
  143. *abserr = GSL_MAX_DBL (trunc_n, noise_n);
  144. w->terms_used = n;
  145. return GSL_SUCCESS;
  146. }
  147. }
  148. }
  149. int
  150. gsl_sum_levin_u_step (const double term, const size_t n, const size_t nmax,
  151. gsl_sum_levin_u_workspace * w, double *sum_accel)
  152. {
  153. #define I(i,j) ((i)*(nmax+1) + (j))
  154. if (n == 0)
  155. {
  156. *sum_accel = term;
  157. w->sum_plain = term;
  158. w->q_den[0] = 1.0 / term;
  159. w->q_num[0] = 1.0;
  160. w->dq_den[I (0, 0)] = -1.0 / (term * term);
  161. w->dq_num[I (0, 0)] = 0.0;
  162. w->dsum[0] = 1.0;
  163. return GSL_SUCCESS;
  164. }
  165. else
  166. {
  167. double result;
  168. double factor = 1.0;
  169. double ratio = (double) n / (n + 1.0);
  170. unsigned int i;
  171. int j;
  172. w->sum_plain += term;
  173. w->q_den[n] = 1.0 / (term * (n + 1.0) * (n + 1.0));
  174. w->q_num[n] = w->sum_plain * w->q_den[n];
  175. for (i = 0; i < n; i++)
  176. {
  177. w->dq_den[I (i, n)] = 0;
  178. w->dq_num[I (i, n)] = w->q_den[n];
  179. }
  180. w->dq_den[I (n, n)] = -w->q_den[n] / term;
  181. w->dq_num[I (n, n)] =
  182. w->q_den[n] + w->sum_plain * (w->dq_den[I (n, n)]);
  183. for (j = n - 1; j >= 0; j--)
  184. {
  185. double c = factor * (j + 1) / (n + 1);
  186. factor *= ratio;
  187. w->q_den[j] = w->q_den[j + 1] - c * w->q_den[j];
  188. w->q_num[j] = w->q_num[j + 1] - c * w->q_num[j];
  189. for (i = 0; i < n; i++)
  190. {
  191. w->dq_den[I (i, j)] =
  192. w->dq_den[I (i, j + 1)] - c * w->dq_den[I (i, j)];
  193. w->dq_num[I (i, j)] =
  194. w->dq_num[I (i, j + 1)] - c * w->dq_num[I (i, j)];
  195. }
  196. w->dq_den[I (n, j)] = w->dq_den[I (n, j + 1)];
  197. w->dq_num[I (n, j)] = w->dq_num[I (n, j + 1)];
  198. }
  199. result = w->q_num[0] / w->q_den[0];
  200. *sum_accel = result;
  201. for (i = 0; i <= n; i++)
  202. {
  203. w->dsum[i] =
  204. (w->dq_num[I (i, 0)] -
  205. result * w->dq_den[I (i, 0)]) / w->q_den[0];
  206. }
  207. return GSL_SUCCESS;
  208. }
  209. }