1.1 Scalars and Vectors


: 30 minutes

This section introduces the two smallest objects in linear algebra—the scalar and the vector—and their counterparts in Python code.

Scalars

A scalar is a single (real) value, denoted by ordinary lower-cased letters (e.g., x, y, and z). We write \mathbb{R} for the space of all real-valued scalars, so that x\in\mathbb{R} says “x is a real number”.

Scalars are represented in Python by an int or a float. For example, let weight record a weight in pounds.

Python Scalars

Python offers four basic scalar types: int (whole numbers), float (decimals), bool (True/False), and str (text). Only the first two are numbers in the sense of linear algebra, though bool quietly behaves like 0 and 1 in arithmetic—a fact that becomes useful when we start counting how many rows satisfy a condition.

The function isinstance reports whether a value has a given type.

A float stores only an approximation of a decimal number, which makes exact comparison unreliable.

Note Python Equality

What is the Python output from 0.1 + 0.1 + 0.1 == 0.3?

This is because the face value 0.1 is represented by Python as a much longer floating number. Therefore, their sum does not match the float representation of the face value 0.3.

Because of the above, never test two floats with ==. Test instead that they are close, as in abs(a - b) < 1e-9. You will see this idiom in every graded exercise in these notes.

Vectors

In real applications it usually makes more sense to group scalars together as a vector than to let them float around as seemingly unrelated variables.

For example, in a hypothetical clinical study of BMI, the scalar measurements of one respondent—weight, height, and age—are naturally collected into a single feature vector.

A vector is a fixed-length array of scalars, called the elements (or entries, or components) of the vector. We denote vectors by bold lowercase letters, e.g., \bold{x}, \bold{y}, \bold{z}.

The standard way to visualize a vector is by vertically stacking its elements:

\bold{x} =\begin{bmatrix}x_{1} \\ \vdots \\x_{n}\end{bmatrix}. \tag{1.1}

We refer to an element of a vector using a subscript. For example, x_2 denotes the second element of \bold{x}. Since x_2 is a scalar, we do not bold it.

If a vector \bold{x} contains n elements, we use the notation \bold{x}\in\mathbb{R}^n. Here, n is called the dimension or size of the vector.

The order of the entries is part of the data. [178, 69, 46] and [69, 178, 46] are different vectors, and confusing them is how a model ends up predicting that a 46-inch-tall person weighs 69 pounds. A vector is a list of numbers plus an agreed-upon meaning for each slot.

TipRow vs Column Vectors

In linear algebra we sometimes distinguish between the column vectors above and row vectors, whose elements are stacked horizontally: \bold{x} =\begin{bmatrix}x_{1}, \ldots, x_{n}\end{bmatrix}. Unless we say otherwise, “vector” in these notes means a column vector. The distinction only starts to matter in 1.4 Matrix Operations, where it decides which matrix products are legal.

Note US States

Let \bold{x} denote the vector containing the number of counties in each of the states in the US. Which of the following must be true.

There are only 50 states in the USA.

Vectors in Python

In plain Python, the closest counterpart of a vector is a list.

Warning0- vs 1-based Indexing

In Python, as in most programming languages, indices start at 0 (zero-based indexing), whereas linear-algebra subscripts begin at 1 (one-based indexing). The entry that a mathematician calls x_2 is written x[1] in code.

Slicing extracts a run of consecutive entries. The slice x[a:b] includes position a and excludes position b, so x[0:2] returns the first two entries.

A list is a fine container for a vector, but it is not a vector: Python’s arithmetic operators mean something else entirely on lists. The next two exercises are about exactly that gap, which NumPy will close for us next week.

Note List vs Vector

In Python, x = [1, 2, 3] and y = [4, 5, 6]. What does x + y evaluate to, and how does it relate to the vector sum \bold{x}+\bold{y}?

Python’s + on two lists glues them end to end, producing the six-element list [1, 2, 3, 4, 5, 6]. The linear-algebra sum \bold{x}+\bold{y}=[5,7,9] is element-wise and requires the two vectors to have equal length.

This is the central caution of this section: a list stores the numbers of a vector, but it does not behave like a vector. The same trap appears with *, where x * 2 repeats the list instead of doubling each entry. NumPy arrays, introduced next week, are the objects that finally make + and * mean what a mathematician expects.

Exercises

Note It Runs, and It Is Still Wrong

heights_in holds four heights in inches. A classmate wanted to double every height and wrote heights_in * 2. The code raised no error, and the answer is nonetheless wrong.

Run their line first to see what actually happens, then produce heights_cm, the list of the same four heights converted to centimetres (1 inch =2.54 cm).

heights_cm = [2.54 * h for h in heights_in]

Note Translating a Subscript

The vector \bold{x}\in\mathbb{R}^5 below is stored in x. Using a single slice, produce the sub-vector (x_2, x_3, x_4)—the second, third, and fourth entries in the mathematician’s one-based numbering.

sub = x[1:4]