Namespaces
Variants

std:: beta, std:: betaf, std:: betal

From cppreference.net
定义于头文件 <cmath>
(1)
float beta ( float x, float y ) ;

double beta ( double x, double y ) ;

long double beta ( long double x, long 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 起)
1-3) 计算 x y Beta 函数 库为所有 cv 未限定浮点类型提供了 std::beta 的重载,作为参数 x y 的类型。 (C++23 起)
A) 为所有其他算术类型组合提供了额外的重载。

目录

参数

x, y - 浮点数或整数值

返回值

If no errors occur, value of the beta function of x and y , that is 1
0
t x-1
(1-t) (y-1)
d t
, or, equivalently,
Γ(x)Γ(y)
Γ(x+y)
is returned.

错误处理

错误报告方式可按照 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)!
. Binomial coefficients can be expressed in terms of the beta function:

n
k


=
1
(n+1)Β(n-k+1,k+1)
.

额外的重载并不需要完全按照 (A) 的形式提供。它们只需确保对于第一个参数 num1 和第二个参数 num2 满足:

  • num1 num2 具有类型 long double ,则 std :: beta ( num1, num2 ) 的效果等同于 std :: beta ( static_cast < long double > ( num1 ) ,
    static_cast < long double > ( num2 ) )
  • 否则,若 num1 和/或 num2 具有类型 double 或整数类型,则 std :: beta ( num1, num2 ) 的效果等同于 std :: beta ( static_cast < double > ( num1 ) ,
    static_cast < double > ( num2 ) )
  • 否则,若 num1 num2 具有类型 float ,则 std :: beta ( num1, num2 ) 的效果等同于 std :: beta ( static_cast < float > ( num1 ) ,
    static_cast < float > ( num2 ) )
(C++23 前)

num1 num2 具有算术类型,则 std :: beta ( num1, num2 ) 的效果等同于 std :: beta ( static_cast < /* common-floating-point-type */ > ( num1 ) ,
static_cast < /* common-floating-point-type */ > ( num2 ) )
,其中 /* common-floating-point-type */ num1 num2 类型之间具有最高 浮点转换等级 和最高 浮点转换子等级 的浮点类型,整数类型的实参被认为具有与 double 相同的浮点转换等级。

若不存在具有最高等级和子等级的此类浮点类型,则 重载决议 不会从提供的重载中得到可用候选。

(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 网络资源。