# Initialize Enviroment


```python
from IPython.display import display, Math
from sympy import *
init_printing()

from helper import comparator_factory, comparator_eval_factory, comparator_method_factory

x,y,z = symbols('x y z')

comparator = comparator_factory('Before applying {}:','After:')
method_comparator = comparator_method_factory('Before calling {}:','After:')
eval_comparator = comparator_eval_factory('Before evaluation:','After evaluation:')
```

# Calculus

In this section, we will cover how to perform baisc calculus tasks such as derivatives, integrals,litmis and series expansion.

## Derivatives
To take derivatives, use diff() 

### basic derivatives
Pass in an expresison and one symbol with respect to which the derivative is applied.


```python
expr = sin(x)

expr_diff = diff(expr,x)

comparator(expr, diff, x)
```

diff() can also be called as a method


```python
expr = sin(x)

method_comparator(expr, 'diff', x)
```

To create an unevaluated derivative, use Derivative class and initiate it with the same syntax with diff()


```python
expr = sin(x)

diff_expr = Derivative(expr,x)

print('Before evaluation:')
display(diff_expr)
```

And call ```doit()``` method to evaluate it.


```python
print('After evaluation:')
display(diff_expr.doit())
```

To reduce code duplication, we user ```eval_comparator``` to handle the comparasion in later notes

### higher order derivative
To take n order derivatives, pass the symbol n times or pass the symbol once and then follows it with n.


```python
expr = Derivative(x**4,x,x,x)

eval_comparator(expr)
```

You can achive the same calculation with the second syntax.


```python
expr = Derivative(x**4,x,3)

eval_comparator(expr)
```

### higher order partial derivatives.

Just pass the symbols in order, with the same syntax with single variable derivative.


```python
expr = Derivative(exp(x*y*z),x, y, y, z, z, z, z)

eval_comparator(expr)
```

Or user number to control the derivatives order for each symbol


```python
expr = Derivative(exp(x*y*z),x, y, 2, z, 4)

eval_comparator(expr)
```

# Integrals

## indefinte integrals
Similiar with derivatives, integrals can be performed through integral() by function or method. To construct an unevaluated expression, initialize an Integral class and call doit() to evaludate the calculation.


```python
expr = Integral(cos(x),x)

eval_comparator(expr)
```

### definite integrals

To perform a definite integral, pass in a tuple of variable symbol, lower bound and upper bound.


```python
expr = Integral(exp(-x),(x,0,oo))

eval_comparator(expr)
```

Note, $\infty$ in Sympy is oo (double lower case 'O')

### multiple integrals
To perform a multiple integral, pass multiple tuples of symbol variable and integral bounds.


```python
expr = Integral(exp(-x**2 - y**2), (x, -oo, oo), (y, -oo, oo))

eval_comparator(expr)
```

If Sympy cannot calculate an integral on an expression, it returns an unevaluated integral expression.


```python
expr = Integral(x**x)

eval_comparator(expr)
```

# Limits

Similiar with derivatives, limits can be performed through limit() by function or method. To construct an unevaluated expression, initialize an Limit class and call doit() to evaludate the calculation.

By default the limit is calculated from right to left with default setting ```dir = '+'```.


```python
expr = Limit(sin(x)/x, x, 0)

eval_comparator(expr)
```

To caluculate a limit from left to right. Pass ```dir='-'```


```python
expr = Limit(sin(x)/x, x, 0, dir = '-')

eval_comparator(expr)
```

# Series Expansion

Sympy can calculate an asymptotic series expansion of a funciton around a point $x_0$ with respect to  $O(x-x_0)^n$ by calling ```series``` method of an expression.


```python
expr = sin(x)

method_comparator(expr, 'series', x,0,6)
```

To remove the order term, call ```removeO()``` method


```python
print('Expanded expression without order term:')
expr.series(x,0,6).removeO()
```

# Reference
[Sympy Documentation](http://docs.sympy.org/latest/index.html)

# Related Articles
* [Sympy Notes I]({filename}0026_sympy_intro_1_en.ipynb)
* [Sympy Notes II]({filename}0027_sympy_intro_2_en.ipynb)
* [Sympy Notes III]({filename}0028_sympy_intro_3_en.ipynb)
* [Sympy Notes IV]({filename}0029_sympy_intro_4_en.ipynb)

f4464673f3728733fe39760a94f4131e20a209cf
ipynb
Jupyter Notebook
0011_sympy/0027_sympy_intro_2_en.ipynb
junjiecai/jupyter_demos
8aa8a0320545c0ea09e05e94aea82bc8aa537750
MIT
2019-09-16T10:44:39.000Z
2021-09-04T18:55:52.000Z
0011_sympy/0027_sympy_intro_2_en.ipynb
junjiecai/jupyter_demos
8aa8a0320545c0ea09e05e94aea82bc8aa537750
MIT
0011_sympy/0027_sympy_intro_2_en.ipynb
junjiecai/jupyter_demos
8aa8a0320545c0ea09e05e94aea82bc8aa537750
MIT
2020-10-24T16:19:29.000Z
2021-09-04T18:55:57.000Z
Qwen/Qwen-72B
1. YES
2. YES