1 // Special functions -*- C++ -*-
3 // Copyright (C) 2006-2016 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file tr1/beta_function.tcc
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{tr1/cmath}
31 // ISO C++ 14882 TR1: 5.2 Special functions
34 // Written by Edward Smith-Rowland based on:
35 // (1) Handbook of Mathematical Functions,
36 // ed. Milton Abramowitz and Irene A. Stegun,
37 // Dover Publications,
38 // Section 6, pp. 253-266
39 // (2) The Gnu Scientific Library, http://www.gnu.org/software/gsl
40 // (3) Numerical Recipes in C, by W. H. Press, S. A. Teukolsky,
41 // W. T. Vetterling, B. P. Flannery, Cambridge University Press (1992),
42 // 2nd ed, pp. 213-216
43 // (4) Gamma, Exploring Euler's Constant, Julian Havil,
46 #ifndef _GLIBCXX_TR1_BETA_FUNCTION_TCC
47 #define _GLIBCXX_TR1_BETA_FUNCTION_TCC 1
49 namespace std _GLIBCXX_VISIBILITY(default)
53 // [5.2] Special functions
55 // Implementation-space details.
58 _GLIBCXX_BEGIN_NAMESPACE_VERSION
61 * @brief Return the beta function: \f$B(x,y)\f$.
63 * The beta function is defined by
65 * B(x,y) = \frac{\Gamma(x)\Gamma(y)}{\Gamma(x+y)}
68 * @param __x The first argument of the beta function.
69 * @param __y The second argument of the beta function.
70 * @return The beta function.
72 template<typename _Tp>
74 __beta_gamma(_Tp __x, _Tp __y)
78 #if _GLIBCXX_USE_C99_MATH_TR1
81 __bet = std::tr1::tgamma(__x)
82 / std::tr1::tgamma(__x + __y);
83 __bet *= std::tr1::tgamma(__y);
87 __bet = std::tr1::tgamma(__y)
88 / std::tr1::tgamma(__x + __y);
89 __bet *= std::tr1::tgamma(__x);
94 __bet = __gamma(__x) / __gamma(__x + __y);
95 __bet *= __gamma(__y);
99 __bet = __gamma(__y) / __gamma(__x + __y);
100 __bet *= __gamma(__x);
108 * @brief Return the beta function \f$B(x,y)\f$ using
109 * the log gamma functions.
111 * The beta function is defined by
113 * B(x,y) = \frac{\Gamma(x)\Gamma(y)}{\Gamma(x+y)}
116 * @param __x The first argument of the beta function.
117 * @param __y The second argument of the beta function.
118 * @return The beta function.
120 template<typename _Tp>
122 __beta_lgamma(_Tp __x, _Tp __y)
124 #if _GLIBCXX_USE_C99_MATH_TR1
125 _Tp __bet = std::tr1::lgamma(__x)
126 + std::tr1::lgamma(__y)
127 - std::tr1::lgamma(__x + __y);
129 _Tp __bet = __log_gamma(__x)
131 - __log_gamma(__x + __y);
133 __bet = std::exp(__bet);
139 * @brief Return the beta function \f$B(x,y)\f$ using
142 * The beta function is defined by
144 * B(x,y) = \frac{\Gamma(x)\Gamma(y)}{\Gamma(x+y)}
147 * @param __x The first argument of the beta function.
148 * @param __y The second argument of the beta function.
149 * @return The beta function.
151 template<typename _Tp>
153 __beta_product(_Tp __x, _Tp __y)
156 _Tp __bet = (__x + __y) / (__x * __y);
158 unsigned int __max_iter = 1000000;
159 for (unsigned int __k = 1; __k < __max_iter; ++__k)
161 _Tp __term = (_Tp(1) + (__x + __y) / __k)
162 / ((_Tp(1) + __x / __k) * (_Tp(1) + __y / __k));
171 * @brief Return the beta function \f$ B(x,y) \f$.
173 * The beta function is defined by
175 * B(x,y) = \frac{\Gamma(x)\Gamma(y)}{\Gamma(x+y)}
178 * @param __x The first argument of the beta function.
179 * @param __y The second argument of the beta function.
180 * @return The beta function.
182 template<typename _Tp>
184 __beta(_Tp __x, _Tp __y)
186 if (__isnan(__x) || __isnan(__y))
187 return std::numeric_limits<_Tp>::quiet_NaN();
189 return __beta_lgamma(__x, __y);
192 _GLIBCXX_END_NAMESPACE_VERSION
193 } // namespace std::tr1::__detail
197 #endif // _GLIBCXX_TR1_BETA_FUNCTION_TCC