Python's list data structure is very general, but it does not (directly have the capability of representing vectors, matrices, and matrix operations. It is not terribly difficult to take nested lists and interpret them as matrices. NumPy provides a set of methods to easily perform operations on arrays, such as matrix multiplication, inverting a matrix, etc.
Numpy is a package to enhance Python. We have already seen packages like math and random. We could refer to numpy methods using (for example) numpy.array(). However, it is more convenient to import numpy as np and use the shorthand np.array() for example.
In Ex2.1.1 we construct a one dimensional array (vector) from a list named cvalues. Note that the entries in cvalues (as in all lists) are separated by commas. In line 5 we convert the list to an array using the numpy method np.array(). Notice the difference in the two print outs. the first one prints the list, while the second prints the 1-d array (with spaces, not commas separating elements.
# Ex2.1.1
import numpy as np
cvalues = [20.1, 20.8, 21.9, 22.5, 22.7, 22.3, 21.8, 21.2, 20.9, 20.1]
print(cvalues, "\n")
C = np.array(cvalues)
print(C)
In the next example we will create a two dimensional array, using values in a nested list. Each sublist in mvalues represents a row in the 2-d array. Line 5 creates the array from the list. Notice the difference between the two printouts
# Ex2.1.2
import numpy as np
mvalues = [[1,6,5],[3,4,8],[1,12,3]]
print(cvalues, "\n")
M = np.array(mvalues)
print(M)
In the next few examples, I'll be using one or more of the following matrices.
$A=\begin{bmatrix} 1 & 6 & 5 \\ 3 & 4 & 8 \\ 2 & 12 & 3 \end{bmatrix}
B=\begin{bmatrix} 3 & 4 & 6 \\ 5 & 6 & 7 \\ 6 & 56 & 7 \end{bmatrix}$
The matrix transpose applies the method transpose() to the matrix.
# Ex2.1.3
import numpy as np
# three dimensional array a
a = np.array([[1, 6, 5],[3 ,4, 8],[2, 12, 3]])
print(a, "\n")
print(a.transpose())
The method .dot(A,B) will form the matrix product of AB. Note: standard requirements for matrix multiplication apply: the row rank of the first matrix must equal the column rank of the second matrix.
# Ex2.1.4
import numpy as np
# three dimensional array a
A = np.array([[1, 6, 5],[3 ,4, 8],[2, 12, 3]])
B = np.array([[3, 4, 6],[5 ,6, 7],[6, 56, 7]])
print(A, "\n")
print(B, "\n")
print(np.dot(A,B))
Numpy is segmented into groupings of methods. In order to take the inverse of a matrix, you must refer to the method as np.linalg.inv(). If you leave out the .linalg you will get an error. Note that multiplying A by its inverse will yield the identity matrix (well, almost). The difference between AB in the off diagonal terms is caused by finite representation of floating point arithmetic.
# Ex2.1.5
import numpy as np
A = np.array([[1, 6, 5],[3 ,4, 8],[2, 12, 3]])
print(A, "\n")
B=np.linalg.inv(A)
print(B,"\n")
print(np.dot(A,B))
$\left[ \begin{array}{c} 3 \\ 5 \end{array} \right] = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} \left[ \begin{array}{c} -1 \\ 2 \end{array} \right]$
Note that we use the same procedure as matrix multiplication. In this case we are multiplying a 2x2 matrix by a 2x1 matrix. That is what multiplying a matrix by a vector is. Notice that even for the V numpy array we use the representation [[-1],[2]]. If we use [-1, 2] this would be a 1x2 row vector.
# Ex2.1.6
import numpy as np
A=np.array([[1,2],[3,4]])
V=np.array([[-1],[2]])
Z=np.dot(A,V)
print(Z)
The scaler product is the product of a row vector and a column vector. In linear algrbra we represent this as
$ \vec x \cdot \vec y$
Note in Ex 2.1.7 the way x (a row vector) and y (a column vector) are represented
The result is always a scalar quantity--a 1x1 numpy array with a single value.
# Ex 2.1.7
import numpy as np
x=np.array([2,4])
y=np.array([[-1],[2]])
z=np.dot(x,y)
print(z)
# Ex 2.1.8
import numpy as np
A=np.array([[1,2],[3,4]])
z=np.linalg.det(A)
print(z)r
The only confusing issue is that in refering to to the method det, you must specify that it is part of the submodule linalg. If we are going to use the Linear Algebra submodule often avoid using np.linalg.det() by doing a sub-import from numpy.
# Ex 2.1.9
import numpy as np
from numpy import linalg as LA
A=np.array([[1,2],[3,4]])
z=LA.det(A)
print(z)
reference
Suppose we had a vector matrix equation
$\vec x = A \vec y$
With an arbitrary A matrix, $\vec x $ and $\vec y $ are highly coupled many values of y map into values of x. Suppose we could find a Transformation of coordinates so that
$z_1=S\vec x$ and $z_2=\vec y$ or $S\vec x = A S \vec y$ furthermore, we carefully chose the transformation matrix S so that:
$z_1 =S^{-1} A S \vec z_2$
Where $S^{-1} A S = D$ a diagonal matrix \ The diagonal elements of D are the eigenvalues of A, while the columns in the S matrix are the eigenvectors.
the assignment w,v = np.linalg.eigh(matrix) returns the eigenvalues in an array w and the eigenvectors in the array v, where the eigenvectors are the columns the v. The order of the eigenvectors corresponds to the order of the eigenvalues in w.
# Ex 2.1.10
import numpy as np
A=np.array([[4,-3,-3],[3,-2,-3],[-1,1,2]])
print(A,"\n")
# Finding the eigenvalues w and eigenvectors v
# The eigenvectors are displayed in array form. In this case a 3X3 array
w, v = np.linalg.eig(A)
print("eigenvalues","\n",w,"\n")
print("eigenvectors","\n",v,"\n")
# The matrix of eigenvectors is the transformation matrix S
S=v
# Find the inverse of S
Sinv=np.linalg.inv(S)
# transformed matrix Sinv A S should be a diagonal matrix with the eigenvalues on the diagonal
D= np.dot(np.dot(Sinv,A),S)
print("diagonalized matrix","\n")
print(D)
NumPy has a rich set of math functions. Often, programmers use these from numpy rather than the math package introduced earlier.
NumPy Mathematical Functions
Note: you can evaluate most functions for a single value or for an array of values. See the left frame in the above reference for detailed documentation of arguments.
In the following example the first print uses a simple evaluation of $\sin(\pi/2)$ while the second example applies the sine function to an array. Note the array contains degrees in angles, so we must convert to radians
# Ex 2.1.11
import numpy as np
print("sine of a floating point number")
print(np.sin(np.pi/2))
print("sine of an array with angles in degrees")
print(np.sin(np.array((0., 30., 45., 60., 90.)) * np.pi / 180. ))
There is a very handy NumPy method called linspace. It produces an array of evenly spaced numbers over a specific interval. https://numpy.org/doc/stable/reference/generated/numpy.linspace.html
In this example we have used np.linspace(0,4,20). We want to create a sequence of numbers starting at 0 and ending at 4, with 20 numbers. NOTE: COUNTING FROM 0 TO 4 WITH 20 NUMBERS GIVES AN INTERVAL BETWEEN NUMBERS OF .2105... Count the number of elements in each sequence below.
# Ex 2.1.12
x=np.linspace(0,4,20)
print(x)
x=np.linspace(0,4,21)
print("\n\n",x)
© Donald R. Falkenburg
Last updated 4/29/23