Working with Matrices#

import numpy as np

See also

Check out Mathematical Python > SciPy to see more about working with matrices in NumPy.

Manual Construction#

Matrices are represented in Python as NumPy arrays. The simplest way to construct a matrix is to use the function np.array. Enter the matrix as a list of rows. Write each row as a list of numbers in square brackets [r0,r1,...,rn] with each entry separated by a comma ,. For example, let’s create the matrix

\[\begin{split} A = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix} \end{split}\]
A = np.array([[1.,2.,3.],[4.,5.,6.],[7.,8.,9.]])
print(A)
[[1. 2. 3.]
 [4. 5. 6.]
 [7. 8. 9.]]

Note that we enter the numbers with decimal points (ie. 1.) to ensure the entries of the matrix are floats instead of integers. This is good practice since most of our matheamtical computations result in decimal values and so we define our matrices with floats from the start.

Construction Functions#

There are NumPy functions such as np.zeros, np.ones, np.eye and np.diag for constructing matrices. For example, create a 2 by 5 matrix of zeros:

np.zeros([2,5])
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])

Create the identity matrix of size 6:

np.eye(6)
array([[1., 0., 0., 0., 0., 0.],
       [0., 1., 0., 0., 0., 0.],
       [0., 0., 1., 0., 0., 0.],
       [0., 0., 0., 1., 0., 0.],
       [0., 0., 0., 0., 1., 0.],
       [0., 0., 0., 0., 0., 1.]])

Create a 3 by 2 matrix of ones:

np.ones([3,2])
array([[1., 1.],
       [1., 1.],
       [1., 1.]])

Create a diagonal matrix:

np.diag([1.,2.,3.])
array([[1., 0., 0.],
       [0., 2., 0.],
       [0., 0., 3.]])

Create a matrix with entries on the upper diagonal:

np.diag([1.,2.,3.],1)
array([[0., 1., 0., 0.],
       [0., 0., 2., 0.],
       [0., 0., 0., 3.],
       [0., 0., 0., 0.]])

Create a matrix with entries on the lower diagonal:

np.diag([1.,2.,3.],-1)
array([[0., 0., 0., 0.],
       [1., 0., 0., 0.],
       [0., 2., 0., 0.],
       [0., 0., 3., 0.]])

Stacking#

Use the functions np.row_stack, np.column_stack and np.block to stack vectors and matrices to create new ones. For example, create two vectors and put them into the columns of a matrix:

c1 = np.array([1.,2.])
c2 = np.array([3.,4.])
A = np.column_stack([c1,c2])
print(A)
[[1. 3.]
 [2. 4.]]

Create two row vectors and put them into the rows of a matrix:

r1 = np.array([0.,-1.])
r2 = np.array([5.,7.])
A = np.row_stack([r1,r2])
print(A)
[[ 0. -1.]
 [ 5.  7.]]

Create the block matrix:

\[\begin{split} A = \left[ \begin{array}{rrrr} 1 & \phantom{+}1 & 0 & 0 \\ 1 & 1 & 0 & 0 \\ 0 & 0 & -1 & -1 \\ 0 & 0 & -1 & -1 \end{array} \right] \end{split}\]
B1 = np.ones([2,2])
B2 = np.zeros([2,2])
A = np.block([[B1,B2],[B2,-B1]])
print(A)
[[ 1.  1.  0.  0.]
 [ 1.  1.  0.  0.]
 [ 0.  0. -1. -1.]
 [ 0.  0. -1. -1.]]

Addition and Multiplication#

Use operators + and - for matrix addition and subtraction, and * for scalar multiplication. For example, let’s use np.eye, np.ones, np.diag, matrix addition and scalar multiplication to construct the matrix:

\[\begin{split} A = \left[ \begin{array}{rrrrr} 2 & -1 & 0 & 0 & 0 \\ -1 & 2 & -1 & 0 & 0 \\ 0 & -1 & 2 & -1 & 0 \\ 0 & 0 & -1 & 2 & -1 \\ 0 & 0 & 0 & -1 & 2 \end{array} \right] \end{split}\]
N = 4;
A = 2*np.eye(N+1) - np.diag(np.ones(N),1) - np.diag(np.ones(N),-1)
print(A)
[[ 2. -1.  0.  0.  0.]
 [-1.  2. -1.  0.  0.]
 [ 0. -1.  2. -1.  0.]
 [ 0.  0. -1.  2. -1.]
 [ 0.  0.  0. -1.  2.]]

Use the operator @ for matrix multiplication. For example, let’s compute \(A \mathbf{x}\) where \(A\) is the matrix above and \(\mathbf{x}\) is the vector:

\[\begin{split} \mathbf{x} = \begin{bmatrix} 1 \\ 1 \\ 1 \\ 1 \\ 1 \end{bmatrix} \end{split}\]
x = np.ones([N+1,1])
y = A @ x
print(y)
[[1.]
 [0.]
 [0.]
 [0.]
 [1.]]

Indexing#

Access the entry of matrix \(A\) at row \(i\) and column \(j\) using the syntax A[i,j]. Note that Python uses 0-indexing and so entries always start at index 0. For example, consider the matrix:

A = np.array([[1.,0.,-2.],[7.,5.,-1.],[3.,4.,-8.]])
print(A)
[[ 1.  0. -2.]
 [ 7.  5. -1.]
 [ 3.  4. -8.]]

Access the entry in row 1 and column 2:

A[1,2]
-1.0

Use the colon : to select an entire row or column. For example, select row 2:

A[2,:]
array([ 3.,  4., -8.])

Select column 1:

A[:,1]
array([0., 5., 4.])

Vector Functions#

Compute the maximum value in a vector with the function np.max.

x = np.array([-4.9,1.4,2.2,6.5,-4.3])
print(x)
[-4.9  1.4  2.2  6.5 -4.3]
np.max(x)
6.5

Compute the minimum value with the function np.min.

np.min(x)
-4.9

Compute the sum of the values in a vector with the function np.sum.

np.sum(x)
0.8999999999999995

Note that numerical computing always includes a little bit rounding error.