00001 // Special functions -*- C++ -*- 00002 00003 // Copyright (C) 2006, 2007, 2008 00004 // Free Software Foundation, Inc. 00005 // 00006 // This file is part of the GNU ISO C++ Library. This library is free 00007 // software; you can redistribute it and/or modify it under the 00008 // terms of the GNU General Public License as published by the 00009 // Free Software Foundation; either version 2, or (at your option) 00010 // any later version. 00011 // 00012 // This library is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 // 00017 // You should have received a copy of the GNU General Public License along 00018 // with this library; see the file COPYING. If not, write to the Free 00019 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 00020 // USA. 00021 // 00022 // As a special exception, you may use this file as part of a free software 00023 // library without restriction. Specifically, if other files instantiate 00024 // templates or use macros or inline functions from this file, or you compile 00025 // this file and link it with other files to produce an executable, this 00026 // file does not by itself cause the resulting executable to be covered by 00027 // the GNU General Public License. This exception does not however 00028 // invalidate any other reasons why the executable file might be covered by 00029 // the GNU General Public License. 00030 00031 /** @file tr1/beta_function.tcc 00032 * This is an internal header file, included by other library headers. 00033 * You should not attempt to use it directly. 00034 */ 00035 00036 // 00037 // ISO C++ 14882 TR1: 5.2 Special functions 00038 // 00039 00040 // Written by Edward Smith-Rowland based on: 00041 // (1) Handbook of Mathematical Functions, 00042 // ed. Milton Abramowitz and Irene A. Stegun, 00043 // Dover Publications, 00044 // Section 6, pp. 253-266 00045 // (2) The Gnu Scientific Library, http://www.gnu.org/software/gsl 00046 // (3) Numerical Recipes in C, by W. H. Press, S. A. Teukolsky, 00047 // W. T. Vetterling, B. P. Flannery, Cambridge University Press (1992), 00048 // 2nd ed, pp. 213-216 00049 // (4) Gamma, Exploring Euler's Constant, Julian Havil, 00050 // Princeton, 2003. 00051 00052 #ifndef _GLIBCXX_TR1_BETA_FUNCTION_TCC 00053 #define _GLIBCXX_TR1_BETA_FUNCTION_TCC 1 00054 00055 namespace std 00056 { 00057 namespace tr1 00058 { 00059 00060 // [5.2] Special functions 00061 00062 // Implementation-space details. 00063 namespace __detail 00064 { 00065 00066 /** 00067 * @brief Return the beta function: \f$B(x,y)\f$. 00068 * 00069 * The beta function is defined by 00070 * @f[ 00071 * B(x,y) = \frac{\Gamma(x)\Gamma(y)}{\Gamma(x+y)} 00072 * @f] 00073 * 00074 * @param __x The first argument of the beta function. 00075 * @param __y The second argument of the beta function. 00076 * @return The beta function. 00077 */ 00078 template<typename _Tp> 00079 _Tp 00080 __beta_gamma(_Tp __x, _Tp __y) 00081 { 00082 00083 _Tp __bet; 00084 #if _GLIBCXX_USE_C99_MATH_TR1 00085 if (__x > __y) 00086 { 00087 __bet = std::tr1::tgamma(__x) 00088 / std::tr1::tgamma(__x + __y); 00089 __bet *= std::tr1::tgamma(__y); 00090 } 00091 else 00092 { 00093 __bet = std::tr1::tgamma(__y) 00094 / std::tr1::tgamma(__x + __y); 00095 __bet *= std::tr1::tgamma(__x); 00096 } 00097 #else 00098 if (__x > __y) 00099 { 00100 __bet = __gamma(__x) / __gamma(__x + __y); 00101 __bet *= __gamma(__y); 00102 } 00103 else 00104 { 00105 __bet = __gamma(__y) / __gamma(__x + __y); 00106 __bet *= __gamma(__x); 00107 } 00108 #endif 00109 00110 return __bet; 00111 } 00112 00113 /** 00114 * @brief Return the beta function \f$B(x,y)\f$ using 00115 * the log gamma functions. 00116 * 00117 * The beta function is defined by 00118 * @f[ 00119 * B(x,y) = \frac{\Gamma(x)\Gamma(y)}{\Gamma(x+y)} 00120 * @f] 00121 * 00122 * @param __x The first argument of the beta function. 00123 * @param __y The second argument of the beta function. 00124 * @return The beta function. 00125 */ 00126 template<typename _Tp> 00127 _Tp 00128 __beta_lgamma(_Tp __x, _Tp __y) 00129 { 00130 #if _GLIBCXX_USE_C99_MATH_TR1 00131 _Tp __bet = std::tr1::lgamma(__x) 00132 + std::tr1::lgamma(__y) 00133 - std::tr1::lgamma(__x + __y); 00134 #else 00135 _Tp __bet = __log_gamma(__x) 00136 + __log_gamma(__y) 00137 - __log_gamma(__x + __y); 00138 #endif 00139 __bet = std::exp(__bet); 00140 return __bet; 00141 } 00142 00143 00144 /** 00145 * @brief Return the beta function \f$B(x,y)\f$ using 00146 * the product form. 00147 * 00148 * The beta function is defined by 00149 * @f[ 00150 * B(x,y) = \frac{\Gamma(x)\Gamma(y)}{\Gamma(x+y)} 00151 * @f] 00152 * 00153 * @param __x The first argument of the beta function. 00154 * @param __y The second argument of the beta function. 00155 * @return The beta function. 00156 */ 00157 template<typename _Tp> 00158 _Tp 00159 __beta_product(_Tp __x, _Tp __y) 00160 { 00161 00162 _Tp __bet = (__x + __y) / (__x * __y); 00163 00164 unsigned int __max_iter = 1000000; 00165 for (unsigned int __k = 1; __k < __max_iter; ++__k) 00166 { 00167 _Tp __term = (_Tp(1) + (__x + __y) / __k) 00168 / ((_Tp(1) + __x / __k) * (_Tp(1) + __y / __k)); 00169 __bet *= __term; 00170 } 00171 00172 return __bet; 00173 } 00174 00175 00176 /** 00177 * @brief Return the beta function \f$ B(x,y) \f$. 00178 * 00179 * The beta function is defined by 00180 * @f[ 00181 * B(x,y) = \frac{\Gamma(x)\Gamma(y)}{\Gamma(x+y)} 00182 * @f] 00183 * 00184 * @param __x The first argument of the beta function. 00185 * @param __y The second argument of the beta function. 00186 * @return The beta function. 00187 */ 00188 template<typename _Tp> 00189 inline _Tp 00190 __beta(_Tp __x, _Tp __y) 00191 { 00192 if (__isnan(__x) || __isnan(__y)) 00193 return std::numeric_limits<_Tp>::quiet_NaN(); 00194 else 00195 return __beta_lgamma(__x, __y); 00196 } 00197 00198 } // namespace std::tr1::__detail 00199 } 00200 } 00201 00202 #endif // __GLIBCXX_TR1_BETA_FUNCTION_TCC