# Vectors and Vector Spaces

Computer Science I · Algebra · https://tryals.app/learn/computer-science-i/vectors-and-vector-spaces

## What is a Vector?

A **vector** is an object with **magnitude** and **direction**, like velocity or force. In $n$ dimensions, it is an $n$-tuple. A **vector space** is any set of objects that can be **added** and **scaled** by numbers (scalars), obeying natural rules. This abstraction applies to arrows, polynomials, matrices, and functions.

Two core operations define the space:

| Operation | Rule | Geometric meaning |
| --- | --- | --- |
| **Vector addition** | $(a_1, b_1) + (a_2, b_2) = (a_1+a_2, b_1+b_2)$ | Tip-to-tail placement |
| **Scalar multiplication** | $c(v_1, v_2) = (cv_1, cv_2)$ | Stretch, shrink, or reverse |

These operations obey **vector-space axioms**: commutative and associative addition, a **zero vector** $\mathbf{0}$ that changes nothing, additive inverses, and distributive laws.

## Length and Pitfalls

The magnitude (**norm**) of a vector measures its length. For a 2D vector, the Pythagorean norm formula is:

$$\|v\| = \sqrt{v_1^2 + v_2^2}$$

A **unit vector** has norm 1 and captures pure direction. In computing, vectors serve as graphics coordinates, machine learning feature vectors, and language model embeddings.

| Concept | Correct Meaning | Common Mistake |
| --- | --- | --- |
| **Magnitude** | Single length $\sqrt{v_1^2 + v_2^2}$ | Treating components $v_1, v_2$ as the length |
| **Addition** | Componentwise addition | Adding magnitudes directly ($\|u+v\| \neq \|u\| + \|v\|$) |

> **Pitfall:** Do not treat vectors as mere lists of numbers. A vector is defined by how it adds and scales, and the length of a vector sum is generally not the sum of their individual lengths.

## Practice questions

3 of this lesson's 11 practice questions, with answers. The full set is in the app.

### 1. A word embedding places 'king' and 'queen' near each other in a 300-dimensional space. What is the vector space doing that a lookup table could not?

A. Nothing, the space is a visualisation of an underlying table
B. Storing the words more compactly than a table would
C. Guaranteeing that synonyms are assigned identical vectors
D. Making similarity computable, since meaning becomes geometry

**Answer:** D. Making similarity computable, since meaning becomes geometry

**Why:** A table stores relationships you put in; a space lets you compute ones you did not. Distance gives similarity and direction encodes relation, so analogies fall out as arithmetic. That is why embeddings replaced hand-built thesauri, the structure generalises past what anyone enumerated.

Page: https://tryals.app/practice/computer-science-i/vectors-and-vector-spaces/a-word-embedding-places-king-and-queen-near-each-other-in-a

### 2. Select every set that the lesson presents as a vector space under its natural addition and scaling.

A. Functions
B. Arrows in the plane
C. Matrices
D. The unit vectors in the plane
E. Polynomials

**Answer:** A. Functions; B. Arrows in the plane; C. Matrices; E. Polynomials

**Why:** Arrows, polynomials, matrices and functions can all be added and scaled, so all four are vector spaces, that generality is the point of the abstraction. The unit vectors are not: (1, 0) + (0, 1) has norm about 1.41, so the sum leaves the set, and scaling by 2 leaves it too.

Page: https://tryals.app/practice/computer-science-i/vectors-and-vector-spaces/select-every-set-that-the-lesson-presents-as-a-vector-space-under-its

### 3. In graphics, a pixel colour (r, g, b), a 3D position (x, y, z) and a weight vector in a neural network are all handled by the same vector code. Why does one implementation serve three unrelated meanings?

A. Because the compiler converts all three to a common internal type before operating on them.
B. Because colours, positions and weights are secretly the same physical quantity.
C. Because all three happen to have three components, which is what makes the code reusable.
D. Because the operations depend only on the vector space axioms.

**Answer:** D. Because the operations depend only on the vector space axioms.

**Why:** Write the addition routine and it never asks what the components mean, it adds them, and the same holds for scaling and the dot product. It generalises across dimension too: 3 for a colour, 512 for a word embedding, 150,000 for a document vector. Which is why a framework computing cosine similarity is running the identical dot-product code a renderer uses for lighting.

Page: https://tryals.app/practice/computer-science-i/vectors-and-vector-spaces/in-graphics-a-pixel-colour-r-g-b-a-3d-position-x-y-z-and-a
