## Math constants - Irrational Genius Numbers

https://docs.sympy.org/0.7.1/modules/mpmath/functions/constants.html


```python
#from sympy import *
#init_printing()

# import mpmath as mp
from mpmath import *
```


```python
mp.dps = 10
```

### Pi


```python
pi
```




    <pi: 3.14159~>




```python
mp.pi
```




    <pi: 3.14159~>




```python
v = mp.pi()
```


```python
type(v)
```




    mpmath.ctx_mp_python.mpf




```python
v.real
```




    mpf('3.141592653585')




```python
mp.dps = 15
mp.pi()
```




    mpf('3.1415926535897931')




```python
print(+mp.pi)
```

    3.14159265358979



```python
float(mp.pi())
```




    3.141592653589793




```python
mp.dps = 50; mp.pretty = True
```


```python
+pi
```




    3.1415926535897932384626433832795028841971693993751




```python
mp.dps = 100000
str(pi)[:100]
```




    '3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706'




```python
mp.dps = 10
sin(pi)
```




    5.126688303e-12




```python
sinpi(1)
```




    0.0



### Base of the natural logarithm 


```python
+e
```




    2.718281828



Use `exp(x)` instead; this is both faster and more accurate


```python
exp(1.0)
```




    2.718281828



### Golden ratio (phi)

Ratio of two neighboring Fibonacci numbers

$ \phi = \frac{1+\sqrt{5}}{2} $


```python
mp.dps = 20; mp.pretty = True
```


```python
+phi
```




    1.6180339887498948482




```python
(1+sqrt(5))/2
```




    1.6180339887498948482




```python
limit(lambda n: fib(n+1)/fib(n), inf)
```




    1.6180339887498948482



### Euler's constant

$\gamma = 0.577 $


```python
mp.dps = 50; mp.pretty = True
+euler
```




    0.57721566490153286060651209008240243104215933593992




```python
# compute from definition
limit(lambda n: harmonic(n)-log(n), inf)
```




    0.57721566490153286060651209008240243104215933593992




```python
γ = lambda n: harmonic(n)-log(n)
```


```python
γ(1000)     # converge very slowly
```




    0.57771558156820786060254384028080755371837470382248



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


```python
-quad(lambda x: exp(-x)*log(x), [0,inf])
```




    0.57721566490153286060651209008240243104215933593992




```python
nsum(lambda k: 1/k-log(1+1/k), [1,inf])
```




    0.57721566490153286060651209008240243104215933593992




```python
nsum(lambda k: (-1)**k*zeta(k)/k, [2,inf])
```




    0.57721566490153286060651209008240243104215933593992




```python
-diff(gamma, 1)
```




    0.57721566490153286060651209008240243104215933593992




```python
limit(lambda x: 1/x-gamma(x), 0)
```




    0.57721566490153286060651209008240243104215933593992




```python
limit(lambda x: zeta(x)-1/(x-1), 1)
```




    0.57721566490153286060651209008240243104215933593992



### Apery’s constant (apery)

an irrational number approximately equal to 1.2020569

$$ \zeta(3) = \sum_{k=1}^{\infty} \frac{1}{k^3} $$

see https://www.overleaf.com/learn/latex/Integrals,_sums_and_limits for type-setting summation


```python
+apery
```




    1.2020569031595942853997381615114499907649862923405




```python
zeta(3)
```




    1.2020569031595942853997381615114499907649862923405




```python
-psi(2,1)/2
```




    1.2020569031595942853997381615114499907649862923405




```python
8*nsum(lambda k: 1/(2*k+1)**3, [0,inf])/7
```




    1.2020569031595942853997381615114499907649862923405




```python
f = lambda k: 2/k**3/(exp(2*pi*k)-1)
7*pi**3/180 - nsum(f, [1,inf])
```




    1.2020569031595942853997381615114499907649862923405



### Twin prime constant (twinprime)

http://mathworld.wolfram.com/TwinPrimesConstant.html

Represents the twin prime constant, which is the factor C2 featuring in the Hardy-Littlewood conjecture for the growth of the twin prime counting function

$$ \pi_2(n) \approx 2C_2 \frac{n}{log^2 n}$$

It is given by the product over primes

$$ C_2 = \prod_{p=3}^{\infty} \frac{p(p-2)}{(p-1)^2} \approx 0.66016 $$


```python
twinprime()
```




    0.66016181584686957392781211001455577843262336028473




```python
+twinprime
```




    0.66016181584686957392781211001455577843262336028473




```python

```

c934b9db5d184c380803b5d6d764c32afded7a6d
ipynb
Jupyter Notebook
chapter15_symbolic/00_math_constants.ipynb
wgong/cookbook-2nd-code
8ca2e5b3c90fee6605f4155e6b9dfb783ce46807
MIT
chapter15_symbolic/00_math_constants.ipynb
wgong/cookbook-2nd-code
8ca2e5b3c90fee6605f4155e6b9dfb783ce46807
MIT
chapter15_symbolic/00_math_constants.ipynb
wgong/cookbook-2nd-code
8ca2e5b3c90fee6605f4155e6b9dfb783ce46807
MIT
Qwen/Qwen-72B
1. YES
2. YES