Python math.lgamma() Method
The Python math.lgamma() method is used to calculate the natural logarithm of the absolute value of the gamma method, denoted as ln|Γ(x)|. This method allows for accurate calculation of the gamma method for large arguments without causing overflow or loss of precision.
Mathematically, the natural logarithm of the absolute value of the gamma method is defined as −
$$\mathrm{\ln|\Gamma(x)|\:=\:\ln|\int_{0}^{∞}\:t^{x-1}e^{-t}dt|}$$Where, e is the base of the natural logarithm.
Note: To use this function, you need to import math module.
Syntax
Following is the basic syntax of the Python math.lgamma() method −
math.lgamma(x)
Parameters
This method accepts a real number or a numeric expression as a parameter for which you want to calculate the natural logarithm of the absolute value of the gamma method.
Return Value
The method returns the natural logarithm of the absolute value of the gamma method evaluated at x.
Example 1
In the following example, we are calculating the natural logarithm of the absolute value of the gamma method for a positive integer using the math.lgamma() method −
import math
x = 5
result = math.lgamma(x)
print("The result obtained for x =", x, ":", result)
Output
The output obtained is as follows −
The result obtained for x = 5 : 3.178053830347945
Example 2
In here, we are calculating the natural logarithm of the absolute value of the gamma method for a positive real number using the math.lgamma() method −
import math
x = 2.5
result = math.lgamma(x)
print("The result obtained for x =", x, ":", result)
Output
Following is the output of the above code −
The result obtained for x = 2.5 : 0.2846828704729196
Example 3
In this example, we are evaluating the sum of the natural logarithm of the absolute value of the gamma method for x=3 and x + 1 using the math.lgamma() method −
import math
x = 3
result = math.lgamma(x) + math.lgamma(x+1)
print("Expression result for x =", x, ":", result)
Output
We get the output as shown below −
Expression result for x = 3 : 2.4849066497880004
Example 4
Now, we use the math.lgamma() method to calculate the natural logarithm of the absolute value of the gamma method for a negative number −
import math
x = -3.5
result = math.lgamma(x)
print("The result obtained for x =", x, ":", result)
Output
The result produced is as shown below −
The result obtained for x = -3.5 : -1.3090066849930417