# Hydrogen calculations using `sympy`
The `sympy` library does symbolic math.  It also includes several useful things in the [`sympy.physics`](https://docs.sympy.org/latest/modules/physics/index.html) module, like hydrogen and harmonic oscillator wavefunctions.


```python
from sympy import *
from sympy.physics.hydrogen import *
```


```python
n = symbols("n")
ell = symbols("ℓ")
m = symbols("m")
```


```python
r, theta, phi = symbols("r θ ϕ", real=True)
```


```python
# Unperturbed energy of state n
E_nl(n)
```




$\displaystyle - \frac{1}{2 n^{2}}$




```python
# Wavefunciton of state |n,ℓ,m>
Psi_nlm(n, ell, m, r, phi, theta)
```




$\displaystyle \frac{\left(\frac{2 r}{n}\right)^{ℓ} \sqrt{\frac{\left(n - ℓ - 1\right)!}{n^{4} \left(n + ℓ\right)!}} \sqrt{\frac{2 ℓ \left(- m + ℓ\right)!}{\left(m + ℓ\right)!} + \frac{\left(- m + ℓ\right)!}{\left(m + ℓ\right)!}} e^{- \frac{r}{n}} e^{i m ϕ} L_{n - ℓ - 1}^{\left(2 ℓ + 1\right)}\left(\frac{2 r}{n}\right) P_{ℓ}^{\left(m\right)}\left(\cos{\left(θ \right)}\right)}{\sqrt{\pi}}$




```python
# Check a specfic state to compare
Psi_nlm(3, 1, 0, r, phi, theta)
```




$\displaystyle \frac{\sqrt{2} r \left(4 - \frac{2 r}{3}\right) e^{- \frac{r}{3}} \cos{\left(θ \right)}}{54 \sqrt{\pi}}$




```python
# define some aliases
z = r * cos(theta)
x = r * sin(theta)*cos(phi)
y = r * sin(theta)*sin(phi)
```

## Example integrals
Let's calculate
$$
\langle 2,1,0 \vert z \vert 1,0,0\rangle = \int\limits_{r=0}^{\infty} \int\limits_{\phi=0}^{2\pi} \int\limits_{\theta=0}^\pi
\phi^*_{2,1,0}\, z\, \phi_{1,0,0}\,r^2\sin\theta\,dr\,d\theta\,d\phi
$$


```python
# Set up some integrals
Integral(conjugate(Psi_nlm(2,1,0,r, phi, theta))*z*Psi_nlm(1,0,0,r,phi,theta)*r**2*sin(theta),(phi,0,2*pi),(theta,0,pi),(r,0,oo))
```




$\displaystyle \int\limits_{0}^{\infty}\int\limits_{0}^{\pi}\int\limits_{0}^{2 \pi} \frac{\sqrt{2} r^{4} e^{- \frac{3 r}{2}} \sin{\left(θ \right)} \cos^{2}{\left(θ \right)}}{8 \pi}\, dϕ\, dθ\, dr$




```python
# perform the integration
_.doit()
```




$\displaystyle \frac{128 \sqrt{2}}{243}$




```python
# approximate this in decimal form
N(_)
```




$\displaystyle 0.744935539027803$




```python
_*5.292e-11 # convert from atomic units to SI units
```




$\displaystyle 3.94219887253513 \cdot 10^{-11}$




```python
_**2
```




$\displaystyle 1.55409319506173 \cdot 10^{-21}$




```python
_21**2
```




$\displaystyle 0.554928957306644$




```python

```

5598ba98651c54ce8636ea07c8e1eda3d87392ac
ipynb
Jupyter Notebook
HydrogenSympy.ipynb
corcoted/Phys475
8fc0ee6bccda19e5afff14f0e8fda6aef7ee6d66
MIT
2021-03-10T04:30:46.000Z
2021-07-12T09:20:43.000Z
HydrogenSympy.ipynb
corcoted/Phys475
8fc0ee6bccda19e5afff14f0e8fda6aef7ee6d66
MIT
HydrogenSympy.ipynb
corcoted/Phys475
8fc0ee6bccda19e5afff14f0e8fda6aef7ee6d66
MIT
Qwen/Qwen-72B
1. YES
2. YES