Sympy is described as a:

"... Python library for symbolic mathematics."

This means it can be used to:

- Manipulate symbolic expressions;
- Solve symbolic equations;
- Carry out symbolic Calculus;
- Plot symbolic function.

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.

In [1]:

```
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:

In [2]:

```
x = sym.symbols('x')
```

Now we can calculate $x - x$:

In [3]:

```
x - x
```

Out[3]:

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$:

In [4]:

```
a, b = sym.symbols('a, b')
```

Now let's create our expression:

In [5]:

```
expr = (a + b) ** 2
expr
```

Out[5]:

**Note** we can get Sympy to use LaTeX so that the output looks nice in a notebook:

In [6]:

```
sym.init_printing()
```

In [7]:

```
expr
```

Out[7]:

Let us expand our expression:

In [8]:

```
expr.expand()
```

Out[8]:

Note that we can also get Sympy to produce the LaTeX code for future use:

In [9]:

```
sym.latex(expr.expand())
```

Out[9]:

**EXERCISE** Use Sympy to verify the following expressions:

- $(a - b) ^ 2 = a ^ 2 - 2 a b + b^2$
- $a ^ 2 - b ^ 2 = (a - b) (a + b)$ (instead of using
`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$$In [10]:

```
# 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.

In [11]:

```
sym.solveset(a * x ** 2 + b * x + c, x)
```

Out[11]:

**EXERCISE** Use Sympy to find the solutions to the generic cubic equation:

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}$:

In [12]:

```
sym.solveset(x ** 2 + 1, x, domain=sym.S.Reals)
```

Out[12]:

**EXERCISE** Use Sympy to find the solutions to the following equations:

- $x ^ 2 == 2$ in $\mathbb{N}$;
- $x ^ 3 + 2 x = 0$ in $\mathbb{R}$.

We can use Sympy to compute limits. Let us calculate:

$$\lim_{x\to 0^+}\frac{1}{x}$$In [13]:

```
sym.limit(1/x, x, 0, dir="+")
```

Out[13]:

**EXERCISE** Compute the following limits:

- $\lim_{x\to 0^-}\frac{1}{x}$
- $\lim_{x\to 0}\frac{1}{x^2}$

We can use also Sympy to differentiate and integrate. Let us experiment with differentiating the following expression:

$$x ^ 2 - \cos(x)$$In [14]:

```
sym.diff(x ** 2 - sym.cos(x), x)
```

Out[14]:

Similarly we can integrate:

In [15]:

```
sym.integrate(x ** 2 - sym.cos(x), x)
```

Out[15]:

We can also carry out definite integrals:

In [16]:

```
sym.integrate(x ** 2 - sym.cos(x), (x, 0, 5))
```

Out[16]:

**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:

In [17]:

```
%matplotlib inline
```

Let us plot $x^2$:

In [18]:

```
expr = x ** 2
p = sym.plot(expr);
```

We can directly save that plot to a file if we wish to:

In [19]:

```
p.save("x_squared.pdf");
```

**EXERCISE** Plot the following functions:

- $y=x + cos(x)$
- $y=x ^ 2 - e^x$ (you might find
`ylim`

helpful as an argument)

Experiment with saving your plots to a file.

This section has discussed using Sympy to:

- Manipulate symbolic expressions;
- Calculate limits, derivates and integrals;
- Plot a symbolic expression.

This just touches the surface of what Sympy can do.

Let us move on to using Numpy to do Linear Algebra.