Determinants
Determinants as volume scaling (lesson 3.4.1)
0 of 14 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
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
A determinant by cofactor expansion
Write det(A), the determinant of a square matrix A given as a list of rows, each a list of numbers. Expand along row 1, as in lesson 3.4.1:
- A matrix
[[a]]has determinanta. - Otherwise, for each column , delete row 1 and column , and find the determinant of what is left with
detitself. Add up the entries of row 1 times these minors, with signs - Raise
ValueErrorifAis empty or not square.
The tests use the matrices of lesson 3.4.1, a row swap, a matrix and a triangular one. With whole-number entries your answer is exact: for lesson 3.4.1's it must be exactly 0, where NumPy prints .
The work grows very fast with the size: a matrix has 3,628,800 terms. That is why NumPy eliminates instead.
See the tests
import numpy as np
def test_two_by_two():
"""det([[3, 1], [1, 2]]) is 3·2 − 1·1 = 5"""
expect_equal(det([[3, 1], [1, 2]]), 5, what="det([[3, 1], [1, 2]])")
def test_one_by_one():
"""A 1 × 1 matrix [[7]] has determinant 7"""
expect_equal(det([[7]]), 7, what="det([[7]])")
def test_three_by_three():
"""Lesson 3.4.1's C = [[2, 1, 3], [0, 4, 1], [1, 0, 2]] has determinant 5"""
expect_equal(det([[2, 1, 3], [0, 4, 1], [1, 0, 2]]), 5, what="det(C)")
def test_box():
"""Lesson 3.4.1's box B = [[2, 1, 1], [0, 2, 1], [0, 0, 2]] has volume 8"""
expect_equal(det([[2, 1, 1], [0, 2, 1], [0, 0, 2]]), 8, what="det(B)")
def test_row_swap():
"""Swapping rows 1 and 3 of C flips the sign: −5"""
expect_equal(det([[1, 0, 2], [0, 4, 1], [2, 1, 3]]), -5, what="det(C with rows 1 and 3 swapped)")
def test_singular_exact():
"""Lesson 3.4.1's S = [[1, 3, 4], [3, 1, 4], [1, 6, 7]] gives exactly 0"""
expect_equal(det([[1, 3, 4], [3, 1, 4], [1, 6, 7]]), 0, what="det(S), which NumPy prints as 7.5e-15")
def test_four_by_four():
"""A 4 × 4 matrix matches NumPy: [[1, 2, 0, 1], [0, 3, 1, 2], [2, 0, 1, 1], [1, 1, 0, 3]] gives 17"""
A = [[1, 2, 0, 1], [0, 3, 1, 2], [2, 0, 1, 1], [1, 1, 0, 3]]
expect_close(det(A), float(np.linalg.det(np.array(A, dtype=float))), what="det of the 4 × 4 matrix", rel=1e-9)
expect_equal(det(A), 17, what="det of the 4 × 4 matrix")
def test_triangular():
"""A triangular 4 × 4 matrix with diagonal 2, −1, 3, 0.5 gives their product, −3"""
A = [[2, 2, 3, 4], [0, -1, 7, 8], [0, 0, 3, 12], [0, 0, 0, 0.5]]
expect_close(det(A), -3.0, what="det of the triangular matrix")
def test_not_square():
"""A 2 × 3 matrix has no determinant: ValueError"""
try:
det([[1, 2, 3], [4, 5, 6]])
except ValueError:
return
raise AssertionError("expected ValueError for a 2 × 3 matrix")
def test_ragged():
"""Rows of different lengths, [[1, 2], [3]], are not a square matrix: ValueError"""
try:
det([[1, 2], [3]])
except ValueError:
return
except IndexError:
raise AssertionError("expected ValueError for rows of different lengths, got IndexError")
raise AssertionError("expected ValueError for rows of different lengths")
def test_empty():
"""An empty matrix [] has no determinant: ValueError"""
try:
det([])
except ValueError:
return
raise AssertionError("expected ValueError for an empty matrix")