名稱空間
變體
操作

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

來自 cppreference.com
< cpp‎ | 數值‎ | 特殊函式
 
 
 
 
定義於標頭檔案 <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)
/* floating-point-type */ beta( /* floating-point-type */ x,
                                /* floating-point-type */ 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 >
/* common-floating-point-type */ beta( Arithmetic1 x, Arithmetic2 y );
(A) (C++17 起)
1-3) 計算 xyBeta 函式庫為所有 cv-unqualified 浮點型別提供了 std::beta 的過載,作為引數 xy 的型別。(C++23 起)
A) 為所有其他算術型別組合提供了附加過載。

目錄

[編輯] 引數

x, y - 浮點數或整數值

[編輯] 返回值

如果沒有錯誤發生,則返回 xy 的 beta 函式值,即 1
0
tx-1
(1-t)(y-1)
dt
,或等價地,
Γ(x)Γ(y)
Γ(x+y)

[編輯] 錯誤處理

錯誤可能按 math_errhandling 中指定的方式報告。

  • 如果任何引數是 NaN,則返回 NaN,不報告域錯誤。
  • 該函式僅要求在 xy 都大於零時定義,否則允許報告域錯誤。

[編輯] 注意

不支援 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)

xy 為正整數時,std::beta(x, y) 等於
(x-1)!(y-1)!
(x+y-1)!
。二項式係數可以用 Beta 函式表示:

n
k


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

不需要完全按照 (A) 提供額外的過載。它們只需要足以確保對於它們的第一個引數 num1 和第二個引數 num2

  • 如果 num1num2 的型別為 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))
    相同。
  • 否則,如果 num1num2 的型別為 float,則 std::beta(num1, num2) 的效果與 std::beta(static_cast<float>(num1),
              static_cast<float>(num2))
    相同。
(直至 C++23)

如果 num1num2 具有算術型別,則 std::beta(num1, num2) 的效果與 std::beta(static_cast</* common-floating-point-type */>(num1),
          static_cast</* common-floating-point-type */>(num2))
相同,其中 /* common-floating-point-type */ 是在 num1num2 的型別之間,具有最大 浮點轉換等級 和最大 浮點轉換子等級 的浮點型別,整數型別引數被視為與 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 << "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';
}

輸出

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

[編輯] 參閱

(C++11)(C++11)(C++11)
伽馬函式
(函式) [編輯]

[編輯] 外部連結

Weisstein, Eric W. "Beta Function." From MathWorld — A Wolfram Web Resource.