Mathematical constants

Mpmath supports arbitrary-precision computation of various common (and less common) mathematical constants. These constants are implemented as lazy objects that can evaluate to any precision. Whenever the objects are used as function arguments or as operands in arithmetic operations, they automagically evaluate to the current working precision. A lazy number can be converted to a regular mpf using the unary + operator, or by calling it as a function:

>>> from mpmath import *
>>> mp.dps = 15
>>> pi
<pi: 3.14159~>
>>> 2*pi
mpf('6.2831853071795862')
>>> +pi
mpf('3.1415926535897931')
>>> pi()
mpf('3.1415926535897931')
>>> mp.dps = 40
>>> pi
<pi: 3.14159~>
>>> 2*pi
mpf('6.283185307179586476925286766559005768394338')
>>> +pi
mpf('3.141592653589793238462643383279502884197169')
>>> pi()
mpf('3.141592653589793238462643383279502884197169')

Exact constants

The predefined objects j (imaginary unit), inf (positive infinity) and nan (not-a-number) are shortcuts to mpc and mpf instances with these fixed values.

Pi (pi)

mpmath.mp.pi(prec=None, dps=None, rounding=None)

\(\pi\), roughly equal to 3.141592654, represents the area of the unit circle, the half-period of trigonometric functions, and many other things in mathematics.

Mpmath can evaluate \(\pi\) to arbitrary precision:

>>> from mpmath import *
>>> mp.dps = 50; mp.pretty = True
>>> +pi
3.1415926535897932384626433832795028841971693993751

This shows digits 99991-100000 of \(\pi\) (the last digit is actually a 4 when the decimal expansion is truncated, but here the nearest rounding is used):

>>> mp.dps = 100000
>>> str(pi)[-10:]
'5549362465'

Possible issues

pi always rounds to the nearest floating-point number when used. This means that exact mathematical identities involving \(\pi\) will generally not be preserved in floating-point arithmetic. In particular, multiples of pi (except for the trivial case 0*pi) are not the exact roots of sin(), but differ roughly by the current epsilon:

>>> mp.dps = 15
>>> sin(pi)
1.22464679914735e-16

One solution is to use the sinpi() function instead:

>>> sinpi(1)
0.0

See the documentation of trigonometric functions for additional details.

References

Degree (degree)

mp.degree = <1 deg = pi / 180: 0.0174533~>

Base of the natural logarithm (e)

mp.e = <e = exp(1): 2.71828~>

Golden ratio (phi)

mp.phi = <Golden ratio phi: 1.61803~>

Euler’s constant (euler)

mpmath.mp.euler(prec=None, dps=None, rounding=None)

Euler’s constant or the Euler-Mascheroni constant \(\gamma\) = 0.57721566… is a number of central importance to number theory and special functions. It is defined as the limit

\[\gamma = \lim_{n\to\infty} H_n - \log n\]

where \(H_n = 1 + \frac{1}{2} + \ldots + \frac{1}{n}\) is a harmonic number (see harmonic()).

Evaluation of \(\gamma\) is supported at arbitrary precision:

>>> from mpmath import *
>>> mp.dps = 50; mp.pretty = True
>>> +euler
0.57721566490153286060651209008240243104215933593992

We can also compute \(\gamma\) directly from the definition, although this is less efficient:

>>> limit(lambda n: harmonic(n)-log(n), inf)
0.57721566490153286060651209008240243104215933593992

This shows digits 9991-10000 of \(\gamma\) (the last digit is actually a 5 when the decimal expansion is truncated, but here the nearest rounding is used):

>>> mp.dps = 10000
>>> str(euler)[-10:]
'4679858166'

Integrals, series, and representations for \(\gamma\) in terms of special functions include the following (there are many others):

>>> mp.dps = 25
>>> -quad(lambda x: exp(-x)*log(x), [0,inf])
0.5772156649015328606065121
>>> quad(lambda x,y: (x-1)/(1-x*y)/log(x*y), [0,1], [0,1])
0.5772156649015328606065121
>>> nsum(lambda k: 1/k-log(1+1/k), [1,inf])
0.5772156649015328606065121
>>> nsum(lambda k: (-1)**k*zeta(k)/k, [2,inf])
0.5772156649015328606065121
>>> -diff(gamma, 1)
0.5772156649015328606065121
>>> limit(lambda x: 1/x-gamma(x), 0)
0.5772156649015328606065121
>>> limit(lambda x: zeta(x)-1/(x-1), 1)
0.5772156649015328606065121
>>> (log(2*pi*nprod(lambda n:
...     exp(-2+2/n)*(1+2/n)**n, [1,inf]))-3)/2
0.5772156649015328606065121

For generalizations of the identities \(\gamma = -\Gamma'(1)\) and \(\gamma = \lim_{x\to1} \zeta(x)-1/(x-1)\), see psi() and stieltjes() respectively.

References

Catalan’s constant (catalan)

mp.catalan = <Catalan's constant: 0.915966~>

Apery’s constant (apery)

mp.apery = <Apery's constant: 1.20206~>

Khinchin’s constant (khinchin)

mp.khinchin = <Khinchin's constant: 2.68545~>

Glaisher’s constant (glaisher)

mp.glaisher = <Glaisher's constant: 1.28243~>

Mertens constant (mertens)

mp.mertens = <Mertens' constant: 0.261497~>

Twin prime constant (twinprime)

mp.twinprime = <Twin prime constant: 0.660162~>