Starting from:

$30

DataScience - Assignment 3 - Solved

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):

import numpy as np from numpy.linalg import inv

# Creating matrices

A   = np.array([[ 1, 2 ],[ 3, 4]])

B   = np.array([[ 9, 8 ],[ 7, 6]])

# Transposing:

A.T # A transposed (danish: A transponeret)

B.T # B transposed

# Matrix multiplication:

A @ B

# Inverse:

inv(A)
1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

Figure 1: Basic matrix functionality with numpy.

In the following, when talking about multiplication, we implicitly mean matrix multiplication (same as dot product).

Task 1
Given the two following matrices

 

(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 BTAT

(f)     Find (AT)T

(g)    Find AAT

Task 2
Given

 

(a)    Find AB

(b)    Find BA

Confirm that they are different! Clearly, when doing matrix multiplication, order matters! AB 6= BA, so matrix multiplication is not commutative.

Task 3
The inverse of a matrix   is found by

                                                                                                          (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  .

Incidentally, a matrix with only ones in the diagonal is called an identity matrix, often denoted I.

Task 4
Given

 

(a) Find A−1

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:

import numpy as np from matplotlib import pyplot as plt from numpy.linalg import inv

xs = np.array([0,0,3,3,0,1.5,3]) # List of x coordinates ys = np.array([1,0,0,1,1,2 ,1]) # List of y coordinates

fig = plt.figure() fig, ax = plt.subplots()

xs_ys = np.array([xs,ys]) ax.axis(’equal’)

# Plot the points ax.plot( *xs_ys, ’-’, color=’b’)

# Create a rotation matrix rot = np.array([[1, 0],[0, 1]]) # <-- CHANGE THIS

# turn the two lists (xs, ys) into a list of (x,y) tuples points = np.array([[x,y] for x,y in zip(xs,ys)])

# Make the transformation: points_rot = (points @ rot)

# Turn it into a row of xs and a row of ys:

xs_ys_rot = np.array([points_rot[:,0], points_rot[:,1]])

# Finally, plot it ax.plot( *xs_ys_rot, ’-’, color=’r’) fig
1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

(a) Set the rotation matrix (line 18) to rotate the shape 45 degrees (  radians).

More products