# Symbolic Computation
Symbolic computation deals with symbols, representing them exactly, instead of numerical approximations (floating point)


```python
import math

math.sqrt(3)
```




    1.7320508075688772




```python
math.sqrt(8)
```




    2.8284271247461903



$\sqrt(8) = 2\sqrt(2)$, but it's hard to see that here


```python
import sympy
sympy.sqrt(3)
```




$\displaystyle \sqrt{3}$



Sympy can even simplify symbolic computations


```python
sympy.sqrt(8)
```




$\displaystyle 2 \sqrt{2}$




```python
from sympy import symbols
x, y = symbols('x y')
expr = x + 2*y
expr

```




$\displaystyle x + 2 y$



Note that simply adding two symbols creates an expression. Now let's play around with it. 


```python
expr + 1
```




$\displaystyle x + 2 y + 1$




```python
expr - x
```




$\displaystyle 2 y$



Note that `expr - x` was not `x + 2y -x`


```python
x*expr
```




$\displaystyle x \left(x + 2 y\right)$




```python
from sympy import expand, factor
expanded_expr = expand(x*expr)
expanded_expr
```




$\displaystyle x^{2} + 2 x y$




```python
factor(expanded_expr)
```




$\displaystyle x \left(x + 2 y\right)$




```python
from sympy import diff, sin, exp

diff(sin(x)*exp(x), x)
```




$\displaystyle e^{x} \sin{\left(x \right)} + e^{x} \cos{\left(x \right)}$




```python
from sympy import limit

limit(sin(x)/x, x, 0)
```




$\displaystyle 1$



### Exercise

Solve $x^2 - 2 = 0$ using sympy.solve


```python
# Type solution here
from sympy import solve
```

## Pretty printing


```python
from sympy import init_printing, Integral, sqrt

init_printing(use_latex=True)
```


```python
Integral(sqrt(1/x), x)
```


```python
from sympy import latex

latex(Integral(sqrt(1/x), x))
```




    '\\int \\sqrt{\\frac{1}{x}}\\, dx'



More symbols


```python
expr2 = x + 2*y +3*z
```

### Exercise 

Solve $x + 2*y + 3*z$ for $x$


```python
# Solution here
solve()
```

Difference between symbol name and python variable name


```python
x, y = symbols("y z")
```


```python
x
```


```python
y
```


```python
z
```

Symbol names can be more than one character long


```python
crazy = symbols('unrelated')

crazy + 1
```


```python
x = symbols("x")
expr = x + 1
x = 2
```

What happens when I print expr now? Does it print 3?


```python
print(expr)
```

    x + 1


How do we get 3?


```python
x = symbols("x")
expr = x + 1
expr.subs(x, 2)
```

## Equalities


```python
x + 1 == 4
```




    False




```python
from sympy import Eq

Eq(x + 1, 4)
```

Suppose we want to ask whether $(x + 1)^2 = x^2 + 2x + 1$


```python
(x + 1)**2 == x**2 + 2*x + 1
```




    False




```python
from sympy import simplify

a = (x + 1)**2
b = x**2 + 2*x + 1

simplify(a-b)
```

### Exercise 
Write a function that takes two expressions as input, and returns a tuple of two booleans. The first if they are equal symbolically, and the second if they are equal mathematically.

## More operations


```python
z = symbols("z")
expr = x**3 + 4*x*y - z
expr.subs([(x, 2), (y, 4), (z, 0)])
```


```python
from sympy import sympify

str_expr = "x**2 + 3*x - 1/2"
expr = sympify(str_expr)
expr
```


```python
expr.subs(x, 2)
```


```python
expr = sqrt(8)
```


```python
expr
```


```python
expr.evalf()
```


```python
from sympy import pi

pi.evalf(100)
```


```python
from sympy import cos

expr = cos(2*x)
expr.evalf(subs={x: 2.4})
```

### Exercise



```python
from IPython.core.display import Image 
Image(filename='figures/comic.png') 
```

Write a function that takes a symbolic expression (like pi), and determines the first place where 999999 appears.
Tip: Use the string representation of the number. Python starts counting at 0, but the decimal point offsets this

## Solving an ODE


```python
from sympy import Function

f, g = symbols('f g', cls=Function)
f(x)
```


```python
f(x).diff()
```


```python
diffeq = Eq(f(x).diff(x, x) - 2*f(x).diff(x) + f(x), sin(x))
diffeq
```


```python
from sympy import dsolve

dsolve(diffeq, f(x))
```

## Finite Differences


```python
from sympy import as_finite_diff

f = Function('f')
dfdx = f(x).diff(x)
as_finite_diff(dfdx)
```


```python
from sympy import Symbol

d2fdx2 = f(x).diff(x, 2)
h = Symbol('h')
as_finite_diff(d2fdx2, h)
```


```python

```

cac19828a646146ace3097ff3e8f3e0ec53415b2
ipynb
Jupyter Notebook
01-sympy.ipynb
navjotk/devitoworkshop
ebb5dcd40ba32caf2be520bfc420251c32ad2079
MIT
01-sympy.ipynb
navjotk/devitoworkshop
ebb5dcd40ba32caf2be520bfc420251c32ad2079
MIT
01-sympy.ipynb
navjotk/devitoworkshop
ebb5dcd40ba32caf2be520bfc420251c32ad2079
MIT
Qwen/Qwen-72B
1. YES
2. YES