1 // Special functions -*- C++ -*-
3 // Copyright (C) 2006-2017 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)
51 _GLIBCXX_BEGIN_NAMESPACE_VERSION
53 #if _GLIBCXX_USE_STD_SPEC_FUNCS
54 # define _GLIBCXX_MATH_NS ::std
55 #elif defined(_GLIBCXX_TR1_CMATH)
58 # define _GLIBCXX_MATH_NS ::std::tr1
60 # error do not include this header directly, use <cmath> or <tr1/cmath>
62 // [5.2] Special functions
64 // Implementation-space details.
68 * @brief Return the beta function: \f$B(x,y)\f$.
70 * The beta function is defined by
72 * B(x,y) = \frac{\Gamma(x)\Gamma(y)}{\Gamma(x+y)}
75 * @param __x The first argument of the beta function.
76 * @param __y The second argument of the beta function.
77 * @return The beta function.
79 template<typename _Tp>
81 __beta_gamma(_Tp __x, _Tp __y)
85 #if _GLIBCXX_USE_C99_MATH_TR1
88 __bet = _GLIBCXX_MATH_NS::tgamma(__x)
89 / _GLIBCXX_MATH_NS::tgamma(__x + __y);
90 __bet *= _GLIBCXX_MATH_NS::tgamma(__y);
94 __bet = _GLIBCXX_MATH_NS::tgamma(__y)
95 / _GLIBCXX_MATH_NS::tgamma(__x + __y);
96 __bet *= _GLIBCXX_MATH_NS::tgamma(__x);
101 __bet = __gamma(__x) / __gamma(__x + __y);
102 __bet *= __gamma(__y);
106 __bet = __gamma(__y) / __gamma(__x + __y);
107 __bet *= __gamma(__x);
115 * @brief Return the beta function \f$B(x,y)\f$ using
116 * the log gamma functions.
118 * The beta function is defined by
120 * B(x,y) = \frac{\Gamma(x)\Gamma(y)}{\Gamma(x+y)}
123 * @param __x The first argument of the beta function.
124 * @param __y The second argument of the beta function.
125 * @return The beta function.
127 template<typename _Tp>
129 __beta_lgamma(_Tp __x, _Tp __y)
131 #if _GLIBCXX_USE_C99_MATH_TR1
132 _Tp __bet = _GLIBCXX_MATH_NS::lgamma(__x)
133 + _GLIBCXX_MATH_NS::lgamma(__y)
134 - _GLIBCXX_MATH_NS::lgamma(__x + __y);
136 _Tp __bet = __log_gamma(__x)
138 - __log_gamma(__x + __y);
140 __bet = std::exp(__bet);
146 * @brief Return the beta function \f$B(x,y)\f$ using
149 * The beta function is defined by
151 * B(x,y) = \frac{\Gamma(x)\Gamma(y)}{\Gamma(x+y)}
154 * @param __x The first argument of the beta function.
155 * @param __y The second argument of the beta function.
156 * @return The beta function.
158 template<typename _Tp>
160 __beta_product(_Tp __x, _Tp __y)
163 _Tp __bet = (__x + __y) / (__x * __y);
165 unsigned int __max_iter = 1000000;
166 for (unsigned int __k = 1; __k < __max_iter; ++__k)
168 _Tp __term = (_Tp(1) + (__x + __y) / __k)
169 / ((_Tp(1) + __x / __k) * (_Tp(1) + __y / __k));
178 * @brief Return the beta function \f$ B(x,y) \f$.
180 * The beta function is defined by
182 * B(x,y) = \frac{\Gamma(x)\Gamma(y)}{\Gamma(x+y)}
185 * @param __x The first argument of the beta function.
186 * @param __y The second argument of the beta function.
187 * @return The beta function.
189 template<typename _Tp>
191 __beta(_Tp __x, _Tp __y)
193 if (__isnan(__x) || __isnan(__y))
194 return std::numeric_limits<_Tp>::quiet_NaN();
196 return __beta_lgamma(__x, __y);
198 } // namespace __detail
199 #undef _GLIBCXX_MATH_NS
200 #if ! _GLIBCXX_USE_STD_SPEC_FUNCS && defined(_GLIBCXX_TR1_CMATH)
204 _GLIBCXX_END_NAMESPACE_VERSION
207 #endif // _GLIBCXX_TR1_BETA_FUNCTION_TCC