# 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
```


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

This may be a bit surprising when you look at the mathematical representation of the expression, but the order of the terms is different from its rendering on the screen.


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

Since the addition operation is commutative, this makes no difference mathematically.


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


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


```python
expr2 = expr.subs(y, a)
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)
```

636c1cc737cf7039e87d6460d0a84abdfe279b01
ipynb
Jupyter Notebook
source-code/sympy/sympy.ipynb
gjbex/Scientific-Python
b4b7ca06fdedf1de37a0ad537d69c128e24c747c
CC-BY-4.0
2021-03-24T08:05:29.000Z
2022-01-06T13:45:23.000Z
source-code/sympy/sympy.ipynb
gjbex/Scientific-Python
b4b7ca06fdedf1de37a0ad537d69c128e24c747c
CC-BY-4.0
2020-01-15T07:17:50.000Z
2020-01-15T07:17:50.000Z
source-code/sympy/sympy.ipynb
gjbex/Scientific-Python
b4b7ca06fdedf1de37a0ad537d69c128e24c747c
CC-BY-4.0
2020-12-07T08:06:05.000Z
2022-01-25T13:00:48.000Z
Qwen/Qwen-72B
1. YES
2. YES