The following tasks should be solved, individually or in groups of 2. Write the answers to the questions in a readme. When reviewing, check that you got the same answers.
Matrix fun
Solve the following using Python with numpy.
In numpy, there are some handy ways of working with matrixes (they are
all explained later in this document):
- 1 import numpy as np
- 2 from numpy.linalg import inv
3
- 4 # Creating matrices
- 5 A = np.array([[ 1, 2 ],[ 3, 4]])
- 6 B = np.array([[ 9, 8 ],[ 7, 6]])
7
- 8 # Transposing:
- 9 A.T # A transposed (danish: A transponeret)
- 10 B.T # B transposed
11
- 12 # Matrix multiplication:
- 13 A@B
14
- 15 # Inverse:
- 16 inv(A)
Figure 1: Basic matrix functionality with numpy.
In the following, when talking about multiplication, we implicitly mean ma-trix multiplication (same as dot product).
Task 1
Given the two following matrices
3 1 −1 4A=26B=38 1
- (a) Find AT
- (b) Find BT
- (c) Find AB (matrix multiplication). Compare with simple multiplication (using * instead of @ in Python). Can you see what is the difference?
- (d) Find ABT
- (e) Compare ABT and BT AT
- (f) Find (AT )T
- (g) Find AAT
Task 2
Given
(a) Find AB (b) Find BA
2 1
A=32 B=34
Confirm that they are different! Clearly, when doing matrix multiplication, order matters! AB ̸= BA, so matrix multiplication is not commutative.
Task 3
a b TheinverseofamatrixA= c d isfoundby
−1 1d−b
A =ad−bc −c a (1)
As seen in listing 1, the inverse of a matrix can be found easily with numpy (after having imported numpy.linalg.inv) by: inv(A).
Using the same matrices from Task 2:
(a) Find A−1
(b) Find B−1
(c) Find AA−1. Look closely at the result. (d) Find A−1A. Look closely at the result. (e) Find BB−1. Look closely at the result.
(f) Find B−1B. Do you start to see a pattern?
It appears that a matrix multiplied with its inverse always gives 0 1 .
Incidentally, a matrix with only ones in the diagonal is called an identity matrix, often denoted I.
2
1 2
1 0
Task 4
Given
(a) Find A−1
2 4 A=12
Oops. We see that not all matrices have an inverse! Looking at equation 1 (the equation for finding the inverse), can you figure out why? (hint: look at the denominator!)
Task 5
Plotting (lines, graphs, coordinates, etc) can be done using matplotlib. Try the following:
- 1 import numpy as np
- 2 from matplotlib import pyplot as plt
- 3 from numpy.linalg import inv
4
- 5 xs = np.array([0,0,3,3,0,1.5,3]) # List of x coordinates
- 6 ys = np.array([1,0,0,1,1,2 ,1]) # List of y coordinates
7
- 8 fig = plt.figure()
- 9 fig, ax = plt.subplots()
10
- 11 xs_ys = np.array([xs,ys])
- 12 ax.axis(’equal’)
13
- 14 # Plot the points
- 15 ax.plot( *xs_ys, ’-’, color=’b’)
16
- 17 # Create a rotation matrix
- 18 rot = np.array([[1, 0],[0, 1]]) # <– CHANGE THIS
19
- 20 # turn the two lists (xs, ys) into a list of (x,y) tuples
- 21 points = np.array([[x,y] for x,y in zip(xs,ys)])
22
- 23 # Make the transformation:
- 24 points_rot = (points @ rot)
25
- 26 #Turnitintoarowofxsandarowofys:
- 27 xs_ys_rot = np.array([points_rot[:,0], points_rot[:,1]])
28
- 29 # Finally, plot it
- 30 ax.plot( *xs_ys_rot, ’-’, color=’r’)
- 31 fig
(a) Settherotationmatrix(line18)torotatetheshape45degrees(π4 radians).
3


