bessel_r10.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* Implementation of the BESSEL_JN and BESSEL_YN transformational
  2. function using a recurrence algorithm.
  3. Copyright (C) 2010-2015 Free Software Foundation, Inc.
  4. Contributed by Tobias Burnus <burnus@net-b.de>
  5. This file is part of the GNU Fortran runtime library (libgfortran).
  6. Libgfortran is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public
  8. License as published by the Free Software Foundation; either
  9. version 3 of the License, or (at your option) any later version.
  10. Libgfortran is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. Under Section 7 of GPL version 3, you are granted additional
  15. permissions described in the GCC Runtime Library Exception, version
  16. 3.1, as published by the Free Software Foundation.
  17. You should have received a copy of the GNU General Public License and
  18. a copy of the GCC Runtime Library Exception along with this program;
  19. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  20. <http://www.gnu.org/licenses/>. */
  21. #include "libgfortran.h"
  22. #include <stdlib.h>
  23. #include <assert.h>
  24. #define MATHFUNC(funcname) funcname ## l
  25. #if defined (HAVE_GFC_REAL_10)
  26. #if defined (HAVE_JNL)
  27. extern void bessel_jn_r10 (gfc_array_r10 * const restrict ret, int n1,
  28. int n2, GFC_REAL_10 x);
  29. export_proto(bessel_jn_r10);
  30. void
  31. bessel_jn_r10 (gfc_array_r10 * const restrict ret, int n1, int n2, GFC_REAL_10 x)
  32. {
  33. int i;
  34. index_type stride;
  35. GFC_REAL_10 last1, last2, x2rev;
  36. stride = GFC_DESCRIPTOR_STRIDE(ret,0);
  37. if (ret->base_addr == NULL)
  38. {
  39. size_t size = n2 < n1 ? 0 : n2-n1+1;
  40. GFC_DIMENSION_SET(ret->dim[0], 0, size-1, 1);
  41. ret->base_addr = xmallocarray (size, sizeof (GFC_REAL_10));
  42. ret->offset = 0;
  43. }
  44. if (unlikely (n2 < n1))
  45. return;
  46. if (unlikely (compile_options.bounds_check)
  47. && GFC_DESCRIPTOR_EXTENT(ret,0) != (n2-n1+1))
  48. runtime_error("Incorrect extent in return value of BESSEL_JN "
  49. "(%ld vs. %ld)", (long int) n2-n1,
  50. (long int) GFC_DESCRIPTOR_EXTENT(ret,0));
  51. stride = GFC_DESCRIPTOR_STRIDE(ret,0);
  52. if (unlikely (x == 0))
  53. {
  54. ret->base_addr[0] = 1;
  55. for (i = 1; i <= n2-n1; i++)
  56. ret->base_addr[i*stride] = 0;
  57. return;
  58. }
  59. last1 = MATHFUNC(jn) (n2, x);
  60. ret->base_addr[(n2-n1)*stride] = last1;
  61. if (n1 == n2)
  62. return;
  63. last2 = MATHFUNC(jn) (n2 - 1, x);
  64. ret->base_addr[(n2-n1-1)*stride] = last2;
  65. if (n1 + 1 == n2)
  66. return;
  67. x2rev = GFC_REAL_10_LITERAL(2.)/x;
  68. for (i = n2-n1-2; i >= 0; i--)
  69. {
  70. ret->base_addr[i*stride] = x2rev * (i+1+n1) * last2 - last1;
  71. last1 = last2;
  72. last2 = ret->base_addr[i*stride];
  73. }
  74. }
  75. #endif
  76. #if defined (HAVE_YNL)
  77. extern void bessel_yn_r10 (gfc_array_r10 * const restrict ret,
  78. int n1, int n2, GFC_REAL_10 x);
  79. export_proto(bessel_yn_r10);
  80. void
  81. bessel_yn_r10 (gfc_array_r10 * const restrict ret, int n1, int n2,
  82. GFC_REAL_10 x)
  83. {
  84. int i;
  85. index_type stride;
  86. GFC_REAL_10 last1, last2, x2rev;
  87. stride = GFC_DESCRIPTOR_STRIDE(ret,0);
  88. if (ret->base_addr == NULL)
  89. {
  90. size_t size = n2 < n1 ? 0 : n2-n1+1;
  91. GFC_DIMENSION_SET(ret->dim[0], 0, size-1, 1);
  92. ret->base_addr = xmallocarray (size, sizeof (GFC_REAL_10));
  93. ret->offset = 0;
  94. }
  95. if (unlikely (n2 < n1))
  96. return;
  97. if (unlikely (compile_options.bounds_check)
  98. && GFC_DESCRIPTOR_EXTENT(ret,0) != (n2-n1+1))
  99. runtime_error("Incorrect extent in return value of BESSEL_JN "
  100. "(%ld vs. %ld)", (long int) n2-n1,
  101. (long int) GFC_DESCRIPTOR_EXTENT(ret,0));
  102. stride = GFC_DESCRIPTOR_STRIDE(ret,0);
  103. if (unlikely (x == 0))
  104. {
  105. for (i = 0; i <= n2-n1; i++)
  106. #if defined(GFC_REAL_10_INFINITY)
  107. ret->base_addr[i*stride] = -GFC_REAL_10_INFINITY;
  108. #else
  109. ret->base_addr[i*stride] = -GFC_REAL_10_HUGE;
  110. #endif
  111. return;
  112. }
  113. last1 = MATHFUNC(yn) (n1, x);
  114. ret->base_addr[0] = last1;
  115. if (n1 == n2)
  116. return;
  117. last2 = MATHFUNC(yn) (n1 + 1, x);
  118. ret->base_addr[1*stride] = last2;
  119. if (n1 + 1 == n2)
  120. return;
  121. x2rev = GFC_REAL_10_LITERAL(2.)/x;
  122. for (i = 2; i <= n2 - n1; i++)
  123. {
  124. #if defined(GFC_REAL_10_INFINITY)
  125. if (unlikely (last2 == -GFC_REAL_10_INFINITY))
  126. {
  127. ret->base_addr[i*stride] = -GFC_REAL_10_INFINITY;
  128. }
  129. else
  130. #endif
  131. {
  132. ret->base_addr[i*stride] = x2rev * (i-1+n1) * last2 - last1;
  133. last1 = last2;
  134. last2 = ret->base_addr[i*stride];
  135. }
  136. }
  137. }
  138. #endif
  139. #endif