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:
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:
import numpy as np
Now we can define $A$:
A = np.matrix([[5, 6, 2],
[4, 7, 19],
[0, 3, 12]])
A
matrix([[ 5, 6, 2], [ 4, 7, 19], [ 0, 3, 12]])
B = np.matrix([[14, -2, 12],
[4, 4, 5],
[5, 5, 1]])
B
matrix([[14, -2, 12], [ 4, 4, 5], [ 5, 5, 1]])
We can obtain the following straightforwardly:
5 * A
matrix([[25, 30, 10], [20, 35, 95], [ 0, 15, 60]])
A ** 3
matrix([[ 557, 1284, 3356], [ 760, 2305, 6994], [ 288, 1074, 3519]])
A + B
matrix([[19, 4, 14], [ 8, 11, 24], [ 5, 8, 13]])
A - B
matrix([[ -9, 8, -10], [ 0, 3, 14], [ -5, -2, 11]])
A * B
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} $$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} $$A = np.matrix([[5, 6, 2],
[4, 7, 19],
[0, 3, 12]])
b = np.matrix([[-1], [2], [1]])
We use the linalg.solve
command:
x = np.linalg.solve(A, b)
x
matrix([[ 0.45736434], [-0.62790698], [ 0.24031008]])
We can verify our result:
A * x
matrix([[-1.], [ 2.], [ 1.]])
EXERCISE Compute the solutions to the matrix equation $Bx=b$ (using the $B$ defined earlier).
Computing the inverse of a matrix is straightforward:
Ainv = np.linalg.inv(A)
Ainv
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}$:
A * Ainv
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:
np.linalg.det(A)
-128.99999999999997
EXERCISE Compute the inverse and determinant of $B$ (defined previously).
In this section we have seen how to using Numpy:
This again just touches on the capabilities of Numpy.
Let us take a look at [Pandas](03 - Data analysis with Pandas.ipynb) for data analysis.