Open In App

C++ Program to Calculate Logarithmic Gamma of a Number

Last Updated : 21 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Gamma Function in C++ is described as the factorial for complex and real numbers. This function is used to calculate the factorial on all complex values except for the negative values.

Representation of gamma function:

Γ(x)=(x−1)!

It is one of the best practices to add a logarithm to the gamma function since the growth rate of the gamma function with respect to the higher values is very high.

Logarithm Gamma

The logarithm of the gamma function, denoted by ln(Γ(x)), is a function that extends the concept of the logarithm to non-integer values of x. It is defined as the logarithm of the integral:

ln(Γ(x)) = ln(∫t^(x-1)e^(-t)dt)

Where t is a real number. The logarithm of the gamma function has many important properties and applications in mathematics and physics.

In probability theory, the logarithm of the gamma function is used to calculate the logarithm of the probability density function of the gamma distribution, which is a continuous probability distribution used to model the time between events in a Poisson process.

In mathematical analysis, the logarithm of the gamma function is used to calculate the logarithm of the beta function, which is a special function used in the calculation of definite integrals.

The lgamma() function in C++ is defined in the header cmath library in C++ . This function is used to apply the natural logarithm to the gamma function.

Example 1:

C++




// C++ Program Using lgamma() function
#include <iostream>
#include<cmath>
using namespace std;
 
// using lgamma() function
float logarithm_gamma(float number)
{
    float result;
    result = lgamma(number);
      return result;
}
 
int main()
{
      // lgamma for positive integer valued number
     float answer = logarithm_gamma(10);
    cout<<answer<<endl;
     
    // lgamma for negative valued number
    answer = logarithm_gamma(-1.7);
    cout<<answer<<endl;
     
    // lgamma for positive valued number
    answer = logarithm_gamma(3.14);
    cout<<answer<<endl;
}


Output:

12.8018
0.921845
0.826139

Example 2:

C++




// C++ Program Using tgamma() and log() function
#include <cmath>
#include <iostream>
using namespace std;
 
// using tgamma() and log() functions
float logarithm_gamma(float number)
{
    float result;
    float gamma = tgamma(number);
    result = log(gamma);
    return result;
}
 
int main()
{
    // lgamma for positive integer valued number
    float answer = logarithm_gamma(10);
    cout << answer << endl;
 
    // lgamma for negative valued number
    answer = logarithm_gamma(-1.7);
    cout << answer << endl;
 
    // lgamma for positive valued number
    answer = logarithm_gamma(3.14);
    cout << answer << endl;
}


Output:

12.8018
0.921845
0.826139

Example 3:

C++




// C++ Program Using Factorial and log() function
#include <cmath>
#include <iostream>
#include <limits>
 
double log_factorial(int n)
{
    // Calculate the logarithm of the factorial function
    double result = 0;
    for (int i = 2; i <= n; i++) {
        result += log(i);
    }
    return result;
}
 
double log_gamma(double x)
{
    // Special cases
    if (x == 0)
        return std::numeric_limits<double>::infinity();
    if (x == 1)
        return 0;
 
    // Use the factorial function to calculate the gamma
    // function for x > 1
    if (x > 1) {
        return log_factorial(x - 1);
    }
 
    // Use the reflection formula for x < 1
    return log_gamma(1 - x) - log(x) - M_PI / tan(M_PI * x);
}
 
int main()
{
    double x = 10;
    std::cout << "logarithmic gamma of " << x << " = "
              << log_gamma(x) << std::endl;
    return 0;
}


Output

logarithmic gamma of 10 = 12.8018

Time complexity: O(log n)

Auxiliary space: O(1).



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads