Difference between revisions of "cpp/numeric/special functions/beta"
m (→Example: +C(n,k) via β vs γ.) |
Andreas Krug (Talk | contribs) m (.) |
||
(6 intermediate revisions by 2 users not shown) | |||
Line 3: | Line 3: | ||
{{dcl begin}} | {{dcl begin}} | ||
{{dcl header|cmath}} | {{dcl header|cmath}} | ||
− | {{dcl|num=1| | + | {{dcl rev multi|num=1|since1=c++17|dcl1= |
− | double beta( double x, double y ); | + | float beta ( float x, float y ); |
+ | double beta ( double x, double y ); | ||
+ | long double beta ( long double x, long double y ); | ||
+ | |since2=c++23|dcl2= | ||
+ | /* floating-point-type */ beta( /* floating-point-type */ x, | ||
+ | /* floating-point-type */ y ); | ||
+ | }} | ||
+ | {{dcl|num=2|since=c++17| | ||
float betaf( float x, float y ); | float betaf( float x, float y ); | ||
+ | }} | ||
+ | {{dcl|num=3|since=c++17| | ||
long double betal( long double x, long double y ); | long double betal( long double x, long double y ); | ||
}} | }} | ||
− | {{dcl|num= | + | {{dcl h|[[#Notes|Additional overloads]]}} |
− | + | {{dcl header|cmath}} | |
+ | {{dcl|num=A|since=c++17| | ||
+ | template< class Arithmetic1, class Arithmetic2 > | ||
+ | /* common-floating-point-type */ beta( Arithmetic1 x, Arithmetic2 y ); | ||
}} | }} | ||
{{dcl end}} | {{dcl end}} | ||
− | @1@ Computes the {{enwiki| | + | @1-3@ Computes the {{enwiki|Beta function}} of {{c|x}} and {{c|y}}.{{rev inl|since=c++23| The library provides overloads of {{tt|std::beta}} for all cv-unqualified floating-point types as the type of the parameters {{c|x}} and {{c|y}}.}} |
− | @ | + | @A@ Additional overloads are provided for all other combinations of arithmetic types. |
===Parameters=== | ===Parameters=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par|x, y| | + | {{par|x, y|floating-point or integer values}} |
{{par end}} | {{par end}} | ||
===Return value=== | ===Return value=== | ||
− | If no errors occur, value of the beta function of {{ | + | If no errors occur, value of the beta function of {{c|x}} and {{c|y}}, that is {{mathjax-or|\(\int_{0}^{1}{ {t}^{x-1}{(1-t)}^{y-1}\mathsf{d}t}\)|{{minteg|0|1|t{{su|p=x-1}}(1-t){{su|p=(y-1)}}d''t''}}}}, or, equivalently, {{mathjax-or|\(\frac{\Gamma(x)\Gamma(y)}{\Gamma(x+y)}\)|{{mfrac|Γ(x)Γ(y)|Γ(x+y)}}}} is returned. |
===Error handling=== | ===Error handling=== | ||
− | Errors may be reported as specified in {{lc|math_errhandling}} | + | Errors may be reported as specified in {{lc|math_errhandling}}. |
− | + | * If any argument is NaN, NaN is returned and domain error is not reported. | |
− | * If any argument is NaN, NaN is returned and domain error is not reported | + | * The function is only required to be defined where both {{c|x}} and {{c|y}} are greater than zero, and is allowed to report a domain error otherwise. |
− | * The function is only required to be defined where both {{ | + | |
===Notes=== | ===Notes=== | ||
− | {{cpp/numeric/ | + | {{cpp/numeric/special functions/older impl note}} |
+ | |||
+ | An implementation of this function is also [https://www.boost.org/doc/libs/release/libs/math/doc/html/math_toolkit/sf_beta/beta_function.html available in boost.math]. | ||
− | + | {{c|std::beta(x, y)}} equals {{c|std::beta(y, x)}}. | |
− | {{c|beta(x, y)}} equals {{ | + | When {{c|x}} and {{c|y}} are positive integers, {{c|std::beta(x, y)}} equals {{mathjax-or|1=\(\frac{(x-1)!(y-1)!}{(x+y-1)!}\)|2={{mfrac|(x-1)!(y-1)!|(x+y-1)!}}}}. |
− | + | Binomial coefficients can be expressed in terms of the beta function: {{mathjax-or|1=\(\binom{n}{k} = \frac{1}{(n+1)B(n-k+1,k+1)}\)|2={{mparen|(|)|n|k}}={{mfrac|1|(n+1)Β(n-k+1,k+1)}}}}. | |
− | + | {{cpp/numeric/special functions/additional overload note|beta}} | |
===Example=== | ===Example=== | ||
− | {{example|code= | + | {{example |
− | + | |code= | |
#include <cassert> | #include <cassert> | ||
+ | #include <cmath> | ||
#include <iomanip> | #include <iomanip> | ||
#include <iostream> | #include <iostream> | ||
+ | #include <numbers> | ||
#include <string> | #include <string> | ||
Line 57: | Line 72: | ||
long binom_via_gamma(int n, int k) | long binom_via_gamma(int n, int k) | ||
{ | { | ||
− | return std::lround( std::tgamma(n + 1) | + | return std::lround(std::tgamma(n + 1) / |
− | + | (std::tgamma(n - k + 1) * | |
− | + | std::tgamma(k + 1))); | |
} | } | ||
Line 75: | Line 90: | ||
std::cout << '\n'; | std::cout << '\n'; | ||
} | } | ||
+ | |||
+ | // A spot-check | ||
+ | const long double p = 0.123; // a random value in [0, 1] | ||
+ | const long double q = 1 - p; | ||
+ | const long double π = std::numbers::pi_v<long double>; | ||
+ | std::cout << "\n\n" << std::setprecision(19) | ||
+ | << "β(p,1-p) = " << std::beta(p, q) << '\n' | ||
+ | << "π/sin(π*p) = " << π / std::sin(π * p) << '\n'; | ||
} | } | ||
|output= | |output= | ||
Line 87: | Line 110: | ||
8 28 56 70 56 28 8 | 8 28 56 70 56 28 8 | ||
9 36 84 126 126 84 36 9 | 9 36 84 126 126 84 36 9 | ||
+ | |||
+ | β(p,1-p) = 8.335989149587307836 | ||
+ | π/sin(π*p) = 8.335989149587307834 | ||
}} | }} | ||
Latest revision as of 23:35, 16 October 2023
Defined in header <cmath>
|
||
(1) | ||
float beta ( float x, float y ); double beta ( double x, double y ); |
(since C++17) (until C++23) |
|
/* floating-point-type */ beta( /* floating-point-type */ x, /* floating-point-type */ y ); |
(since C++23) | |
float betaf( float x, float y ); |
(2) | (since C++17) |
long double betal( long double x, long double y ); |
(3) | (since C++17) |
Defined in header <cmath>
|
||
template< class Arithmetic1, class Arithmetic2 > /* common-floating-point-type */ beta( Arithmetic1 x, Arithmetic2 y ); |
(A) | (since C++17) |
std::beta
for all cv-unqualified floating-point types as the type of the parameters x and y.(since C++23)Contents |
[edit] Parameters
x, y | - | floating-point or integer values |
[edit] Return value
If no errors occur, value of the beta function of x and y, that is ∫10tx-1(1-t)(y-1)dt, or, equivalently,Γ(x)Γ(y) |
Γ(x+y) |
[edit] Error handling
Errors may be reported as specified in math_errhandling.
- If any argument is NaN, NaN is returned and domain error is not reported.
- The function is only required to be defined where both x and y are greater than zero, and is allowed to report a domain error otherwise.
[edit] Notes
Implementations that do not support C++17, but support ISO 29124:2010, provide this function if __STDCPP_MATH_SPEC_FUNCS__
is defined by the implementation to a value at least 201003L and if the user defines __STDCPP_WANT_MATH_SPEC_FUNCS__
before including any standard library headers.
Implementations that do not support ISO 29124:2010 but support TR 19768:2007 (TR1), provide this function in the header tr1/cmath
and namespace std::tr1
.
An implementation of this function is also available in boost.math.
std::beta(x, y) equals std::beta(y, x).
When x and y are positive integers, std::beta(x, y) equals(x-1)!(y-1)! |
(x+y-1)! |
⎜
⎝n
k⎞
⎟
⎠=
1 |
(n+1)Β(n-k+1,k+1) |
The additional overloads are not required to be provided exactly as (A). They only need to be sufficient to ensure that for their first argument num1 and second argument num2:
|
(until C++23) |
If num1 and num2 have arithmetic types, then std::beta(num1, num2) has the same effect as std::beta(static_cast</* common-floating-point-type */>(num1), If no such floating-point type with the greatest rank and subrank exists, then overload resolution does not result in a usable candidate from the overloads provided. |
(since C++23) |
[edit] Example
#include <cassert> #include <cmath> #include <iomanip> #include <iostream> #include <numbers> #include <string> long binom_via_beta(int n, int k) { return std::lround(1 / ((n + 1) * std::beta(n - k + 1, k + 1))); } long binom_via_gamma(int n, int k) { return std::lround(std::tgamma(n + 1) / (std::tgamma(n - k + 1) * std::tgamma(k + 1))); } int main() { std::cout << "Pascal's triangle:\n"; for (int n = 1; n < 10; ++n) { std::cout << std::string(20 - n * 2, ' '); for (int k = 1; k < n; ++k) { std::cout << std::setw(3) << binom_via_beta(n, k) << ' '; assert(binom_via_beta(n, k) == binom_via_gamma(n, k)); } std::cout << '\n'; } // A spot-check const long double p = 0.123; // a random value in [0, 1] const long double q = 1 - p; const long double π = std::numbers::pi_v<long double>; std::cout << "\n\n" << std::setprecision(19) << "β(p,1-p) = " << std::beta(p, q) << '\n' << "π/sin(π*p) = " << π / std::sin(π * p) << '\n'; }
Output:
Pascal's triangle: 2 3 3 4 6 4 5 10 10 5 6 15 20 15 6 7 21 35 35 21 7 8 28 56 70 56 28 8 9 36 84 126 126 84 36 9 β(p,1-p) = 8.335989149587307836 π/sin(π*p) = 8.335989149587307834
[edit] See also
(C++11)(C++11)(C++11) |
gamma function (function) |
[edit] External links
Weisstein, Eric W. "Beta Function." From MathWorld — A Wolfram Web Resource. |