Processing math: 100%

C++程序计算给定数字的对数伽马


在数学中,伽马函数被描述为对每个给定数字的阶乘的扩展。另一方面,阶乘只能为实数定义,因此伽马函数扩展到计算除负整数以外的所有复数上的阶乘。它由 - 表示

Γ(x)=(x1)!

对于较高的值,伽马函数增长很快;因此,对伽马函数应用对数将大大降低其增长速度。特定数字的自然对数伽马是它的另一个名称。

在本文中,我们将了解如何在 C++ 中计算给定输入数字 x 的伽马函数的对数。

使用 lgamma() 函数计算对数伽马

C++ 的 cmath 库有一个 lgamma() 函数,它接受一个参数 x,然后执行 gamma(x) 并对该值应用自然对数。使用 lgamma() 的语法如下所示:

语法

#include < cmath >
lgamma( <number> )

算法

  • 读取一个数字 x
  • res := 使用 lgamma( x ) 计算对数伽马
  • 返回 res

示例

Open Compiler
#include <iostream> #include <cmath> using namespace std; float solve( float x ){ float answer; answer = lgamma( x ); return answer; } int main(){ cout << "Logarithm Gamma for x = 10 is: " << solve( 10 ) << endl; cout << "Logarithm Gamma for 15! which is x = 16 is: " << solve( 16 ) << endl; cout << "Logarithm Gamma for x = -1.2 is: " << solve( -1.2 ) << endl; cout << "Logarithm Gamma for x = 3.1415 is: " << solve( 3.1415 ) << endl; }

输出

Logarithm Gamma for x = 10 is: 12.8018
Logarithm Gamma for 15! which is x = 16 is: 27.8993
Logarithm Gamma for x = -1.2 is: 1.57918
Logarithm Gamma for x = 3.1415 is: 0.827604

使用 gamma() 和 log() 函数

C++ 还提供用于伽马的 tgamma() 方法和 log() 函数。我们可以使用它们来制定 lgamma()。让我们看看算法以获得更清晰的认识。

算法

  • 读取一个数字 x
  • g := 使用 tgamma( x ) 计算伽马
  • res := 使用 log( g ) 计算对数伽马
  • 返回 res

示例

Open Compiler
#include <iostream> #include <cmath> using namespace std; float solve( float x ){ float answer; float g = tgamma( x ); answer = log( g ); return answer; } int main(){ cout << "Logarithm Gamma for x = 10 is: " << solve( 10 ) << endl; cout << "Logarithm Gamma for 15! which is x = 16 is: " << solve( 16 ) << endl; cout << "Logarithm Gamma for x = -1.2 is: " << solve( -1.2 ) << endl; cout << "Logarithm Gamma for x = 3.1415 is: " << solve( 3.1415 ) << endl; }

输出

Logarithm Gamma for x = 10 is: 12.8018
Logarithm Gamma for 15! which is x = 16 is: 27.8993
Logarithm Gamma for x = -1.2 is: 1.57918
Logarithm Gamma for x = 3.1415 is: 0.827604

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

使用 factorial() 和 log() 函数

在最后一个示例中,我们已经看到了 tgamma() 和 log() 方法的使用。我们可以定义我们自己的 factorial() 函数,但这只接受正数。让我们看看算法以更好地理解。

算法

  • 定义阶乘函数,它将接收 n

  • 如果 n 为 1,则

    • 返回 n

  • 否则

    • 返回 n * factorial ( n - 1 )

  • 结束 if

  • 在主方法中,取一个数字 x 以找到 x 的对数伽马

  • g := factorial( x - 1)

  • res := 使用 log( g ) 查找 g 的自然对数

  • 返回 res

示例

Open Compiler
#include <iostream> #include <cmath> using namespace std; long fact( int n ){ if( n == 1 ) { return n; } else { return n * fact( n - 1); } } float solve( float x ){ float answer; float g = fact( x - 1 ); answer = log( g ); return answer; } int main(){ cout << "Logarithm Gamma for x = 10 is: " << solve( 10 ) << endl; cout << "Logarithm Gamma for 15! which is x = 16 is: " << solve( 16 ) << endl; cout << "Logarithm Gamma for x = -1.2 is: " << solve( -1.2 ) << endl; }

输出

Logarithm Gamma for x = 10 is: 12.8018
Logarithm Gamma for 15! which is x = 16 is: 27.8993
Segmentation fault (core dumped)

结论

伽马方法有时被称为阶乘方法的扩展。由于伽马或阶乘方法增长得如此之快,我们可以对其使用对数。在本文中,我们已经看到了几种对给定数字 x 执行对数伽马的技术。最初,我们使用了默认函数,即来自 C++ 中 cmath 库的 lgamma()。第二种方法是使用 tgamma() 和 log(),最后定义我们自己的阶乘方法。但是,最后一种方法仅限于正数。它不适用于负数。并且它只对整数执行良好。

更新于: 2022年12月7日

378 次浏览

开启你的 职业生涯

通过完成课程获得认证

开始
广告