Special Functions



erf() Function

The erf() function in math module returns the error function at the given parameter x. n mathematics, the error function (also called the Gauss error function), often denoted by erf, is a complex function of a complex variable. If the function argument is real, then the function value is also real.

Syntax

math.erf(x)

Parameters

  • x − a number, whose error function we are interested in finding the error function at.

Return Value

The erf() function returns a float, representing the error function at x.

Example

from math import erf, pi

x = 1
val = erf(x)
print ("x: ",x, "erf(x): ", val)

x = pi
val = erf(x)
print ("x: ",x, "erf(x): ", val)

x = -12.23
val = erf(x)
print ("x: ",x, "erf(x): ", val)

x = 0
val = erf(x)
print ("x: ",x, "erf(x): ", val)

It will produce the following output

x: 1 erf(x): 0.8427007929497149
x: 3.141592653589793 erf(x): 0.9999911238536323
x: -12.23 erf(x): -1.0
x: 0 erf(x): 0.0

erfc() Function

The erfc() function in math module stands for complementary error function. Value of erf(x) is equivalent to 1-erf(x). This function is used for large values, where subtraction from 1 reults in loss of significance.

Syntax

math.erfc(x)

Parameters

  • x − any number between -Inf to Inf.

Return Value

The erfc() function results a float representing complementary error function of x, and is between 0 to 2.

Example

from math import erfc, pi

x = 1
val = erfc(x)
print ("x: ",x, "erfc(x): ", val)

x = pi
val = erfc(x)
print ("x: ",x, "erfc(x): ", val)

x = -12.23
val = erfc(x)
print ("x: ",x, "erfc(x): ", val)

x = 0
val = erfc(x)
print ("x: ",x, "erfc(x): ", val)

It will produce the following output

x: 1 erfc(x): 0.1572992070502851
x: 3.141592653589793 erfc(x): 8.876146367641612e-06
x: -12.23 erfc(x): 2.0
x: 0 erfc(x): 1.0

gamma() Function

In mathematics, the gamma function (represented by Γ, the capital letter gamma from the Greek alphabet) is one commonly used extension of the factorial function to complex numbers. The gamma function is defined for all complex numbers except the non-positive integers. The mathematical notation for gamma function is −

$$\Gamma \left ( n \right )=\left ( n-1 \right )!$$

Syntax

math.gamma(x)

Parameters

  • x − a number to find the gamma function. Should be >=1.

Return Value

The gamma() function returns a float value. The gamma value is equal to factorial(x-1).

Example

from math import gamma, pi

x = 6
val = gamma(x)
print ("x: ",x, "gamma(x): ", val)

x = 2
val = gamma(x)
print ("x: ",x, "gamma(x): ", val)

x = 1
val = gamma(x)
print ("x: ",x, "gamma(x): ", val)

x = 0
val = gamma(x)
print ("x: ",x, "gamma(x): ", val)

It will produce the following output

x: 12 gamma(x): 120.0
x: 2 gamma(x): 1.0
x: 1 gamma(x): 1.0
Traceback (most recent call last):
   File "C:\Users\mlath\examples\main.py", line 17, in <module>
      val = gamma(x)
            ^^^^^^^^
ValueError: math domain error

lgamma() Function

The lgamma() function returns the natural logarithm of the absolute value of the Gamma function at x.

Syntax

math.lgamma(x)

Parameters

  • x − a float value for lgamma calculation.

Return Value

The lgamma() function returns a float value.

Example

from math import gamma, lgamma, log

x = 6
val = lgamma(x)
print ("x: ",x, "lgamma(x): ", val)
print ("Cross check")
y = log(gamma(x))
print ("lgamma(x) = log(gamma(x))", y )

x = 2
val = lgamma(x)
print ("x: ",x, "lgamma(x): ", val)

x = 1
val = lgamma(x)
print ("x: ",x, "lgamma(x): ", val)

It will produce the following output

x: 6 lgamma(x): 4.787491742782047
Cross check
lgamma(x) = log(gamma(x)) 4.787491742782046
x: 2 lgamma(x): 0.0
x: 1 lgamma(x): 0.0
python_maths.htm
Advertisements