Skip to content

Exercises

These are meant to be done as you go. If you get stuck, check the hints (or jump to Solutions).

Exercise 1 — Matrix multiply from Python

Expose a function matmul(A, B) in C++ and call it from Python with NumPy arrays.

Success criteria:

  • result matches A @ B in Python (within tolerance)

Hint:

  • start with Eigen::MatrixXd matmul(Eigen::MatrixXd A, Eigen::MatrixXd B).

Exercise 2 — Avoid copies (conceptual)

Create a NumPy array view: A[:, ::2] (non-contiguous) and pass it to your binding.

Success criteria:

  • you can explain whether a copy is likely happening and why.

Hint:

  • inspect A.flags.

Exercise 3 — Input validation

Check that A and B have compatible shapes and raise an exception otherwise.