# Linear algebra with Numpy¶

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.

## Manipulating matrices¶

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 :
import numpy as np


Now we can define $A$:

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

In :
A

Out:
matrix([[ 5,  6,  2],
[ 4,  7, 19],
[ 0,  3, 12]])
In :
B = np.matrix([[14, -2, 12],
[4, 4, 5],
[5, 5, 1]])

In :
B

Out:
matrix([[14, -2, 12],
[ 4,  4,  5],
[ 5,  5,  1]])

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

Out:
matrix([[25, 30, 10],
[20, 35, 95],
[ 0, 15, 60]])
In :
A ** 3

Out:
matrix([[ 557, 1284, 3356],
[ 760, 2305, 6994],
[ 288, 1074, 3519]])
In :
A + B

Out:
matrix([[19,  4, 14],
[ 8, 11, 24],
[ 5,  8, 13]])
In :
A - B

Out:
matrix([[ -9,   8, -10],
[  0,   3,  14],
[ -5,  -2,  11]])
In :
A * B

Out:
matrix([[104,  24,  92],
[179, 115, 102],
[ 72,  72,  27]])

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

$$A = \begin{pmatrix} 1 & -1\\ 2 & 1 \end{pmatrix}$$

## Solving Matrix equations¶

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 :
A = np.matrix([[5, 6, 2],
[4, 7, 19],
[0, 3, 12]])
b = np.matrix([[-1], , ])


We use the linalg.solve command:

In :
x = np.linalg.solve(A, b)
x

Out:
matrix([[ 0.45736434],
[-0.62790698],
[ 0.24031008]])

We can verify our result:

In :
A * x

Out:
matrix([[-1.],
[ 2.],
[ 1.]])

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

## Matrix inversion and determinants¶

Computing the inverse of a matrix is straightforward:

In :
Ainv = np.linalg.inv(A)
Ainv

Out:
matrix([[-0.20930233,  0.51162791, -0.7751938 ],
[ 0.37209302, -0.46511628,  0.6744186 ],
[-0.09302326,  0.11627907, -0.08527132]])

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

In :
A * Ainv

Out:
matrix([[  1.00000000e+00,   2.77555756e-17,   3.05311332e-16],
[ -2.08166817e-16,   1.00000000e+00,  -2.08166817e-16],
[  5.55111512e-17,  -5.55111512e-17,   1.00000000e+00]])

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 :
np.linalg.det(A)

Out:
-128.99999999999997

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

## Summary¶

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.