This is one of the 100+ free recipes of the IPython Cookbook, Second Edition, by Cyrille Rossant, a guide to numerical computing and data science in the Jupyter Notebook. The ebook and printed book are available for purchase at Packt Publishing.
▶ Text on GitHub with a CC-BY-NC-ND license
▶ Code on GitHub with a MIT license
Chapter 15 : Symbolic and Numerical Mathematics
SymPy contains a rich calculus toolbox to analyze real-valued functions: limits, power series, derivatives, integrals, Fourier transforms, and so on. In this recipe, we will show the very basics of these capabilities.
- Let's define a few symbols and a function (which is just an expression depending on
x
):
from sympy import *
init_printing()
var('x z')
f = 1 / (1 + x**2)
- Let's evaluate this function at 1:
f.subs(x, 1)
- We can compute the derivative of this function:
diff(f, x)
- What is
$f$ 's limit to infinity? (Note the double o (oo
) for the infinity symbol):
limit(f, x, oo)
- Here's how to compute a Taylor series (here, around 0, of order 9). The Big O can be removed with the
removeO()
method.
series(f, x0=0, n=9)
- We can compute definite integrals (here, over the entire real line):
integrate(f, (x, -oo, oo))
- SymPy can also compute indefinite integrals:
integrate(f, x)
- Finally, let's compute
$f$ 's Fourier transforms:
fourier_transform(f, x, z)
SymPy includes a large number of other integral transforms besides the Fourier transform (http://docs.sympy.org/latest/modules/integrals/integrals.html). However, SymPy will not always be able to find closed-form solutions.
Here are a few general references about real analysis and calculus:
- Real analysis on Wikipedia, at https://en.wikipedia.org/wiki/Real_analysis#Bibliography
- Calculus on Wikibooks, at http://en.wikibooks.org/wiki/Calculus
- Real analysis on Awesome Math, at https://github.com/rossant/awesome-math/#real-analysis