Sympy is described as a:
"... Python library for symbolic mathematics."
This means it can be used to:
It has other capabilities that we will not go in to in this handbook. But you can read more about it here: http://www.sympy.org/en/index.html
Before we can start using the library to manipulate expressions, we need to import it.
import sympy as sym
The above imports the library and gives us access to it's commands using the shortand sym
which is conventially used.
If we wanted to get Python to check that $x - x = 0$ we would get an error if we did not tell Python what $x$ was.
This is where Sympy comes in, we can tell Python to create $x$ as a symbolic variable:
x = sym.symbols('x')
Now we can calculate $x - x$:
x - x
0
We can create and manipulate expressions in Sympy. Let us for example verify:
$$(a + b) ^ 2 = a ^ 2 + 2ab + b ^2$$First, we create the symbolic variables $a, b$:
a, b = sym.symbols('a, b')
Now let's create our expression:
expr = (a + b) ** 2
expr
(a + b)**2
Note we can get Sympy to use LaTeX so that the output looks nice in a notebook:
sym.init_printing()
expr
Let us expand our expression:
expr.expand()
Note that we can also get Sympy to produce the LaTeX code for future use:
sym.latex(expr.expand())
'a^{2} + 2 a b + b^{2}'
EXERCISE Use Sympy to verify the following expressions:
expand
, try factor
)We can use Sympy to solve symbolic expression. For example let's find the solution in $x$ of the quadratic equation:
$$a x ^ 2 + b x + c = 0$$# We only really need to define `c` but doing them all again.
a, b, c, x = sym.symbols('a, b, c, x')
The Sympy command for solving equations is solveset
. The first argument is an expression for which the root will be found. The second argument is the value that we are solving for.
sym.solveset(a * x ** 2 + b * x + c, x)
EXERCISE Use Sympy to find the solutions to the generic cubic equation:
$$a x ^ 3 + b x ^ 2 + c x + d = 0$$It is possible to pass more arguments to solveset
for example to constrain the solution space. Let us see what the solution of the following is in $\mathbb{R}$:
sym.solveset(x ** 2 + 1, x, domain=sym.S.Reals)
EXERCISE Use Sympy to find the solutions to the following equations:
We can use Sympy to compute limits. Let us calculate:
$$\lim_{x\to 0^+}\frac{1}{x}$$sym.limit(1/x, x, 0, dir="+")
EXERCISE Compute the following limits:
We can use also Sympy to differentiate and integrate. Let us experiment with differentiating the following expression:
$$x ^ 2 - \cos(x)$$sym.diff(x ** 2 - sym.cos(x), x)
Similarly we can integrate:
sym.integrate(x ** 2 - sym.cos(x), x)
We can also carry out definite integrals:
sym.integrate(x ** 2 - sym.cos(x), (x, 0, 5))
EXERCISE Use Sympy to calculate the following:
Finally Sympy can be used to plot functions. Note that this makes use of another Python library called matplotlib. Whilst Sympy allows us to not directly need to make use of matplotlib it could be worth learning to use as it's a very powerful and versatile library.
Before plotting in Jupyter we need to run a command to tell it to display the plots directly in the notebook:
%matplotlib inline
Let us plot $x^2$:
expr = x ** 2
p = sym.plot(expr);
We can directly save that plot to a file if we wish to:
p.save("x_squared.pdf");
EXERCISE Plot the following functions:
ylim
helpful as an argument)Experiment with saving your plots to a file.
This section has discussed using Sympy to:
This just touches the surface of what Sympy can do.
Let us move on to using [Numpy](02 - Linear algebra with Numpy.ipynb) to do Linear Algebra.