# sympy

Since we want to work interactively with `sympy`, we will import the complete module. Note that this polutes the namespace, and is not recommended in general.


```python
from sympy import *
```

Enable pretty printing in this notebook.


```python
init_printing()
```

## Expression manipulation

Define a number of symbols to work with, as well as an example expression.


```python
x, y, a, b, c = symbols('x y a b c')
```


```python
expr = (a*x**2 - b*y**2 + 5)/(c*x + y)
```

Check the expression's type.


```python
expr.func
```




    sympy.core.mul.Mul



Although the expression was defined as a divisino, it is represented as a multiplicatino by `sympy`.  The `args` attribute of an expressions stores the operands of the top-level operator.


```python
expr.args
```

Although the first factor appears to be a division, it is in fact a power.  The denominator of this expression would be given by:


```python
expr.args[0].func
```




    sympy.core.power.Pow




```python
expr.args[0].args[0]
```

The expression $\frac{1}{a x + b}$ can alternatively be defined as follows, which highlights the internal representation of expressions.


```python
expr = Pow(Add(Mul(a, x), b), -1)
```


```python
pprint(expr)
```

       1   
    ───────
    a⋅x + b



```python
expr.args
```




    (a*x + b, -1)




```python
expr.args[0].args[0]
```




    b




```python
expr = x**2 + 2*a*x + y**2
```


```python
expr2 = expr.subs(a, y)
expr2
```

Most expression manipulation algorithms can be called as functions, or as methods on expressions.


```python
factor(expr2)
```


```python
expr2.factor()
```


```python
x, y = symbols('x y', positive=True)
```


```python
(log(x) + log(y)).simplify()
```

## Calculus

### Series expansion


```python
x, a = symbols('x a')
```


```python
expr = sin(a*x)/x
```


```python
expr2 = series(expr, x, 0, n=7)
```


```python
expr2
```

A term of a specific order in a given variable can be selected easily.


```python
expr2.taylor_term(2, x)
```

When the order is unimportant, or when the expression should be used to define a function, the order term can be removed.


```python
expr2.removeO()
```

Adding two series deals with the order correctly.


```python
s1 = series(sin(x), x, 0, n=7)
```


```python
s2 = series(cos(x), x, 0, n=4)
```


```python
s1 + s2
```

### Derivatives and integrals


```python
expr = a*x**2 + b*x + c
```


```python
expr.diff(x)
```


```python
expr.integrate(x)
```

bc4dddf0cb38ea390f1d59cd6ece1635e1686d1e
ipynb
Jupyter Notebook
Python/Sympy/sympy.ipynb
Gjacquenot/training-material
16b29962bf5683f97a1072d961dd9f31e7468b8d
CC-BY-4.0
2015-03-23T13:34:42.000Z
2022-03-21T00:27:21.000Z
Python/Sympy/sympy.ipynb
Gjacquenot/training-material
16b29962bf5683f97a1072d961dd9f31e7468b8d
CC-BY-4.0
2015-02-25T15:04:26.000Z
2022-01-03T07:42:48.000Z
Python/Sympy/sympy.ipynb
Gjacquenot/training-material
16b29962bf5683f97a1072d961dd9f31e7468b8d
CC-BY-4.0
2015-11-26T11:44:51.000Z
2022-03-21T00:27:22.000Z
Qwen/Qwen-72B
1. YES
2. YES