gsl_specfunc__recurse.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* specfunc/recurse.h
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman
  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. #ifndef _RECURSE_H_
  21. #define _RECURSE_H_
  22. #define CONCAT(a,b) a ## _ ## b
  23. /* n_max >= n_min + 2
  24. * f[n+1] + a[n] f[n] + b[n] f[n-1] = 0
  25. *
  26. * Trivial forward recurrence.
  27. */
  28. #define GEN_RECURSE_FORWARD_SIMPLE(func) \
  29. int CONCAT(recurse_forward_simple, func) ( \
  30. const int n_max, const int n_min, \
  31. const double parameters[], \
  32. const double f_n_min, \
  33. const double f_n_min_p1, \
  34. double * f, \
  35. double * f_n_max \
  36. ) \
  37. { \
  38. int n; \
  39. \
  40. if(f == 0) { \
  41. double f2 = f_n_min; \
  42. double f1 = f_n_min_p1; \
  43. double f0; \
  44. for(n=n_min+2; n<=n_max; n++) { \
  45. f0 = -REC_COEFF_A(n-1,parameters) * f1 - REC_COEFF_B(n-1, parameters) * f2; \
  46. f2 = f1; \
  47. f1 = f0; \
  48. } \
  49. *f_n_max = f0; \
  50. } \
  51. else { \
  52. f[n_min] = f_n_min; \
  53. f[n_min + 1] = f_n_min_p1; \
  54. for(n=n_min+2; n<=n_max; n++) { \
  55. f[n] = -REC_COEFF_A(n-1,parameters) * f[n-1] - REC_COEFF_B(n-1, parameters) * f[n-2]; \
  56. } \
  57. *f_n_max = f[n_max]; \
  58. } \
  59. \
  60. return GSL_SUCCESS; \
  61. } \
  62. /* n_start >= n_max >= n_min
  63. * f[n+1] + a[n] f[n] + b[n] f[n-1] = 0
  64. *
  65. * Generate the minimal solution of the above recursion relation,
  66. * with the simplest form of the normalization condition, f[n_min] given.
  67. * [Gautschi, SIAM Rev. 9, 24 (1967); (3.9) with s[n]=0]
  68. */
  69. #define GEN_RECURSE_BACKWARD_MINIMAL_SIMPLE(func) \
  70. int CONCAT(recurse_backward_minimal_simple, func) ( \
  71. const int n_start, \
  72. const int n_max, const int n_min, \
  73. const double parameters[], \
  74. const double f_n_min, \
  75. double * f, \
  76. double * f_n_max \
  77. ) \
  78. { \
  79. int n; \
  80. double r_n = 0.; \
  81. double r_nm1; \
  82. double ratio; \
  83. \
  84. for(n=n_start; n > n_max; n--) { \
  85. r_nm1 = -REC_COEFF_B(n, parameters) / (REC_COEFF_A(n, parameters) + r_n); \
  86. r_n = r_nm1; \
  87. } \
  88. \
  89. if(f != 0) { \
  90. f[n_max] = 10.*DBL_MIN; \
  91. for(n=n_max; n > n_min; n--) { \
  92. r_nm1 = -REC_COEFF_B(n, parameters) / (REC_COEFF_A(n, parameters) + r_n); \
  93. f[n-1] = f[n] / r_nm1; \
  94. r_n = r_nm1; \
  95. } \
  96. ratio = f_n_min / f[n_min]; \
  97. for(n=n_min; n<=n_max; n++) { \
  98. f[n] *= ratio; \
  99. } \
  100. } \
  101. else { \
  102. double f_nm1; \
  103. double f_n = 10.*DBL_MIN; \
  104. *f_n_max = f_n; \
  105. for(n=n_max; n > n_min; n--) { \
  106. r_nm1 = -REC_COEFF_B(n, parameters) / (REC_COEFF_A(n, parameters) + r_n); \
  107. f_nm1 = f_n / r_nm1; \
  108. r_n = r_nm1; \
  109. } \
  110. ratio = f_n_min / f_nm1; \
  111. *f_n_max *= ratio; \
  112. } \
  113. \
  114. return GSL_SUCCESS; \
  115. } \
  116. #endif /* !_RECURSE_H_ */