gsl_integration__qpsrt.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* integration/qpsrt.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. static inline void
  20. qpsrt (gsl_integration_workspace * workspace);
  21. static inline
  22. void qpsrt (gsl_integration_workspace * workspace)
  23. {
  24. const size_t last = workspace->size - 1;
  25. const size_t limit = workspace->limit;
  26. double * elist = workspace->elist;
  27. size_t * order = workspace->order;
  28. double errmax ;
  29. double errmin ;
  30. int i, k, top;
  31. size_t i_nrmax = workspace->nrmax;
  32. size_t i_maxerr = order[i_nrmax] ;
  33. /* Check whether the list contains more than two error estimates */
  34. if (last < 2)
  35. {
  36. order[0] = 0 ;
  37. order[1] = 1 ;
  38. workspace->i = i_maxerr ;
  39. return ;
  40. }
  41. errmax = elist[i_maxerr] ;
  42. /* This part of the routine is only executed if, due to a difficult
  43. integrand, subdivision increased the error estimate. In the normal
  44. case the insert procedure should start after the nrmax-th largest
  45. error estimate. */
  46. while (i_nrmax > 0 && errmax > elist[order[i_nrmax - 1]])
  47. {
  48. order[i_nrmax] = order[i_nrmax - 1] ;
  49. i_nrmax-- ;
  50. }
  51. /* Compute the number of elements in the list to be maintained in
  52. descending order. This number depends on the number of
  53. subdivisions still allowed. */
  54. if(last < (limit/2 + 2))
  55. {
  56. top = last ;
  57. }
  58. else
  59. {
  60. top = limit - last + 1;
  61. }
  62. /* Insert errmax by traversing the list top-down, starting
  63. comparison from the element elist(order(i_nrmax+1)). */
  64. i = i_nrmax + 1 ;
  65. /* The order of the tests in the following line is important to
  66. prevent a segmentation fault */
  67. while (i < top && errmax < elist[order[i]])
  68. {
  69. order[i-1] = order[i] ;
  70. i++ ;
  71. }
  72. order[i-1] = i_maxerr ;
  73. /* Insert errmin by traversing the list bottom-up */
  74. errmin = elist[last] ;
  75. k = top - 1 ;
  76. while (k > i - 2 && errmin >= elist[order[k]])
  77. {
  78. order[k+1] = order[k] ;
  79. k-- ;
  80. }
  81. order[k+1] = last ;
  82. /* Set i_max and e_max */
  83. i_maxerr = order[i_nrmax] ;
  84. workspace->i = i_maxerr ;
  85. workspace->nrmax = i_nrmax ;
  86. }