Sympy II - Limit and Derivative

JunjieCai

2022-05-22

中文

It is recommended to download the .ipynb file and related resources here. Then you can test the codes in the article interactively while you are reading in Jupyter.

Initialize Enviroment

In [1]:
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.

In [2]:
expr = sin(x)

expr_diff = diff(expr,x)

comparator(expr, diff, x)
Before applying diff():
$\displaystyle \sin{\left(x \right)}$
After:
$\displaystyle \cos{\left(x \right)}$

diff() can also be called as a method

In [3]:
expr = sin(x)

method_comparator(expr, 'diff', x)
Before calling diff():
$\displaystyle \sin{\left(x \right)}$
After:
$\displaystyle \cos{\left(x \right)}$

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

In [4]:
expr = sin(x)

diff_expr = Derivative(expr,x)

print('Before evaluation:')
display(diff_expr)
Before evaluation:
$\displaystyle \frac{d}{d x} \sin{\left(x \right)}$

And call doit() method to evaluate it.

In [5]:
print('After evaluation:')
display(diff_expr.doit())
After evaluation:
$\displaystyle \cos{\left(x \right)}$

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.

In [6]:
expr = Derivative(x**4,x,x,x)

eval_comparator(expr)
Before evaluation:
$\displaystyle \frac{d^{3}}{d x^{3}} x^{4}$
After evaluation:
$\displaystyle 24 x$

You can achive the same calculation with the second syntax.

In [7]:
expr = Derivative(x**4,x,3)

eval_comparator(expr)
Before evaluation:
$\displaystyle \frac{d^{3}}{d x^{3}} x^{4}$
After evaluation:
$\displaystyle 24 x$

higher order partial derivatives.

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

In [8]:
expr = Derivative(exp(x*y*z),x, y, y, z, z, z, z)

eval_comparator(expr)
Before evaluation:
$\displaystyle \frac{\partial^{7}}{\partial z^{4}\partial y^{2}\partial x} e^{x y z}$
After evaluation:
$\displaystyle x^{3} y^{2} \left(x^{3} y^{3} z^{3} + 14 x^{2} y^{2} z^{2} + 52 x y z + 48\right) e^{x y z}$

Or user number to control the derivatives order for each symbol

In [9]:
expr = Derivative(exp(x*y*z),x, y, 2, z, 4)

eval_comparator(expr)
Before evaluation:
$\displaystyle \frac{\partial^{7}}{\partial z^{4}\partial y^{2}\partial x} e^{x y z}$
After evaluation:
$\displaystyle x^{3} y^{2} \left(x^{3} y^{3} z^{3} + 14 x^{2} y^{2} z^{2} + 52 x y z + 48\right) e^{x y z}$

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.

In [10]:
expr = Integral(cos(x),x)

eval_comparator(expr)
Before evaluation:
$\displaystyle \int \cos{\left(x \right)}\, dx$
After evaluation:
$\displaystyle \sin{\left(x \right)}$

definite integrals

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

In [11]:
expr = Integral(exp(-x),(x,0,oo))

eval_comparator(expr)
Before evaluation:
$\displaystyle \int\limits_{0}^{\infty} e^{- x}\, dx$
After evaluation:
$\displaystyle 1$

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.

In [12]:
expr = Integral(exp(-x**2 - y**2), (x, -oo, oo), (y, -oo, oo))

eval_comparator(expr)
Before evaluation:
$\displaystyle \int\limits_{-\infty}^{\infty}\int\limits_{-\infty}^{\infty} e^{- x^{2} - y^{2}}\, dx\, dy$
After evaluation:
$\displaystyle \pi$

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

In [13]:
expr = Integral(x**x)

eval_comparator(expr)
Before evaluation:
$\displaystyle \int x^{x}\, dx$
After evaluation:
$\displaystyle \int x^{x}\, dx$

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 = '+'.

In [14]:
expr = Limit(sin(x)/x, x, 0)

eval_comparator(expr)
Before evaluation:
$\displaystyle \lim_{x \to 0^+}\left(\frac{\sin{\left(x \right)}}{x}\right)$
After evaluation:
$\displaystyle 1$

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

In [15]:
expr = Limit(sin(x)/x, x, 0, dir = '-')

eval_comparator(expr)
Before evaluation:
$\displaystyle \lim_{x \to 0^-}\left(\frac{\sin{\left(x \right)}}{x}\right)$
After evaluation:
$\displaystyle 1$

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.

In [16]:
expr = sin(x)

method_comparator(expr, 'series', x,0,6)
Before calling series():
$\displaystyle \sin{\left(x \right)}$
After:
$\displaystyle x - \frac{x^{3}}{6} + \frac{x^{5}}{120} + O\left(x^{6}\right)$

To remove the order term, call removeO() method

In [17]:
print('Expanded expression without order term:')
expr.series(x,0,6).removeO()
Expanded expression without order term:
Out[17]:
$\displaystyle \frac{x^{5}}{120} - \frac{x^{3}}{6} + x$