Python math.lgamma() Method

Vaibhav Vaibhav Jan 30, 2023
  1. Syntax
  2. Example 1: Use math.lgamma() to Get the Natural Logarithm Gamma of a Number
  3. Example 2: Returns a ValueError for a Zero Value
  4. Example 3: Returns a ValueError for a Negative Integer Value
  5. Example 4: Returns a TypeError for a String Value
Python math.lgamma() Method

In Mathematics, the Gamma function extends the factorial function to complex numbers (a + bi) and non-negative integers. It is represented by the Greek alphabet Γ.

Gamma function

A natural logarithm refers to a logarithm at the base of a number’s mathematical constant e. Here, e is equal to 2.718281828459.

Natural logarithm

Python contains a built-in module, math, that enables us to easily perform a torrent of mathematical operations. It supports various mathematical operations and domains such as trigonometry and logarithms.

This module has procedures to perform the gamma function and natural logarithm, namely, math.gamma(x) and math.log(x).

The natural logarithm gamma value for a non-negative number is an amalgamation of two functions we talked about. We can perform this operation using the math.lgamma().

Natural logarithm Gamma function

Syntax

math.lgamma(x)

Parameters

Type Description
x Float A non-negative integer and non-zero value. Note that the value can be negative, but it should not be an integer.

Return

The lgamma() method returns the natural logarithm for an absolute result of the gamma operation over a value.

Example 1: Use math.lgamma() to Get the Natural Logarithm Gamma of a Number

import math

print(math.lgamma(0.1))
print(math.lgamma(-0.1))
print(math.lgamma(-44.234))
print(math.lgamma(4800.345))
print(math.lgamma(0.0000001))
print(math.lgamma(1))
print(math.lgamma(-1.0000001))

Output:

2.2527126517342055
2.3689613327287886
-124.66184995040256
35886.18683992019
16.11809559323676
0.0
16.11809560809603

The Python code above computes the natural logarithm gamma value for 0.1, -0.1, -44.234, 4800.345, 0.0000001, 1, and -1.0000001. Note that these values are non-negative integers and non-zero.

If an out-of-domain value is offered to the lgamma() method, it raises a ValueError exception. Additionally, if the input is not a number, it raises a TypeError exception.

Example 2: Returns a ValueError for a Zero Value

import math

print(math.lgamma(0))

Output:

Traceback (most recent call last):
  File "main.py", line 3, in <module>
    print(math.lgamma(0))
ValueError: math domain error

Example 3: Returns a ValueError for a Negative Integer Value

import math

print(math.lgamma(-343))

Output:

Traceback (most recent call last):
  File "main.py", line 3, in <module>
    print(math.lgamma(-343))
ValueError: math domain error

The Python codes above show that the input for the lgamma() method can’t be a negative integer or a zero; hence, the method raises ValueError exceptions.

Example 4: Returns a TypeError for a String Value

import math

print(math.lgamma("this is a string!"))

Output:

Traceback (most recent call last):
  File "main.py", line 3, in <module>
    print(math.lgamma("this is a string!"))
TypeError: must be real number, not str

The Python code above takes a string as input; hence, the lgamma() method raises a TypeError exception.

Vaibhav Vaibhav avatar Vaibhav Vaibhav avatar

Vaibhav is an artificial intelligence and cloud computing stan. He likes to build end-to-end full-stack web and mobile applications. Besides computer science and technology, he loves playing cricket and badminton, going on bike rides, and doodling.

Related Article - Python Math