Skip to content
Tutorials Center
My learning

Topics

Lesson 57 of 62 ยท Practice and Review

Practice: Vectors and Matrices

Twelve linear algebra exercises on vectors, norms, dot and cross products, matrix building, multiplication and transposes. Every solution runs live in NumPy.

You learn linear algebra by doing small computations until the ideas stop feeling abstract. This page has twelve exercises that cover the first half of the track: vectors, lengths, dot and cross products, and matrices with their products and transposes. Each exercise states a task and then gives a runnable solution. The solutions use assert lines, so if you change the code in a way that breaks the result, the run fails loudly.

How to use this page

Do not run the solution first. Read the task, then try it on paper or in your own scratch editor. Check your answer against the output, and look at the solution only to compare approaches. Three habits make most of these exercises easy:

  1. Write the shapes down before you write any code.
  2. Predict the result (even roughly) before you run it, so surprises teach you something.
  3. Verify with an identity. Many results can be checked: a unit vector has length 1, a normal vector is perpendicular to its inputs, and (AB)^T = B^T A^T.

A tiny problem-solving routine

Shapes Write the input and output shapes first.
Predict Guess the answer before running.
Verify Check with an identity or a tiny case.
1 / 2

Part 1: Vectors

Related lessons: Vectors and Length, Distance and Unit Vectors.

Exercise 1. Given a = (1, 2, 3) and b = (4, 0, -1), compute 3a - 2b and the vector halfway between a and b.

Python Exercise 1: vector arithmetic

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 2. Find the L2 length, the L1 length and the max norm of v = (3, 4, 12). Then normalize v to a unit vector and verify its length.

Python Exercise 2: norms and unit vectors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 3. The points P = (1, 1) and Q = (4, 5) are the corners of a right triangle with the legs parallel to the axes. Find the Euclidean distance between them and the "taxicab" (city-block) distance, which is the L1 norm of the difference.

Python Exercise 3: two distances

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 4. Find the angle in degrees between u = (1, 0, 1) and w = (0, 1, 1), using the dot product.

Python Exercise 4: angle between vectors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 5. Project a = (2, 3) onto the line through b = (4, 0). Check that what is left over, a - projection, is perpendicular to b.

Python Exercise 5: projection onto a line

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 6. A triangle has corners A = (0, 0, 0), B = (2, 0, 0) and C = (0, 3, 0). Use the cross product to find a normal vector to the triangle and its area.

Python Exercise 6: normal vector and area

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Part 2: Building and combining matrices

Related lessons: Matrices, Matrix Addition and Scalar Multiplication and Matrix Multiplication.

Exercise 7. Build these four matrices without typing every entry by hand: the 4 by 4 identity, a diagonal matrix with entries 1, 2, 3, 4, an upper triangular matrix of the numbers 1 to 16 (everything below the diagonal zeroed), and a symmetric matrix made from the last one.

Python Exercise 7: special matrices

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 8. Let A be 2 by 3 and B be 3 by 2. Predict the shapes of A @ B and B @ A, compute both, and show that matrix multiplication is not commutative.

Python Exercise 8: shapes and non-commutativity

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 9. For M = [[2, 1], [0, 3]] and N = [[1, 4], [2, 0]], show that M * N (elementwise) and M @ N (matrix product) are different things, and verify that (M @ N).T equals N.T @ M.T.

Python Exercise 9: * versus @ and the transpose rule

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 10. Take A = [[1, 2], [3, 4], [5, 6]] and x = (10, 1). Compute A @ x three ways: with the dot product of each row, as a combination of the columns, and with @. Confirm that the answers are identical.

Python Exercise 10: three views of A @ x

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 11. For any matrix A, the product A.T @ A is symmetric. Verify this for a random 5 by 3 matrix, and also check that its diagonal entries equal the squared lengths of the columns of A.

Python Exercise 11: A transpose A

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 12. Write a function is_rotation(R) that returns True when a 2 by 2 matrix is a proper rotation: R.T @ R is the identity and the determinant is +1. Test it on a rotation by 40 degrees, on a reflection and on a scaling.

Python Exercise 12: recognize a rotation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. Which expression gives the cosine of the angle between vectors u and w?

    The dot product equals |u||w|cos(theta), so dividing it by the two lengths leaves the cosine.
  2. 2. A is 4 by 3 and B is 3 by 5. What is the shape of A @ B, and is B @ A defined?

    The inner dimensions (3 and 3) match, so A @ B is 4 by 5. For B @ A the inner dimensions would be 5 and 4, which do not match.
  3. 3. What is (A @ B).T equal to?

    The transpose of a product reverses the order of the factors: (AB)^T = B^T A^T.

Summary

  • Norms, unit vectors, distances and angles all come from the dot product and np.linalg.norm.
  • The cross product gives a perpendicular vector, and half its length is a triangle's area.
  • Build special matrices with np.eye, np.diag, np.triu and transposes instead of typing entries.
  • * is elementwise; @ is the matrix product, which depends on shapes and order.
  • A.T @ A is always symmetric, and (AB)^T = B^T A^T.

Next: Practice: Systems and Vector Spaces.