It is possible to do symbolic linear algebrea with Sympy but for numeric computations Numpy is a high performance library that should be used.

Here is how it is described:

NumPy is the fundamental package for scientific computing with Python. It contains among other things: [...] useful linear algebra, Fourier transform, and random number capabilities.

In this section we will see how to:

- Manipulate matrices;
- Solve Matrix equations;
- Calculate Matrix inverse and determinants.

It is straightforward to create a Matrix using Numpy. Let us consider the following as a examples:

$$ A = \begin{pmatrix} 5 & 6 & 2\\ 4 & 7 & 19\\ 0 & 3 & 12 \end{pmatrix} $$$$ B = \begin{pmatrix} 14 & -2 & 12\\ 4 & 4 & 5\\ 5 & 5 & 1 \end{pmatrix} $$First, similarly to Sympy, we need to import Numpy:

In [1]:

```
import numpy as np
```

Now we can define $A$:

In [2]:

```
A = np.matrix([[5, 6, 2],
[4, 7, 19],
[0, 3, 12]])
```

In [3]:

```
A
```

Out[3]:

In [5]:

```
B = np.matrix([[14, -2, 12],
[4, 4, 5],
[5, 5, 1]])
```

In [6]:

```
B
```

Out[6]:

We can obtain the following straightforwardly:

- $5A$ (or any other scalar multiple of $A$);
- $A ^ 3$ (or any other exponent of $A$);
- $A + B$;
- $A - B$;
- $AB$

In [7]:

```
5 * A
```

Out[7]:

In [13]:

```
A ** 3
```

Out[13]:

In [9]:

```
A + B
```

Out[9]:

In [10]:

```
A - B
```

Out[10]:

In [11]:

```
A * B
```

Out[11]:

**EXERCISE** Compute $A ^ 2 - 2 A + 3$ with:

We can use Numpy to (efficiently) solve large systems of equations of the form:

$$Ax=b$$Let us illustrate that with:

$$ A = \begin{pmatrix} 5 & 6 & 2\\ 4 & 7 & 19\\ 0 & 3 & 12 \end{pmatrix} $$$$ b = \begin{pmatrix} -1\\ 2\\ 1 \end{pmatrix} $$In [28]:

```
A = np.matrix([[5, 6, 2],
[4, 7, 19],
[0, 3, 12]])
b = np.matrix([[-1], [2], [1]])
```

We use the `linalg.solve`

command:

In [33]:

```
x = np.linalg.solve(A, b)
x
```

Out[33]:

We can verify our result:

In [34]:

```
A * x
```

Out[34]:

**EXERCISE** Compute the solutions to the matrix equation $Bx=b$ (using the $B$ defined earlier).

Computing the inverse of a matrix is straightforward:

In [35]:

```
Ainv = np.linalg.inv(A)
Ainv
```

Out[35]:

We can verify that $A^{-1}A=\mathbb{1}$:

In [36]:

```
A * Ainv
```

Out[36]:

The above might not look like the identity matrix but if you look closer you see that the diagonals are all `1`

and the off diagonals are a **very** small number (which from a computer's point of view is `0`

).

To calculate the determinant:

In [37]:

```
np.linalg.det(A)
```

Out[37]:

**EXERCISE** Compute the inverse and determinant of $B$ (defined previously).

In this section we have seen how to using Numpy:

- Manipulate matrices;
- Solve linear systems;
- Compute Matrix inverses and determinants.

This again just touches on the capabilities of Numpy.

Let us take a look at Pandas for data analysis.