Exercise 1.1¶

Open this file (ch1-exercises.ipynb) in VS Code.

Familiarise yourself with the VS Code editor. You can read https://code.visualstudio.com/docs/getstarted/userinterface alongside, or watch the YouTube video https://youtube.com/live/B5cZhBUpocQ.

  • On the left, you see a vertical bar (Activity bar) with several icons. Hover over the icons and click on them to see what they correspond to. For now, you will mainly need to learn about using Explorer, Search, and Extensions. Note that when you click repeatedly on an icon, you can make the Activity Bar disappear or bring them back.

  • Next the the Activity Bar is the Explorer (when selected), which lists all the folders files in the currently opened directory (also called workspace). Right click on a file or folder, or on the empty space to see what options pop up.

  • The biggest screen space is occupied by the Editor, which you use to edit your files and execute them. When a Jupyter Notebook is opened, watch out in particular for the horizontal buttons on the top showing + Code + Markdown >> Run All

  • On the top you find the Menu bar (File, Edit, etc.) and next to it the Search bar and command palette. The command palette is accessed using Ctrl + Shift + P. Start typing and you'll see the available options.

  • Hitting Ctrl + J will toggle the bottom panel which shows the Output and Debug Console, as well the Terminal and Jupyter tab. The terminal is where you can access the command line to install Python packages, and the Jupyter tab lists the variables which are currently in memory.

Exercise 1.2¶

Modify this markdown cell by double clicking on it. Here is an example of how to typeset a mathematical definition.

Definition: The nullspace of a matrix $A\in\mathbb{C}^{m\times n}$ is defined as the set of all vectors $x\in\mathbb{C}^n$ that satisfy $Ax = 0$. We denote this as $$ \text{null}(A) := \{ x\in\mathbb{C}^n\, : \, Ax = 0 \}. $$

Now extend this cell with your favourite mathematical theorem below. Use LaTeX notation with $ ... $ and $$ ... $$. When done, hit Shift + Enter to see it displayed.

Theorem: TODO

Exercise 1.3¶

Insert a code cell below that defines a function mean that takes as input a list or NumPy array $x$ and returns the mean of the entries. You can use this code snippet as a starting point:

import numpy as np

def mean(x):
    """Returns the mean of the entries of x."""
    # TODO
    return

Notes:

  • If you get an error No module named 'numpy', you need to install the NumPy package. Open the terminal (Ctrl + J) and type pip install numpy (or if this fails, python -m pip install numpy).

  • If you have already created a virtual environment, double check that it is actually being used to run your Jupyter Notebooks. You should see a kernel indicating .venv in the top-right corner of the notebook. If this merely displays 'Python 3.xx.x' then click on it and select the .venv interpreter.

  • The default powershell terminal does not (always) display the virtual environment that it is using. This is a known problem. You can check whether or not you are using the virtual environment by typing in the terminal:

    pip -V

    If the path displayed there does not point to the MAML\.venv folder, then you need to activate the .venv manually. This is done by typing

    .venv\Scripts\activate

    When working on Windows, the standard Command Prompt (cmd) does a better job. You can change your default terminal to the Command Prompt (cmd). Search for Terminal: Select Default Profile in the command palette.

In [2]:
# Testing
print(mean([1, 7, -2])) # should print 2.0
print(mean(np.array([1, 7, -2])))
2.0
2.0

Exercise 1.4¶

Familiarise yourself with the Jupyter command bar shown on top of the editor window. Try out in particular the Run All, Restart, Clear All Outputs, and Variables buttons. If you ever need to send a Jupyter Notebook to someone (e.g., as a coursework submission), it is strongly recommended to

  1. Restart the kernel in order to clear all variables in memory

  2. Run All cells to confirm that everything executes without errors