std:: beta, std:: betaf, std:: betal
|
定义于头文件
<cmath>
|
||
| (1) | ||
|
float
beta
(
float
x,
float
y
)
;
double
beta
(
double
x,
double
y
)
;
|
(C++17 起)
(C++23 前) |
|
|
/* 浮点类型 */
beta
(
/* 浮点类型 */
x,
/* 浮点类型 */ y ) ; |
(C++23 起) | |
|
float
betaf
(
float
x,
float
y
)
;
|
(2) | (C++17 起) |
|
long
double
betal
(
long
double
x,
long
double
y
)
;
|
(3) | (C++17 起) |
|
定义于头文件
<cmath>
|
||
|
template
<
class
Arithmetic1,
class
Arithmetic2
>
/* 公共浮点类型 */ beta ( Arithmetic1 x, Arithmetic2 y ) ; |
(A) | (C++17 起) |
目录 |
参数
| x, y | - | 浮点数或整数值 |
返回值
If no errors occur, value of the beta function of x and y , that is ∫ 10 t x-1
(1-t) (y-1)
d t , or, equivalently,
| Γ(x)Γ(y) |
| Γ(x+y) |
错误处理
错误报告方式可按照 math_errhandling 中的规定执行。
- 若任一参数为 NaN,则返回 NaN 且不报告定义域错误。
- 本函数仅要求在 x 与 y 均大于零时有定义,否则允许报告定义域错误。
注释
不支持C++17但支持
ISO 29124:2010
的实现,若实现将
__STDCPP_MATH_SPEC_FUNCS__
定义为至少201003L的值,且用户在包含任何标准库头文件之前定义了
__STDCPP_WANT_MATH_SPEC_FUNCS__
,则提供此函数。
不支持 ISO 29124:2010 但支持 TR 19768:2007 (TR1) 的实现,会在头文件
tr1/cmath
和命名空间
std::tr1
中提供此函数。
此函数的实现也可在 boost.math库 中找到。
std :: beta ( x, y ) 等于 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) |
额外的重载并不需要完全按照 (A) 的形式提供。它们只需确保对于第一个参数 num1 和第二个参数 num2 满足:
|
(C++23 前) |
|
若
num1
和
num2
具有算术类型,则
std
::
beta
(
num1, num2
)
的效果等同于
std
::
beta
(
static_cast
<
/* common-floating-point-type */
>
(
num1
)
,
若不存在具有最高等级和子等级的此类浮点类型,则 重载决议 不会从提供的重载中得到可用候选。 |
(C++23 起) |
示例
#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 << "帕斯卡三角形:\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'; } // 抽样验证 const long double p = 0.123; // [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'; }
输出:
帕斯卡三角形:
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
参见
|
(C++11)
(C++11)
(C++11)
|
gamma 函数
(函数) |
外部链接
| 韦斯坦,埃里克·W。《Beta Function》。 摘自 MathWorld —— 一个 Wolfram 网络资源。 |