From the free course Applied Linear Algebra and Least Squares on Lykke
Vector Fundamentals and Inner Products
Key concepts: Scalar-vector multiplication · Unit vector · Sparse vector · Inner product · Complexity (flops)
Covers basic vector notation, scalar-vector multiplication, addition, and the definition of the inner product.
Vector Fundamentals and Inner Products
In the landscape of modern computational science, the vector is the fundamental atom of data. Whether representing a point in physical space, a sequence of stock prices over time, or the high-dimensional embedding of a word in a large language model, vectors provide a unified mathematical language for describing the world. This article explores the foundational mechanics of vectors, moving from basic arithmetic to the inner product—the engine that powers nearly all of machine learning and signal processing.
Vector Basics: The Geometry of Data
A vector is an ordered list of $n$ numbers, typically written as a column. In the context of applied linear algebra, we denote a vector $x$ as belonging to the set $\mathbb{R}^n$, where $n$ is the dimension or size of the vector. The individual numbers within the vector, $x_1, x_2, \dots, x_n$, are referred to as its entries, components, or elements.
What it is
Mathematically, a vector $x$ is defined as: $$x = \begin{bmatrix} x_1 \ x_2 \ \vdots \ x_n \end{bmatrix}$$ While we often visualize vectors as arrows in 2D or 3D space, in high-dimensional applications (where $n$ might be millions), we treat them as abstract points in an $n$-dimensional manifold.
Why it matters
Vectors allow us to group related quantities into a single mathematical object. This "chunking" of data enables us to perform operations on entire datasets simultaneously rather than iterating through individual numbers. In engineering, a vector might represent the state of a system (position, velocity, acceleration); in economics, it might represent a portfolio of assets.
Common Vector Notations and Types
| Term | Notation | Description |
|---|---|---|
| Dimension | $n$ | The number of elements in the vector. |
| $i$-th element | $x_i$ | The scalar value at position $i$. |
| Block Vector | $[a; b]$ | A vector formed by stacking vectors $a$ and $b$. |
| Transpose | $x^T$ | The row-vector representation of a column vector. |
Scalar-Vector Multiplication
Scalar-vector multiplication is the process of scaling a vector by a single real number (a scalar).
What it is
Given a scalar $\alpha$ and a vector $x$, the product $\alpha x$ is a vector of the same dimension as $x$, where each entry is multiplied by $\alpha$: $$\alpha x = \begin{bmatrix} \alpha x_1 \ \alpha x_2 \ \vdots \ \alpha x_n \end{bmatrix}$$
How it works
This operation is "element-wise." If you have a vector representing a physical force and you double the magnitude of that force without changing its direction, you are performing scalar-vector multiplication where $\alpha = 2$.
The Scaling Property: Scalar multiplication changes the magnitude of the vector but preserves its direction (unless $\alpha$ is negative, in which case the direction is exactly reversed).
Concrete Example
Consider a 3D velocity vector $v = [10, -2, 5]^T$ representing meters per second. To convert this to kilometers per hour, we multiply by the scalar $\alpha = 3.6$: $$3.6 \times \begin{bmatrix} 10 \ -2 \ 5 \end{bmatrix} = \begin{bmatrix} 36 \ -7.2 \ 18 \end{bmatrix}$$
Common Pitfalls
- Dimension Confusion: Beginners sometimes confuse scalar-vector multiplication with the inner product. Remember: Scalar-vector multiplication results in a vector; the inner product results in a scalar.
- Zero Scaling: Multiplying any vector by the scalar $0$ results in the zero vector ($0$), not the scalar $0$.
Vector Addition and Subtraction
Vector addition is the primary method for combining data streams or shifting points in space.
What it is
Two vectors $x$ and $y$ can be added if and only if they have the same dimension $n$. The sum $x+y$ is a vector where each entry is the sum of the corresponding entries: $$x + y = \begin{bmatrix} x_1 + y_1 \ \vdots \ x_n + y_n \end{bmatrix}$$
Why it matters
Addition represents the "superposition" of effects. In physics, if two forces act on an object, the resulting force is the vector sum. In data science, adding a "bias" vector to a "feature" vector shifts the data in a specific direction in the feature space.
Properties of Vector Addition
Vector addition follows the same fundamental laws as real-number addition, which makes it highly intuitive for algebraic manipulation.
| Property | Formula | Description |
|---|---|---|
| Commutativity | $x + y = y + x$ | Order of addition does not matter. |
| Associativity | $(x + y) + z = x + (y + z)$ | Grouping does not matter. |
| Additive Identity | $x + 0 = x$ | Adding the zero vector leaves $x$ unchanged. |
| Distributivity | $\alpha(x + y) = \alpha x + \alpha y$ | Scaling a sum is the same as summing the scaled parts. |
Special Vectors
In linear algebra, certain vectors appear so frequently that they are given standard names and symbols. Recognizing these is crucial for reading technical literature.
1. The Zero Vector ($0$)
A vector where all entries are zero. It acts as the origin in $n$-dimensional space.
2. The Ones Vector ($1$)
A vector where all entries are 1. This is frequently used to calculate sums or averages. For example, the sum of all elements in $x$ can be written as the inner product $1^T x$.
3. Unit Vectors ($e_i$)
The $i$-th standard unit vector $e_i$ is a vector where the $i$-th entry is 1 and all other entries are 0.
- In $\mathbb{R}^3$, $e_1 = [1, 0, 0]^T$, $e_2 = [0, 1, 0]^T$, and $e_3 = [0, 0, 1]^T$.
- These are the building blocks of any vector; any $x$ can be written as $x = x_1 e_1 + x_2 e_2 + \dots + x_n e_n$.
4. Sparse Vectors
A vector is sparse if most of its entries are zero.
Why Sparse Vectors Matter
In modern applications like natural language processing (NLP) or large-scale graph analysis, vectors can have dimensions in the billions, but only a few hundred entries might be non-zero. Storing these as dense arrays would be a catastrophic waste of memory.
- nnz(x): This notation represents the "number of non-zeros" in vector $x$.
- Efficiency: Computational libraries use specialized data structures (like compressed sparse rows) to only store and compute with the non-zero elements.
Insight: A vector with $1,000,000$ entries where only $10$ are non-zero is "mostly empty." Operations on such vectors should take time proportional to the number of non-zeros, not the total dimension.
The Inner Product
The inner product (also known as the dot product) is the most critical operation in vector calculus. It provides a way to multiply two vectors to produce a single scalar value.
What it is
The inner product of two vectors $a$ and $b$ of the same dimension is the sum of the products of their corresponding entries: $$a^T b = a_1 b_1 + a_2 b_2 + \dots + a_n b_n = \sum_{i=1}^n a_i b_i$$
Why it matters
The inner product measures "alignment."
- Similarity: If $a$ and $b$ point in the same direction, their inner product is large and positive.
- Orthogonality: If $a$ and $b$ are perpendicular, their inner product is $0$.
- Weighted Sums: If $w$ is a vector of weights and $x$ is a vector of features, $w^T x$ is the weighted sum, which is the core calculation of a neuron in a neural network.
Geometric Interpretation
While the algebraic definition is a sum of products, the geometric definition is: $$a^T b = |a| |b| \cos(\theta)$$ where $|a|$ is the length of the vector and $\theta$ is the angle between them. This connects the raw numbers in the vector to the physical concept of an angle.
Properties of the Inner Product
| Property | Formula | Name |
|---|---|---|
| Symmetry | $a^T b = b^T a$ | Commutative property of inner products. |
| Linearity | $(\gamma a)^T b = \gamma (a^T b)$ | Scaling one vector scales the result. |
| Distributivity | $(a + b)^T c = a^T c + b^T c$ | The inner product distributes over addition. |
| Sum of Squares | $x^T x = x_1^2 + \dots + x_n^2$ | The inner product of a vector with itself is the sum of squares. |
Computational Complexity (FLOPs)
In high-performance computing, we measure the "cost" of an operation in FLOPs (Floating Point Operations). A FLOP is typically one addition or one multiplication of two decimal numbers.
Complexity of Vector Operations
When a senior engineer optimizes a system, they look at the algorithmic complexity to ensure the system scales.
| Operation | Dimension | Multiplications | Additions | Total FLOPs | Complexity |
|---|---|---|---|---|---|
| Scalar Multiplication | $n$ | $n$ | $0$ | $n$ | $O(n)$ |
| Vector Addition | $n$ | $0$ | $n$ | $n$ | $O(n)$ |
| Inner Product | $n$ | $n$ | $n-1$ | $2n-1$ | $O(n)$ |
Why $2n-1$?
To compute $a^T b$:
- We perform $n$ multiplications ($a_1 b_1, a_2 b_2, \dots$).
- We perform $n-1$ additions to sum those $n$ products.
- Total: $n + (n-1) = 2n-1$.
Real-World Impact
On a modern processor, inner products are often accelerated using FMA (Fused Multiply-Add) instructions. An FMA instruction performs $a \times b + c$ in a single clock cycle. This effectively cuts the perceived time of an inner product in half, as the multiplication and addition happen simultaneously at the hardware level.
Concrete Example: Portfolio Valuation
Imagine you are managing a small investment portfolio.
- Vector $q$ (Quantities): The number of shares you own in three companies: Apple, Google, and Tesla. $$q = \begin{bmatrix} 100 \ 50 \ 20 \end{bmatrix}$$
- Vector $p$ (Prices): The current price per share of each company. $$p = \begin{bmatrix} 150 \ 2800 \ 700 \end{bmatrix}$$
The total value of your portfolio is the inner product $p^T q$: $$p^T q = (150 \times 100) + (2800 \times 50) + (700 \times 20)$$ $$p^T q = 15,000 + 140,000 + 14,000 = 169,000$$
If the market drops and all prices decrease by 10%, we can find the new value using scalar-vector multiplication: $$p_{new} = 0.9 p$$ $$Value_{new} = (0.9 p)^T q = 0.9 (p^T q) = 0.9 \times 169,000 = 152,100$$
This demonstrates how the properties of linearity ($(\alpha p)^T q = \alpha (p^T q)$) allow us to quickly calculate changes in complex systems.
Common Pitfalls and Edge Cases
- Dimension Mismatch: The most common error in vector programming. You cannot add a vector in $\mathbb{R}^3$ to a vector in $\mathbb{R}^4$. In code, this usually triggers a
ValueErrororShapeMismatch. - Row vs. Column Vectors: Mathematically, $a^T b$ assumes $a$ and $b$ are column vectors. If you are using a library like NumPy, you must be careful about the "shape" of your arrays (e.g.,
(n, 1)vs(1, n)vs(n,)). - Floating Point Precision: Because the inner product involves summing many products, small rounding errors in each multiplication can accumulate. In extremely high dimensions ($n > 10^7$), the order in which you add the numbers (summing from smallest to largest vs. largest to smallest) can actually change the result slightly.
- Sparse vs. Dense Performance: Using a dense inner product function on sparse vectors is a "silent" performance killer. It will work, but it will be orders of magnitude slower than it needs to be.
Summary of Key Connections
The concepts in this section form a hierarchy of complexity:
- Scalars are the simplest units.
- Vectors organize scalars into meaningful lists (points in space).
- Scalar Multiplication and Addition allow us to move and scale these points.
- Special Vectors (Zero, Ones, Unit) provide the coordinate system and utility tools.
- Inner Products allow us to compare vectors, find angles, and calculate weighted sums.
Understanding these fundamentals is not just about memorizing formulas; it is about developing an intuition for how data "moves" and "interacts" in high-dimensional space. Whether you are calculating the trajectory of a rocket or the attention mechanism in a Transformer model, these are the tools you will use.
Source Materials
Study Applied Linear Algebra and Least Squares on Lykke
This course page is free to open: signed out, you can read its first two sections, and signing in, on any plan, opens the rest. The free Lykke Chrome extension syncs your own Canvas courses, so ChatGPT, Claude and Muse can answer from your classes and deadlines. It only reads Canvas.
Add the free extensionSign in to open every sectionAll sections of Applied Linear Algebra and Least Squares · Browse more course pages