# Chapter 10 - Position and Momentum
We can start using sympy to handle symbolic math (integrals and other calculus):


```python
from sympy import *
```


```python
init_printing(use_unicode=True)
```


```python
x, y, z = symbols('x y z', real=True)
a, c = symbols('a c', nonzero=True, real=True)
```


```python
integrate?
```

There are two ways to use the `integrate` function. In one line, like `integrate(x,(x,0,1))` or by naming an expression and then integrating it over a range:

```
A = (c*cos((pi*x)/(2.0*a)))**2
A.integrate((x,-a,a),conds='none')
```

We'll use both, at different times. For longer expressions, the second form can be easier to read and write.

First, just try the following, then we'll re-create some examples in the book.


```python
integrate(x,(x,0,1))
```


```python
integrate(x**2,(x,0,1))
```

The cell below will return an odd set of conditions on the result. This is because the solver doesn't want to assume anything about `a` and there is a special case where the answer would be different. If you look closely though, that special case isn't physically realistic so to igore these special conditions, we add `conds='none'`. The next cell down does what you'd expect. From here on out, just add this to the `integrate` function and we'll get what we expect.


```python
A = (c*cos((pi*x)/(2.0*a)))**2
A.integrate((x,-a,a))
```


```python
A = (c*cos((pi*x)/(2.0*a)))**2
A.integrate((x,-a,a), conds='none')
```

So this tells us the normalization constant should be $c=\frac{1}{\sqrt{a}}$. Check that it is normalized if we do that:


```python
psi = 1/sqrt(a)*cos((pi*x)/(2.0*a))  # notice we can name the expression something useful.
B = psi**2
B.integrate( (x,-a,a), conds='none')
```

Because `psi` is a real function, we can calculate expectation values by integrating over $x$ or $x^2$ with `psi**2`:


```python
C = x*psi**2
C.integrate( (x,-a,a), conds='none')
```


```python
D = x**2 * psi**2
E = D.integrate( (x,-a,a), conds='none')
```


```python
E.n()  # the .n() method approximates the numerical part. You can look at the full expression below.
```


```python
E
```

## Example 10.2


```python
h = Symbol('hbar', real=True)
```

Use the `diff` function to take a derivative of a symbolic expression. For example:


```python
diff(x**2, x)
```


```python
# Solution goes here
```


```python
# Solution goes here
```

## Example 10.3


```python
p = Symbol('p', real=True) 
```


```python
# Solution goes here
```


```python
# Solution goes here
```


```python

```

bb618fbbeab2fb6ad662766143a601decd274e98
ipynb
Jupyter Notebook
Chapter 10 - Position & Momentum_blank.ipynb
mniehus/QMlabs
bf9ba41bafdac7625ce1810d67d79072219b32b9
MIT
2016-08-21T16:59:18.000Z
2021-12-21T03:22:14.000Z
Chapter 10 - Position & Momentum_blank.ipynb
mniehus/QMlabs
bf9ba41bafdac7625ce1810d67d79072219b32b9
MIT
2020-04-23T16:37:40.000Z
2020-04-23T17:42:01.000Z
Chapter 10 - Position & Momentum_blank.ipynb
mniehus/QMlabs
bf9ba41bafdac7625ce1810d67d79072219b32b9
MIT
2015-09-27T04:35:02.000Z
2022-02-16T23:03:34.000Z
Qwen/Qwen-72B
1. YES
2. YES