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