Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

      From cppreference.com
      <cpp‎ |numeric‎ |special functions
       
       
       
       
      Defined in header<cmath>
      (1)
      float       beta(float x,float y);

      double      beta(double x,double y);

      longdouble beta(longdouble x,longdouble 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)
      longdouble betal(longdouble x,longdouble 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)
      1-3) Computes theBeta function ofx andy. The library provides overloads ofstd::beta for all cv-unqualified floating-point types as the type of the parametersx andy.(since C++23)
      A) Additional overloads are provided for all other combinations of arithmetic types.

      Contents

      [edit]Parameters

      x, y - floating-point or integer values

      [edit]Return value

      If no errors occur, value of the beta function ofx andy, that is\(\int_{0}^{1}{ {t}^{x-1}{(1-t)}^{y-1}\mathsf{d}t}\)1
      0
      tx-1
      (1-t)(y-1)
      dt
      , or, equivalently,\(\frac{\Gamma(x)\Gamma(y)}{\Gamma(x+y)}\)
      Γ(x)Γ(y)
      Γ(x+y)
      is returned.

      [edit]Error handling

      Errors may be reported as specified inmath_errhandling.

      • If any argument is NaN, NaN is returned and domain error is not reported.
      • The function is only required to be defined where bothx andy are greater than zero, and is allowed to report a domain error otherwise.

      [edit]Notes

      Implementations that do not support C++17, but supportISO 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 headertr1/cmath and namespacestd::tr1.

      An implementation of this function is alsoavailable in boost.math.

      std::beta(x, y) equalsstd::beta(y, x).

      Whenx andy are positive integers,std::beta(x, y) equals\(\frac{(x-1)!(y-1)!}{(x+y-1)!}\)
      (x-1)!(y-1)!
      (x+y-1)!
      .Binomial coefficients can be expressed in terms of the beta function:\(\binom{n}{k} = \frac{1}{(n+1)B(n-k+1,k+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 argumentnum1 and second argumentnum2:

      • Ifnum1 ornum2 has typelongdouble, thenstd::beta(num1, num2) has the same effect asstd::beta(static_cast<longdouble>(num1),
                 static_cast<longdouble>(num2))
        .
      • Otherwise, ifnum1 and/ornum2 has typedouble or an integer type, thenstd::beta(num1, num2) has the same effect asstd::beta(static_cast<double>(num1),
                 static_cast<double>(num2))
        .
      • Otherwise, ifnum1 ornum2 has typefloat, thenstd::beta(num1, num2) has the same effect asstd::beta(static_cast<float>(num1),
                 static_cast<float>(num2))
        .
      (until C++23)

      Ifnum1 andnum2 have arithmetic types, thenstd::beta(num1, num2) has the same effect asstd::beta(static_cast</* common-floating-point-type */>(num1),
               static_cast</* common-floating-point-type */>(num2))
      , where/* common-floating-point-type */ is the floating-point type with the greatestfloating-point conversion rank and greatestfloating-point conversion subrank between the types ofnum1 andnum2, arguments of integer type are considered to have the same floating-point conversion rank asdouble.

      If no such floating-point type with the greatest rank and subrank exists, thenoverload resolution does not result in a usable candidate from the overloads provided.

      (since C++23)

      [edit]Example

      Run this code
      #include <cassert>#include <cmath>#include <iomanip>#include <iostream>#include <numbers>#include <string> long binom_via_beta(int n,int k){returnstd::lround(1/((n+1)* std::beta(n- k+1, k+1)));} long binom_via_gamma(int n,int k){returnstd::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-checkconstlongdouble p=0.123;// a random value in [0, 1]constlongdouble q=1- p;constlongdouble π=std::numbers::pi_v<longdouble>;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]

      [edit]External links

      Weisstein, Eric W. "Beta Function." From MathWorld — A Wolfram Web Resource.
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/numeric/special_functions/beta&oldid=160789"

      [8]ページ先頭

      ©2009-2025 Movatter.jp