# Sympy


```python
import sympy as sp

# See: http://docs.sympy.org/latest/tutorial/printing.html
sp.init_printing()
```

## Live shell

http://live.sympy.org/

## Make symbols

Make one symbol:


```python
x = sp.symbols("x")
```

Make several symbols at once:


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

## Substitute

See: http://docs.sympy.org/latest/tutorial/basic_operations.html#substitution


```python
x = sp.symbols("x")
```


```python
expr = sp.cos(x) + 1
expr.subs(x, 0)
```

## Simplify

See: http://docs.sympy.org/latest/tutorial/simplification.html#simplify


```python
x = sp.symbols("x")
```


```python
sp.simplify((x**3 + x**2 - x - 1)/(x**2 + 2*x + 1))
```


```python
sp.simplify(sp.exp(sp.I * sp.pi) + sp.exp(sp.I * sp.pi))
```

## Factor

See: http://docs.sympy.org/latest/tutorial/simplification.html#factor


```python
x = sp.symbols("x")
```


```python
sp.factor(x**3 - x**2 + x - 1)
```

## Expand

See: http://docs.sympy.org/latest/tutorial/simplification.html#expand


```python
x = sp.symbols("x")
```


```python
sp.expand((x - 1)*(x**2 + 1))
```

## Solve

See: http://docs.sympy.org/latest/tutorial/solvers.html


```python
x = sp.symbols("x")
```


```python
eq = sp.Eq(x**2, 1)
```


```python
sp.solveset(eq, x)
```

## Derivatives

See: http://docs.sympy.org/latest/tutorial/calculus.html#derivatives


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

### Create an unevaluated derivative


```python
sp.Derivative(sp.cos(x), x)
```

### Evaluate an unevaluated derivative


```python
diff = sp.Derivative(sp.cos(x), x)
diff
```


```python
diff.doit()
```

### Directly compute an integral


```python
sp.diff(sp.cos(x), x)
```

### Print the equation


```python
expr = sp.exp(x*y*z)
diff = sp.Derivative(expr, x, y, y, z, z, z, z)
sp.Eq(diff, diff.doit())
```

### First derivatives


```python
diff = sp.Derivative(sp.cos(x), x)
sp.Eq(diff, diff.doit())
```


```python
diff = sp.Derivative(3*sp.cos(x)**2, x)
sp.Eq(diff, diff.doit())
```


```python
diff = sp.Derivative(sp.exp(x**2), x)
sp.Eq(diff, diff.doit())
```

### Second derivatives


```python
diff = sp.Derivative(x**4, x, 2)
sp.Eq(diff, diff.doit())
```

or


```python
diff = sp.Derivative(x**4, x, x)
sp.Eq(diff, diff.doit())
```

### Third derivatives


```python
diff = sp.Derivative(x**4, x, 3)
sp.Eq(diff, diff.doit())
```

or


```python
diff = sp.Derivative(x**4, x, x, x)
sp.Eq(diff, diff.doit())
```

### Derivatives with respect to several variables at once


```python
diff = sp.Derivative(sp.exp(x*y), x, y)
sp.Eq(diff, diff.doit())
```

### Multiple derivatives with respect to several variables at once


```python
diff = sp.Derivative(sp.exp(x*y*z), x, y, y, z, z, z, z)
sp.Eq(diff, diff.doit())
```

## Integrals

See: http://docs.sympy.org/latest/tutorial/calculus.html#integrals


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

### Create an unevaluated derivative


```python
sp.Integral(sp.cos(x), x)
```

### Evaluate an unevaluated derivative


```python
integ = sp.Integral(sp.cos(x), x)
integ
```


```python
integ.doit()
```

### Directly compute an integral


```python
sp.integrate(sp.cos(x), x)
```

### Print the equation


```python
integ = sp.Integral(sp.cos(x), x)
sp.Eq(integ, integ.doit())
```

### Create an indefinite integral (i.e. an antiderivative or primitive)


```python
integ = sp.Integral(sp.cos(x), x)
sp.Eq(integ, integ.doit())
```

### Create a definite integral

`sp.oo` means infinity.


```python
integ = sp.Integral(sp.cos(x), (x, -sp.oo, sp.oo))
sp.Eq(integ, integ.doit())
```


```python
integ = sp.Integral(sp.cos(x), (x, -sp.pi, sp.pi))
sp.Eq(integ, integ.doit())
```


```python
integ = sp.Integral(sp.exp(-x), (x, 0, sp.oo))
sp.Eq(integ, integ.doit())
```

### Multiple integrals


```python
integ = sp.Integral(sp.cos(x), (x, -sp.oo, sp.oo), (x, -sp.oo, sp.oo))
sp.Eq(integ, integ.doit())
```

### Multiple variables integrals


```python
integ = sp.Integral(sp.cos(x**2 + y**2), (x, -sp.oo, sp.oo), (y, -sp.oo, sp.oo))
sp.Eq(integ, integ.doit())
```

## Limits

See: http://docs.sympy.org/latest/tutorial/calculus.html#limits

## Series expansion

See: http://docs.sympy.org/latest/tutorial/calculus.html#series-expansion

## Finite differences

See: http://docs.sympy.org/latest/tutorial/calculus.html#finite-differences
