Python Program to calculate the logarithm gamma of the given number

PythonServer Side ProgrammingProgramming

In mathematics, the gamma function is said to be an extension to the factorial of any given number. However, as the factorial is only defined for real numbers, the gamma function exceeds to define the factorial on all complex numbers except the negative integers. It is represented by −

Γ(x) = (x-1)!

The logarithm gamma function comes into picture as the gamma function grows rapidly only larger numbers, so applying logarithm to the gamma will slow it down a lot. It is also known as the natural logarithm gamma of a given number.

log(Γ(x)) = log((x-1)!)

In python programming language, like few other programming languages, the logarithm gamma function is calculated using the math.lgamma() function. However, we will also look into several other ways to calculate the log gamma of a number in this article.

Input Output Scenarios

Let us look at some input output scenarios to find the log gamma function using the math.lgamma() method.

Assume that the input to the log gamma function is a positive integer −

Input: 12
Result: 17.502307845873887

Assume that the input to the log gamma function is a negative integer −

Input: -12
Result: “ValueError: math domain error”

Assume that the input to the log gamma function is a zero −

Input: 0
Result: “ValueError: math domain error”

Assume that the input to the log gamma function is a negative decimal value closer to zero −

Input: -0.2
Result: 1.761497590833938

The domain errors occur while using the lgamma() method as the function is defined for all complex numbers minus the negative “integers”. Let us look at various approaches to find the logarithm gamma of a given number.

Using math.lgamma() Function

The lgamma() method is defined the math library and returns the natural logarithm gamma value of a given number. The syntax of the method is −

math.lgamma(x)

where x is any complex number except negative integers.

Example

The python example to find the log gamma using the math.lgamma() function is as follows −

# import math library import math #log gamma of positive integer x1 = 10 print(math.lgamma(x1)) #log gamma of negative complex number x2 = -1.2 print(math.lgamma(x2)) #log gamma of a positive complex number x3 = 3.4 print(math.lgamma(x3))

Output

The output for the above python code is obtained as −

12.801827480081467
1.5791760340399836
1.0923280598027416

Using math.gamma() and math.log() Functions

In another approach, the logarithm gamma of a number can be found by first finding the gamma of the number using the math.gamma() function and later apply logarithm on the gamma value using the math.log() function. Here, we are just breaking the function of lgamma() into multiple steps.

Example

The python implementation for the said procedure is given below −

# import math library import math #log gamma of positive integer x1 = math.gamma(10) print(math.log(x1)) #log gamma of negative complex number x2 = math.gamma(-1.2) print(math.log(x2)) #log gamma of a positive complex number x3 = math.gamma(3.4) print(math.log(x3))

Output

The output is obtained is as −

12.801827480081469
1.5791760340399839
1.0923280598027414

By Applying Logarithm on Factorial of a Number

An even simpler method is to find the factorial of the given number, since gamma function is defined as the factorial of a complex number, and apply logarithm using the math.log() method on that factorial computed.

Example

In this python example, we are finding the logarithm gamma of a number using the factorial and math.log() method. The only drawback of using this approach is that it is only applicable for positive integers.

# import math library import math def factorial(n): if n == 1: return 1 else: return n*factorial(n-1) #log gamma of positive integer x1 = 10 y1 = factorial(x1-1) print(math.log(y1)) x2 = 3 y2 = factorial(x2-1) print(math.log(y2)) #log gamma of a positive complex number x3 = 3.4 y3 = factorial(x3-1) print(math.log(y3))

Output

The output is obtained as −

12.801827480081469
0.6931471805599453
RecursionError: maximum recursion depth exceeded in comparison
raja
Updated on 14-Oct-2022 08:30:21

Advertisements