Lab 10 : Basic matrix operations¶
In this lab, we will write programs to carry out various matrix operations. We will implement matrices using lists of lists of numbers. For example,
will be implemented by
[[1, 2, 3], [4, 5, 6]]
We view each list of numbers as a row of the matrix. We assume that every list of numbers (row) in a list of list of numbers (matrix) has the same length. We also assume that every matrix has at least one row and one column. You need not check for correct inputs when you write your programs.
0 Preliminaries¶
To run doctests, add the following to the bottom of your file:
if (__name__ == '__main__'):
import doctest
doctest.testmod(optionflags= doctest.NORMALIZE_WHITESPACE)
1 Matrix shape¶
Exercise 1 Write a function that takes a matrix and returns the number of rows.
def num_rows(m):
"""
>>> num_rows([[1, 2, 3], [4, 5, 6]])
2
"""
Exercise 2 Write a function that takes a matrix and returns the number of columns.
def num_cols(m):
"""
>>> num_cols([[1, 2, 3], [4, 5, 6]])
3
"""
Exercise 3 Write a function that takes a matrix and returns shape of the matrix: e.g., the shape of a \(2\times 3\) matrix is (2, 3)
.
def shape(m):
"""
>>> shape([[1, 2, 3], [4, 5, 6]])
(2, 3)
"""
Exercise 4 Write a function which determines whether or not two matrices may be multiplied.
def can_be_multiplied(m1, m2):
"""
>>> can_be_multiplied([[1, 2, 3], [4, 5, 6]], [[1, 0], [2, 1], [1, 0]])
True
>>> can_be_multiplied([[1, 2], [4, 5]], [[1, 0], [2, 1], [1, 0]])
False
"""
Exercise 5 Write a function which outputs the shape of the matrix you would get if you multiplied the two given matrices in the order given. (Do not actually multiply the matrices.)
def prod_shape(m1, m2):
"""
>>> prod_shape([[1, 2, 3], [4, 5, 6]], [[1, 0], [2, 1], [1, 0]])
(2, 2)
"""
2 Matrix Addition¶
Recall that we add an \(m\times n\) matrix to another \(m\times n\) matrix by adding the corresponding entries.
Exercise 6 Write a function which adds two lists of numbers together element-wise.
def list_add(l1, l2):
"""
Assumes len(l1) == len(l2).
>>> list_add([1, 2, 3], [4, 5, 6])
[5, 7, 9]
"""
Exercise 7 Write a function which adds two matrices together. Use list_add
.
def matrix_add(m1, m2):
"""
Assumes shape(m1) == shape(m2)
>>> matrix_add([[1, 2, 3], [4, 5, 6]], [[1, 0, 1], [0, 1, 0]])
[[2, 2, 4], [4, 6, 6]]
"""
3 Matrix Multiplication¶
An \(m\times n\) matrix A on the left can be multiplied by a \(n\times p\) matrix B on the right to produce an \(m\times p\) matrix C, where
In other words, the (i,j)-th entry of C is obtained by multiplying the ith row vector of A on the left with the jth column vector of B on the right.
Exercise 8 Write a function which multiplies two lists of numbers element-wise and then sums the result. (If you view these lists of numbers as vectors, then this operation is the dot product.)
def mult_and_sum(l1, l2):
"""
Assumes len(l1) = len(l2).
>>> mult_and_sum([1, 2, 3], [4, 5, 6])
32
"""
Exercise 9 Write a function which extracts the jth column of a matrix as a list of numbers.
def jth_col_to_list(m, j):
"""
>>> jth_col_to_list([[1, 2, 3], [4, 5, 6]], 0)
[1, 4]
"""
Exercise 10 Write a function which multiplies a list of numbers on the left with each column of a matrix on the right. Use mult_and_sum
and jth_col_to_list
and num_cols
.
def list_times_matrix(l , m):
"""
>>> list_times_matrix([1, 2, 3], [[1, 0], [2, 1], [1, 0]])
[8, 2]
"""
Exercise 11 Write a function which multiplies two matrices. Use list_times_matrix
.
def matrix_mult(m1, m2):
"""
>>> matrix_mult([[1, 2, 3], [4, 5, 6]], [[1, 0], [2, 1], [1, 0]])
[[8, 2], [20, 5]]
"""
4 Transpose¶
Exercise 12 Write a function which transposes a matrix. Use jth_col_to_list
and num_cols
.
def transpose(m):
"""
>>> transpose([[1, 2, 3], [4, 5, 6]])
[[1, 4], [2, 5], [3, 6]]
"""
5 Identity matrix¶
Challenge Write a function which outputs the \(n\times n\) identity matrix.
def I(n):
"""
>>> I(3)
[[1, 0, 0], [0, 1, 0], [0, 0, 1]]
"""
6 Pretty-printing¶
Challenge 2 Write a function which prints a matrix.
def print_matrix(matrix):
"""
>>> print_matrix([[1,2,3],[4,5,6]])
1 2 3
4 5 6
"""