1 `/* Implementation of the BESSEL_JN and BESSEL_YN transformational
2 function using a recurrence algorithm.
3 Copyright 2010 Free Software Foundation, Inc.
4 Contributed by Tobias Burnus <burnus@net-b.de>
6 This file is part of the GNU Fortran runtime library (libgfortran).
8 Libgfortran is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public
10 License as published by the Free Software Foundation; either
11 version 3 of the License, or (at your option) any later version.
13 Libgfortran is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 Under Section 7 of GPL version 3, you are granted additional
19 permissions described in the GCC Runtime Library Exception, version
20 3.1, as published by the Free Software Foundation.
22 You should have received a copy of the GNU General Public License and
23 a copy of the GCC Runtime Library Exception along with this program;
24 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 <http://www.gnu.org/licenses/>. */
27 #include "libgfortran.h"
32 include(`mtype.m4')dnl
34 `#if defined (HAVE_'rtype_name`)
38 #if defined (HAVE_JN'Q`)
39 extern void bessel_jn_r'rtype_kind` ('rtype` * const restrict ret, int n1,
40 int n2, 'rtype_name` x);
41 export_proto(bessel_jn_r'rtype_kind`);
44 bessel_jn_r'rtype_kind` ('rtype` * const restrict ret, int n1, int n2, 'rtype_name` x)
49 'rtype_name` last1, last2, x2rev;
51 stride = GFC_DESCRIPTOR_STRIDE(ret,0);
53 if (ret->data == NULL)
55 size_t size = n2 < n1 ? 0 : n2-n1+1;
56 GFC_DIMENSION_SET(ret->dim[0], 0, size-1, 1);
57 ret->data = internal_malloc_size (sizeof ('rtype_name`) * size);
61 if (unlikely (n2 < n1))
64 if (unlikely (compile_options.bounds_check)
65 && GFC_DESCRIPTOR_EXTENT(ret,0) != (n2-n1+1))
66 runtime_error("Incorrect extent in return value of BESSEL_JN "
67 "(%ld vs. %ld)", (long int) n2-n1,
68 GFC_DESCRIPTOR_EXTENT(ret,0));
70 stride = GFC_DESCRIPTOR_STRIDE(ret,0);
72 if (unlikely (x == 0.0'Q`))
74 ret->data[0] = 1.0'Q`;
75 for (i = 1; i <= n2-n1; i++)
76 ret->data[i*stride] = 0.0'Q`;
80 ret->data = ret->data;
81 last1 = jn'q` (n2, x);
82 ret->data[(n2-n1)*stride] = last1;
87 last2 = jn'q` (n2 - 1, x);
88 ret->data[(n2-n1-1)*stride] = last2;
95 for (i = n2-n1-2; i >= 0; i--)
97 ret->data[i*stride] = x2rev * (i+1+n1) * last2 - last1;
99 last2 = ret->data[i*stride];
105 #if defined (HAVE_YN'Q`)
106 extern void bessel_yn_r'rtype_kind` ('rtype` * const restrict ret,
107 int n1, int n2, 'rtype_name` x);
108 export_proto(bessel_yn_r'rtype_kind`);
111 bessel_yn_r'rtype_kind` ('rtype` * const restrict ret, int n1, int n2,
117 'rtype_name` last1, last2, x2rev;
119 stride = GFC_DESCRIPTOR_STRIDE(ret,0);
121 if (ret->data == NULL)
123 size_t size = n2 < n1 ? 0 : n2-n1+1;
124 GFC_DIMENSION_SET(ret->dim[0], 0, size-1, 1);
125 ret->data = internal_malloc_size (sizeof ('rtype_name`) * size);
129 if (unlikely (n2 < n1))
132 if (unlikely (compile_options.bounds_check)
133 && GFC_DESCRIPTOR_EXTENT(ret,0) != (n2-n1+1))
134 runtime_error("Incorrect extent in return value of BESSEL_JN "
135 "(%ld vs. %ld)", (long int) n2-n1,
136 GFC_DESCRIPTOR_EXTENT(ret,0));
138 stride = GFC_DESCRIPTOR_STRIDE(ret,0);
140 if (unlikely (x == 0.0'Q`))
142 for (i = 0; i <= n2-n1; i++)
143 #if defined('rtype_name`_INFINITY)
144 ret->data[i*stride] = -'rtype_name`_INFINITY;
146 ret->data[i*stride] = -'rtype_name`_HUGE;
151 ret->data = ret->data;
152 last1 = yn'q` (n1, x);
153 ret->data[0] = last1;
158 last2 = yn'q` (n1 + 1, x);
159 ret->data[1*stride] = last2;
166 for (i = 2; i <= n1+n2; i++)
168 #if defined('rtype_name`_INFINITY)
169 if (unlikely (last2 == -'rtype_name`_INFINITY))
171 ret->data[i*stride] = -'rtype_name`_INFINITY;
176 ret->data[i*stride] = x2rev * (i-1+n1) * last2 - last1;
178 last2 = ret->data[i*stride];