Python math.gamma() Method
The Python math.gamma() method calculates the gamma method, denoted as Γ. It is a mathematical extension of the factorial() method to non-integer values. The gamma method is defined for all complex numbers except non-positive integers.
Mathematically, the gamma method is defined as −
$$\mathrm{\Gamma(x)\:=\:\int_{0}^{∞}\:t^{x-1}e^{-t}dt}$$Where, e is the base of the natural logarithm. The gamma method has various properties, they are as follows −
- It is defined for all complex numbers x except non-positive integers (x ≤ 0).
- For positive integers, Γ(n) = (n-1)!, where n! denotes the factorial of n.
- It is a continuous and differentiable method.
- It satisfies the recurrence relation Γ(x + 1) = x.Γ(x) for all x > 0.
- It grows rapidly as x increases, and approaches infinity as x approaches zero from the right.
Syntax
Following is the basic syntax of the Python math.gamma() method −
math.gamma(x)
Parameters
This method accepts a real number or a numeric expression as a parameter for which you want to calculate the gamma method.
Return Value
The method returns the value of gamma method evaluated at x.
Example 1
In the following example, we are calculating the gamma method for a positive integer using the math.gamma() method −
import math
x = 5
result = math.gamma(x)
print("Gamma method for x =", x, ":", result)
Output
The output obtained is as follows −
Gamma method for x = 5 : 24.0
Example 2
In here, we are calculating the gamma method for a positive real number using the math.gamma() method −
import math
x = 2.5
result = math.gamma(x)
print("Gamma method for x =", x, ":", result)
Output
Following is the output of the above code −
Gamma method for x = 2.5 : 1.3293403881791372
Example 3
In this example, we are evaluating the product of gamma methods for x=3 and x + 1 using the math.gamma() method −
import math
x = 3
result = math.gamma(x) * math.gamma(x+1)
print("Expression result for x =", x, ":", result)
Output
We get the output as shown below −
Expression result for x = 3 : 12.0
Example 4
Now, we use the math.gamma() method to calculate the gamma method for a negative number −
import math
x = -3.5
result = math.gamma(x)
print("Gamma method for x =", x, ":", result)
Output
The result produced is as shown below −
Gamma method for x = -3.5 : 0.27008820585226917