Matrices and their products
Matrices and matrix multiplication (lesson 3.2.1)
0 of 6 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Matrices as maps
Matrices as linear maps (lesson 3.2.2)
0 of 7 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Transpose, identity and inverse
Transpose, identity and inverse (lesson 3.2.3)
0 of 8 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Coding exercise
Matrix multiplication and the 2 × 2 inverse
Write two functions. Matrices are lists of rows, so is [[2, 1], [-1, 3]].
matmul(A, B)returns as a list of rows, built from the rule of lesson 3.2.1: entry is row ofAdotted with column ofB. Do not use NumPy's@. RaiseValueErrorif the rows ofAare not as long as the columns ofB.inverse_2x2(A)returns the inverse of a matrix by the formula of lesson 3.2.3. RaiseValueErrorif .
The tests try square and rectangular matrices, both orders of the same pair, a row times a column, and a pair whose shapes do not match. For the inverse they try whole numbers, decimals, a rotation and two matrices with no inverse.
See the tests
import math
import numpy as np
def test_square():
"""[[2, 1], [−1, 3]] times [[1, 0], [2, 1]] is [[4, 1], [5, 3]]"""
expect_close(matmul([[2, 1], [-1, 3]], [[1, 0], [2, 1]]), [[4, 1], [5, 3]], what="matmul(A, C)")
def test_order_matters():
"""The same two matrices in the other order give [[2, 1], [3, 5]]"""
expect_close(matmul([[1, 0], [2, 1]], [[2, 1], [-1, 3]]), [[2, 1], [3, 5]], what="matmul(C, A)")
def test_rectangular():
"""A 2 × 3 matrix times a 3 × 2 matrix gives a 2 × 2 matrix"""
expect_close(matmul([[1, 2, 0], [3, -1, 4]], [[1, 0], [2, 1], [0, 3]]), [[5, 2], [1, 11]], what="matmul(M, N)")
def test_other_shape():
"""The same pair the other way round gives a 3 × 3 matrix"""
expected = [[1, 2, 0], [5, 3, 4], [9, -3, 12]]
expect_close(matmul([[1, 0], [2, 1], [0, 3]], [[1, 2, 0], [3, -1, 4]]), expected, what="matmul(N, M)")
def test_column():
"""A matrix times a column: 6 chairs and 2 tables need 32 legs, 72 screws and 12 boards"""
expect_close(matmul([[4, 4], [8, 12], [1, 3]], [[6], [2]]), [[32], [72], [12]], what="matmul(P, [[6], [2]])")
def test_row_times_column():
"""A row times a column is a 1 × 1 matrix, the dot product: [[5]]"""
expect_close(matmul([[3, 1]], [[1], [2]]), [[5]], what="matmul([[3, 1]], [[1], [2]])")
def test_matches_numpy():
"""Random 4 × 3 and 3 × 5 matrices agree with NumPy's @"""
rng = np.random.default_rng(3)
A = rng.integers(-5, 6, size=(4, 3))
B = rng.integers(-5, 6, size=(3, 5))
expect_close(matmul(A.tolist(), B.tolist()), A @ B, what="matmul of a 4 × 3 and a 3 × 5 matrix")
def test_shape_mismatch():
"""Two 2 × 3 matrices cannot be multiplied: ValueError"""
try:
matmul([[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]])
except ValueError:
return
raise AssertionError("expected ValueError: rows of length 3 cannot meet columns of length 2")
def test_inverse_whole_numbers():
"""The inverse of [[2, 1], [1, 1]] is [[1, −1], [−1, 2]]"""
expect_close(inverse_2x2([[2, 1], [1, 1]]), [[1, -1], [-1, 2]], what="inverse_2x2([[2, 1], [1, 1]])")
def test_inverse_decimals():
"""The inverse of [[4, 2], [1, 3]] is [[0.3, −0.2], [−0.1, 0.4]]"""
expect_close(inverse_2x2([[4, 2], [1, 3]]), [[0.3, -0.2], [-0.1, 0.4]], what="inverse_2x2([[4, 2], [1, 3]])")
def test_inverse_undoes():
"""[[2, 1], [−1, 3]] times its inverse is the identity, in both orders"""
A = np.array([[2, 1], [-1, 3]])
inv = np.asarray(inverse_2x2([[2, 1], [-1, 3]]), dtype=float)
expect_close(A @ inv, np.eye(2), what="A times your inverse")
expect_close(inv @ A, np.eye(2), what="your inverse times A")
def test_rotation():
"""The inverse of the 30° rotation is the rotation by −30°"""
t = math.radians(30)
R = [[math.cos(t), -math.sin(t)], [math.sin(t), math.cos(t)]]
back = [[math.cos(t), math.sin(t)], [-math.sin(t), math.cos(t)]]
expect_close(inverse_2x2(R), back, what="inverse of the 30° rotation")
def test_singular():
"""[[1, 2], [0.5, 1]] has ad − bc = 0, so it has no inverse: ValueError"""
try:
inverse_2x2([[1, 2], [0.5, 1]])
except ValueError:
return
raise AssertionError("expected ValueError: ad − bc = 1 − 2 × 0.5 = 0")
def test_zero_matrix():
"""The zero matrix has no inverse: ValueError"""
try:
inverse_2x2([[0, 0], [0, 0]])
except ValueError:
return
raise AssertionError("expected ValueError for the zero matrix")