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.
We first need to import SymPy. We also initialize pretty printing in the notebook (see the first recipe of this chapter).
Let's define a few symbols and a function (which is just an expression depending on
x
):In [1]: var('x z') Out[1]: (x, z) In [2]: f = 1/(1+x**2)
Let's evaluate this function at
1
:In [3]: f.subs(x, 1) Out[3]: 1/2
We can compute the derivative of this function:
In [4]: diff(f, x) Out[4]: -2*x/(x**2 + 1)**2
What is
f
's limit to infinity? (Note the double o (oo
) for the infinity symbol):In [5]: limit(f, x, oo) Out[5]: 0
Here's how to compute a Taylor series (here, around
0
, of order9
). The Big O can be removed with theremoveO()
method.In [6]: series(f, x0=0, n=9) Out[6]: 1 - x**2 + x**4 - x**6 + x**8 + O...