]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/tr1/modified_bessel_func.tcc
Update Copyright years for files modified in 2011 and/or 2012.
[thirdparty/gcc.git] / libstdc++-v3 / include / tr1 / modified_bessel_func.tcc
1 // Special functions -*- C++ -*-
2
3 // Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
10 // any later version.
11 //
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
20
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
25
26 /** @file tr1/modified_bessel_func.tcc
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{tr1/cmath}
29 */
30
31 //
32 // ISO C++ 14882 TR1: 5.2 Special functions
33 //
34
35 // Written by Edward Smith-Rowland.
36 //
37 // References:
38 // (1) Handbook of Mathematical Functions,
39 // Ed. Milton Abramowitz and Irene A. Stegun,
40 // Dover Publications,
41 // Section 9, pp. 355-434, Section 10 pp. 435-478
42 // (2) The Gnu Scientific Library, http://www.gnu.org/software/gsl
43 // (3) Numerical Recipes in C, by W. H. Press, S. A. Teukolsky,
44 // W. T. Vetterling, B. P. Flannery, Cambridge University Press (1992),
45 // 2nd ed, pp. 246-249.
46
47 #ifndef _GLIBCXX_TR1_MODIFIED_BESSEL_FUNC_TCC
48 #define _GLIBCXX_TR1_MODIFIED_BESSEL_FUNC_TCC 1
49
50 #include "special_function_util.h"
51
52 namespace std _GLIBCXX_VISIBILITY(default)
53 {
54 namespace tr1
55 {
56 // [5.2] Special functions
57
58 // Implementation-space details.
59 namespace __detail
60 {
61 _GLIBCXX_BEGIN_NAMESPACE_VERSION
62
63 /**
64 * @brief Compute the modified Bessel functions @f$ I_\nu(x) @f$ and
65 * @f$ K_\nu(x) @f$ and their first derivatives
66 * @f$ I'_\nu(x) @f$ and @f$ K'_\nu(x) @f$ respectively.
67 * These four functions are computed together for numerical
68 * stability.
69 *
70 * @param __nu The order of the Bessel functions.
71 * @param __x The argument of the Bessel functions.
72 * @param __Inu The output regular modified Bessel function.
73 * @param __Knu The output irregular modified Bessel function.
74 * @param __Ipnu The output derivative of the regular
75 * modified Bessel function.
76 * @param __Kpnu The output derivative of the irregular
77 * modified Bessel function.
78 */
79 template <typename _Tp>
80 void
81 __bessel_ik(const _Tp __nu, const _Tp __x,
82 _Tp & __Inu, _Tp & __Knu, _Tp & __Ipnu, _Tp & __Kpnu)
83 {
84 if (__x == _Tp(0))
85 {
86 if (__nu == _Tp(0))
87 {
88 __Inu = _Tp(1);
89 __Ipnu = _Tp(0);
90 }
91 else if (__nu == _Tp(1))
92 {
93 __Inu = _Tp(0);
94 __Ipnu = _Tp(0.5L);
95 }
96 else
97 {
98 __Inu = _Tp(0);
99 __Ipnu = _Tp(0);
100 }
101 __Knu = std::numeric_limits<_Tp>::infinity();
102 __Kpnu = -std::numeric_limits<_Tp>::infinity();
103 return;
104 }
105
106 const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
107 const _Tp __fp_min = _Tp(10) * std::numeric_limits<_Tp>::epsilon();
108 const int __max_iter = 15000;
109 const _Tp __x_min = _Tp(2);
110
111 const int __nl = static_cast<int>(__nu + _Tp(0.5L));
112
113 const _Tp __mu = __nu - __nl;
114 const _Tp __mu2 = __mu * __mu;
115 const _Tp __xi = _Tp(1) / __x;
116 const _Tp __xi2 = _Tp(2) * __xi;
117 _Tp __h = __nu * __xi;
118 if ( __h < __fp_min )
119 __h = __fp_min;
120 _Tp __b = __xi2 * __nu;
121 _Tp __d = _Tp(0);
122 _Tp __c = __h;
123 int __i;
124 for ( __i = 1; __i <= __max_iter; ++__i )
125 {
126 __b += __xi2;
127 __d = _Tp(1) / (__b + __d);
128 __c = __b + _Tp(1) / __c;
129 const _Tp __del = __c * __d;
130 __h *= __del;
131 if (std::abs(__del - _Tp(1)) < __eps)
132 break;
133 }
134 if (__i > __max_iter)
135 std::__throw_runtime_error(__N("Argument x too large "
136 "in __bessel_jn; "
137 "try asymptotic expansion."));
138 _Tp __Inul = __fp_min;
139 _Tp __Ipnul = __h * __Inul;
140 _Tp __Inul1 = __Inul;
141 _Tp __Ipnu1 = __Ipnul;
142 _Tp __fact = __nu * __xi;
143 for (int __l = __nl; __l >= 1; --__l)
144 {
145 const _Tp __Inutemp = __fact * __Inul + __Ipnul;
146 __fact -= __xi;
147 __Ipnul = __fact * __Inutemp + __Inul;
148 __Inul = __Inutemp;
149 }
150 _Tp __f = __Ipnul / __Inul;
151 _Tp __Kmu, __Knu1;
152 if (__x < __x_min)
153 {
154 const _Tp __x2 = __x / _Tp(2);
155 const _Tp __pimu = __numeric_constants<_Tp>::__pi() * __mu;
156 const _Tp __fact = (std::abs(__pimu) < __eps
157 ? _Tp(1) : __pimu / std::sin(__pimu));
158 _Tp __d = -std::log(__x2);
159 _Tp __e = __mu * __d;
160 const _Tp __fact2 = (std::abs(__e) < __eps
161 ? _Tp(1) : std::sinh(__e) / __e);
162 _Tp __gam1, __gam2, __gampl, __gammi;
163 __gamma_temme(__mu, __gam1, __gam2, __gampl, __gammi);
164 _Tp __ff = __fact
165 * (__gam1 * std::cosh(__e) + __gam2 * __fact2 * __d);
166 _Tp __sum = __ff;
167 __e = std::exp(__e);
168 _Tp __p = __e / (_Tp(2) * __gampl);
169 _Tp __q = _Tp(1) / (_Tp(2) * __e * __gammi);
170 _Tp __c = _Tp(1);
171 __d = __x2 * __x2;
172 _Tp __sum1 = __p;
173 int __i;
174 for (__i = 1; __i <= __max_iter; ++__i)
175 {
176 __ff = (__i * __ff + __p + __q) / (__i * __i - __mu2);
177 __c *= __d / __i;
178 __p /= __i - __mu;
179 __q /= __i + __mu;
180 const _Tp __del = __c * __ff;
181 __sum += __del;
182 const _Tp __del1 = __c * (__p - __i * __ff);
183 __sum1 += __del1;
184 if (std::abs(__del) < __eps * std::abs(__sum))
185 break;
186 }
187 if (__i > __max_iter)
188 std::__throw_runtime_error(__N("Bessel k series failed to converge "
189 "in __bessel_jn."));
190 __Kmu = __sum;
191 __Knu1 = __sum1 * __xi2;
192 }
193 else
194 {
195 _Tp __b = _Tp(2) * (_Tp(1) + __x);
196 _Tp __d = _Tp(1) / __b;
197 _Tp __delh = __d;
198 _Tp __h = __delh;
199 _Tp __q1 = _Tp(0);
200 _Tp __q2 = _Tp(1);
201 _Tp __a1 = _Tp(0.25L) - __mu2;
202 _Tp __q = __c = __a1;
203 _Tp __a = -__a1;
204 _Tp __s = _Tp(1) + __q * __delh;
205 int __i;
206 for (__i = 2; __i <= __max_iter; ++__i)
207 {
208 __a -= 2 * (__i - 1);
209 __c = -__a * __c / __i;
210 const _Tp __qnew = (__q1 - __b * __q2) / __a;
211 __q1 = __q2;
212 __q2 = __qnew;
213 __q += __c * __qnew;
214 __b += _Tp(2);
215 __d = _Tp(1) / (__b + __a * __d);
216 __delh = (__b * __d - _Tp(1)) * __delh;
217 __h += __delh;
218 const _Tp __dels = __q * __delh;
219 __s += __dels;
220 if ( std::abs(__dels / __s) < __eps )
221 break;
222 }
223 if (__i > __max_iter)
224 std::__throw_runtime_error(__N("Steed's method failed "
225 "in __bessel_jn."));
226 __h = __a1 * __h;
227 __Kmu = std::sqrt(__numeric_constants<_Tp>::__pi() / (_Tp(2) * __x))
228 * std::exp(-__x) / __s;
229 __Knu1 = __Kmu * (__mu + __x + _Tp(0.5L) - __h) * __xi;
230 }
231
232 _Tp __Kpmu = __mu * __xi * __Kmu - __Knu1;
233 _Tp __Inumu = __xi / (__f * __Kmu - __Kpmu);
234 __Inu = __Inumu * __Inul1 / __Inul;
235 __Ipnu = __Inumu * __Ipnu1 / __Inul;
236 for ( __i = 1; __i <= __nl; ++__i )
237 {
238 const _Tp __Knutemp = (__mu + __i) * __xi2 * __Knu1 + __Kmu;
239 __Kmu = __Knu1;
240 __Knu1 = __Knutemp;
241 }
242 __Knu = __Kmu;
243 __Kpnu = __nu * __xi * __Kmu - __Knu1;
244
245 return;
246 }
247
248
249 /**
250 * @brief Return the regular modified Bessel function of order
251 * \f$ \nu \f$: \f$ I_{\nu}(x) \f$.
252 *
253 * The regular modified cylindrical Bessel function is:
254 * @f[
255 * I_{\nu}(x) = \sum_{k=0}^{\infty}
256 * \frac{(x/2)^{\nu + 2k}}{k!\Gamma(\nu+k+1)}
257 * @f]
258 *
259 * @param __nu The order of the regular modified Bessel function.
260 * @param __x The argument of the regular modified Bessel function.
261 * @return The output regular modified Bessel function.
262 */
263 template<typename _Tp>
264 _Tp
265 __cyl_bessel_i(const _Tp __nu, const _Tp __x)
266 {
267 if (__nu < _Tp(0) || __x < _Tp(0))
268 std::__throw_domain_error(__N("Bad argument "
269 "in __cyl_bessel_i."));
270 else if (__isnan(__nu) || __isnan(__x))
271 return std::numeric_limits<_Tp>::quiet_NaN();
272 else if (__x * __x < _Tp(10) * (__nu + _Tp(1)))
273 return __cyl_bessel_ij_series(__nu, __x, +_Tp(1), 200);
274 else
275 {
276 _Tp __I_nu, __K_nu, __Ip_nu, __Kp_nu;
277 __bessel_ik(__nu, __x, __I_nu, __K_nu, __Ip_nu, __Kp_nu);
278 return __I_nu;
279 }
280 }
281
282
283 /**
284 * @brief Return the irregular modified Bessel function
285 * \f$ K_{\nu}(x) \f$ of order \f$ \nu \f$.
286 *
287 * The irregular modified Bessel function is defined by:
288 * @f[
289 * K_{\nu}(x) = \frac{\pi}{2}
290 * \frac{I_{-\nu}(x) - I_{\nu}(x)}{\sin \nu\pi}
291 * @f]
292 * where for integral \f$ \nu = n \f$ a limit is taken:
293 * \f$ lim_{\nu \to n} \f$.
294 *
295 * @param __nu The order of the irregular modified Bessel function.
296 * @param __x The argument of the irregular modified Bessel function.
297 * @return The output irregular modified Bessel function.
298 */
299 template<typename _Tp>
300 _Tp
301 __cyl_bessel_k(const _Tp __nu, const _Tp __x)
302 {
303 if (__nu < _Tp(0) || __x < _Tp(0))
304 std::__throw_domain_error(__N("Bad argument "
305 "in __cyl_bessel_k."));
306 else if (__isnan(__nu) || __isnan(__x))
307 return std::numeric_limits<_Tp>::quiet_NaN();
308 else
309 {
310 _Tp __I_nu, __K_nu, __Ip_nu, __Kp_nu;
311 __bessel_ik(__nu, __x, __I_nu, __K_nu, __Ip_nu, __Kp_nu);
312 return __K_nu;
313 }
314 }
315
316
317 /**
318 * @brief Compute the spherical modified Bessel functions
319 * @f$ i_n(x) @f$ and @f$ k_n(x) @f$ and their first
320 * derivatives @f$ i'_n(x) @f$ and @f$ k'_n(x) @f$
321 * respectively.
322 *
323 * @param __n The order of the modified spherical Bessel function.
324 * @param __x The argument of the modified spherical Bessel function.
325 * @param __i_n The output regular modified spherical Bessel function.
326 * @param __k_n The output irregular modified spherical
327 * Bessel function.
328 * @param __ip_n The output derivative of the regular modified
329 * spherical Bessel function.
330 * @param __kp_n The output derivative of the irregular modified
331 * spherical Bessel function.
332 */
333 template <typename _Tp>
334 void
335 __sph_bessel_ik(const unsigned int __n, const _Tp __x,
336 _Tp & __i_n, _Tp & __k_n, _Tp & __ip_n, _Tp & __kp_n)
337 {
338 const _Tp __nu = _Tp(__n) + _Tp(0.5L);
339
340 _Tp __I_nu, __Ip_nu, __K_nu, __Kp_nu;
341 __bessel_ik(__nu, __x, __I_nu, __K_nu, __Ip_nu, __Kp_nu);
342
343 const _Tp __factor = __numeric_constants<_Tp>::__sqrtpio2()
344 / std::sqrt(__x);
345
346 __i_n = __factor * __I_nu;
347 __k_n = __factor * __K_nu;
348 __ip_n = __factor * __Ip_nu - __i_n / (_Tp(2) * __x);
349 __kp_n = __factor * __Kp_nu - __k_n / (_Tp(2) * __x);
350
351 return;
352 }
353
354
355 /**
356 * @brief Compute the Airy functions
357 * @f$ Ai(x) @f$ and @f$ Bi(x) @f$ and their first
358 * derivatives @f$ Ai'(x) @f$ and @f$ Bi(x) @f$
359 * respectively.
360 *
361 * @param __n The order of the Airy functions.
362 * @param __x The argument of the Airy functions.
363 * @param __i_n The output Airy function.
364 * @param __k_n The output Airy function.
365 * @param __ip_n The output derivative of the Airy function.
366 * @param __kp_n The output derivative of the Airy function.
367 */
368 template <typename _Tp>
369 void
370 __airy(const _Tp __x,
371 _Tp & __Ai, _Tp & __Bi, _Tp & __Aip, _Tp & __Bip)
372 {
373 const _Tp __absx = std::abs(__x);
374 const _Tp __rootx = std::sqrt(__absx);
375 const _Tp __z = _Tp(2) * __absx * __rootx / _Tp(3);
376
377 if (__isnan(__x))
378 return std::numeric_limits<_Tp>::quiet_NaN();
379 else if (__x > _Tp(0))
380 {
381 _Tp __I_nu, __Ip_nu, __K_nu, __Kp_nu;
382
383 __bessel_ik(_Tp(1) / _Tp(3), __z, __I_nu, __K_nu, __Ip_nu, __Kp_nu);
384 __Ai = __rootx * __K_nu
385 / (__numeric_constants<_Tp>::__sqrt3()
386 * __numeric_constants<_Tp>::__pi());
387 __Bi = __rootx * (__K_nu / __numeric_constants<_Tp>::__pi()
388 + _Tp(2) * __I_nu / __numeric_constants<_Tp>::__sqrt3());
389
390 __bessel_ik(_Tp(2) / _Tp(3), __z, __I_nu, __K_nu, __Ip_nu, __Kp_nu);
391 __Aip = -__x * __K_nu
392 / (__numeric_constants<_Tp>::__sqrt3()
393 * __numeric_constants<_Tp>::__pi());
394 __Bip = __x * (__K_nu / __numeric_constants<_Tp>::__pi()
395 + _Tp(2) * __I_nu
396 / __numeric_constants<_Tp>::__sqrt3());
397 }
398 else if (__x < _Tp(0))
399 {
400 _Tp __J_nu, __Jp_nu, __N_nu, __Np_nu;
401
402 __bessel_jn(_Tp(1) / _Tp(3), __z, __J_nu, __N_nu, __Jp_nu, __Np_nu);
403 __Ai = __rootx * (__J_nu
404 - __N_nu / __numeric_constants<_Tp>::__sqrt3()) / _Tp(2);
405 __Bi = -__rootx * (__N_nu
406 + __J_nu / __numeric_constants<_Tp>::__sqrt3()) / _Tp(2);
407
408 __bessel_jn(_Tp(2) / _Tp(3), __z, __J_nu, __N_nu, __Jp_nu, __Np_nu);
409 __Aip = __absx * (__N_nu / __numeric_constants<_Tp>::__sqrt3()
410 + __J_nu) / _Tp(2);
411 __Bip = __absx * (__J_nu / __numeric_constants<_Tp>::__sqrt3()
412 - __N_nu) / _Tp(2);
413 }
414 else
415 {
416 // Reference:
417 // Abramowitz & Stegun, page 446 section 10.4.4 on Airy functions.
418 // The number is Ai(0) = 3^{-2/3}/\Gamma(2/3).
419 __Ai = _Tp(0.35502805388781723926L);
420 __Bi = __Ai * __numeric_constants<_Tp>::__sqrt3();
421
422 // Reference:
423 // Abramowitz & Stegun, page 446 section 10.4.5 on Airy functions.
424 // The number is Ai'(0) = -3^{-1/3}/\Gamma(1/3).
425 __Aip = -_Tp(0.25881940379280679840L);
426 __Bip = -__Aip * __numeric_constants<_Tp>::__sqrt3();
427 }
428
429 return;
430 }
431
432 _GLIBCXX_END_NAMESPACE_VERSION
433 } // namespace std::tr1::__detail
434 }
435 }
436
437 #endif // _GLIBCXX_TR1_MODIFIED_BESSEL_FUNC_TCC