1.1 Scalars and Vectors


: 30 minutes

This subsection defines the mathematical concepts behind vectors, matrices, tensors, and their codding counterparts.

Scalars

A scalar is a single (real) value, denoted by ordinary lower-cased letters (e.g., x, y, and z) and the space of all (continuous) real-valued scalars by \mathbb{R}. Consequently, if x is a real-valued scalar, we use the notation x\in\mathbb{R}.

Scalars are represented in Python by an int or float. For example, let x represent my weight in pounds.

Python Scalars

Python offers four basic data types: int, float, bool, and str.

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.

Vectors

In real-world applications, it oftentimes make more sense to group scalars together as a vector than letting them float around as seemingly unrelated variables.

For example, in a hypothetical clinical study of BMI, scalar measurements—like weight, height, and age—of an individual respondent can be put into to a feature vector for easer data manipulation.

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

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 can refer to an element of a vector by using a subscript. For example, x_2 denotes the second element of \mathbf{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.

TipRow vs Column Vectors

In linear algebra, we sometimes distinguish between such column vectors and row vectors whose elements are stacked horizontally. A row vector is written as follows: \bold{x} =\begin{bmatrix}x_{1}, \ldots, x_{n}\end{bmatrix}.

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 Python, the code counterpart of a vector is simply a list.

Warning0- vs 1-based Indexing

In Python, as in most programming languages, vector indices start at 0, also known as zero-based indexing, whereas in linear algebra subscripts begin at 1 (one-based indexing).