Math for AI course · Lesson 27 of 180
Vector Spaces: Test Whether AI Representations Stay Legal
Test zero, addition, and scalar closure, then use normalized embeddings as a counterexample that separates a useful representation set from a vector space.
An embedding pipeline can force each nonzero output vector to length . That makes similarity calculations convenient, but it creates a subtle trap: the normalized outputs do not form a vector space under ordinary vector addition and scalar multiplication. Add two of them or change one vector’s scale, and the result can leave the allowed set.
This lesson turns “vector space” into a test rather than a label. By the end, you will be able to check the zero vector, addition closure, and scalar closure; construct one decisive counterexample when a rule fails; and explain why normalized embeddings live inside an ambient vector space without themselves forming one.
Learning outcome: Test whether a set satisfies vector-space closure rules.
Lesson 17: Linear combinations build new vectors supplied the operation . Lesson 26: Groups, rings, and fields explain valid operations showed why a set and its operations must be considered together. Section 2.4 of the official Mathematics for Machine Learning book formalizes that combination as a vector space. The tests, embedding counterexample, code, visual, and assessments below are original.
A vector space promises that linear combinations stay inside
Let be a set whose elements are called vectors. For a real vector space, two operations are available:
The first operation adds two vectors. The second multiplies a vector by a real number, or scalar. The arrows ending in encode closure: both operations must return another member of the same set.
Closure alone is not the complete definition. Vector addition must also have a zero vector and additive inverses, addition must be associative and commutative, scalar multiplication must associate correctly, and the familiar distributive and identity rules must hold.
The rule about every scalar is demanding. A set that survives multiplication by but fails for , or survives positive scalars but fails for , is not a real vector space.
Inherited operations shorten the test
Most practical questions start with a candidate set using the ordinary addition and scalar multiplication already defined in . The algebraic laws are inherited from the ambient space. The remaining work is to determine whether the results ever escape .
There is an important asymmetry in how this test is used:
- To prove that is a vector space, the closure argument must cover every allowed input.
- To prove that is not a vector space, one valid counterexample is enough.
Checking three convenient vectors cannot prove universal closure. Finding one pair whose sum escapes the set can disprove it immediately.
Worked example: a constraint that survives combinations
Consider the set
Every vector in has the form . The zero vector belongs to by choosing . Now take arbitrary vectors
and arbitrary scalars .
For a numerical check, choose , , , and :
The calculation illustrates the rule, while the symbolic argument proves it for all values.
Normalized embeddings fail the same test
Now define the unit-length set
This is the unit sphere in the ambient vector space . An
embedding library may deliberately return vectors in this set. For example,
the current Sentence Transformers encode
documentation
describes a normalize_embeddings option that normalizes returned vectors to
length .
In two dimensions, take
Both have Euclidean norm , so both are in . Their sum is
Because , the sum lies outside . Scalar closure also fails:
The zero vector supplies a third failure because its norm is , not .
Static closure counterexample
The unit circle rejects ordinary linear combinations
The blue circle contains every two-dimensional vector of Euclidean norm 1. The solid and dashed unit vectors begin inside the set, while their sum, a doubled vector, and the zero vector fail membership.
| Closure test | Inputs in S | Result | Result norm | Membership |
|---|---|---|---|---|
| Addition | e₁ and e₂ | e₁ + e₂ = (1, 1) | √2 | Outside S |
| Scalar multiplication | 2 and e₁ | 2e₁ = (2, 0) | 2 | Outside S |
| Zero vector | Required additive identity | 0 = (0, 0) | 0 | Not in S |
Any one row disproves vector-space membership. The three rows expose different requirements, so fixing one failure would not fix the others.
The visual uses because the geometry fits on a page. The same scalar counterexample works in every dimension: if , then . Therefore unit-normalized embeddings in are not a vector space under the usual operations.
Renormalizing changes the operation
A common pipeline adds or averages normalized embeddings, then normalizes the result again. That procedure can be useful, but the final normalization is a new nonlinear operation:
provided the denominator is nonzero. The raw sum first leaves the unit sphere; normalization maps it back. Mapping the answer back into does not make closed under the original addition operation.
The zero case matters. If , then , and dividing by its norm would divide by zero. An implementation needs an explicit policy for zero or near-zero rows.
The NumPy linalg.norm
documentation
specifies the axis and keepdims controls used below to compute one norm per
embedding while preserving a broadcastable column shape:
import numpy as np
def normalize_rows(vectors):
vectors = np.asarray(vectors, dtype=float)
norms = np.linalg.norm(vectors, axis=1, keepdims=True)
normalized = np.divide(
vectors,
norms,
out=np.zeros_like(vectors),
where=norms > 0,
)
return normalized
embeddings = np.array([
[3.0, 4.0],
[-4.0, 3.0],
[0.0, 0.0],
])
normalized = normalize_rows(embeddings)
print(normalized)
print(np.linalg.norm(normalized, axis=1))
The output is
[[ 0.6 0.8]
[-0.8 0.6]
[ 0. 0. ]]
[1. 1. 0.]
The first two rows land on the unit circle. The zero row remains zero because the function deliberately uses zero as its engineering fallback. That fallback belongs to but not to , so callers must not claim that every output has unit norm.
A reliable closure review asks about the actual operations
When a model description calls a collection of representations a “space,” identify the set and operations before assuming linear structure. Raw embeddings may live in . Unit-normalized embeddings live on a sphere. Probability vectors with nonnegative coordinates summing to live in another constrained set. Each can support useful algorithms, but ordinary addition and arbitrary real scaling do not necessarily keep the result legal.
The next lesson studies subspaces: restricted subsets that do pass the closure test and therefore retain the ambient linear structure. Before moving on, use the questions below to practice proving universal closure and finding decisive failures.
Check your understanding
Question 1
For a nonempty subset of Rⁿ with ordinary vector operations, which three membership checks establish that it is a vector space?
Show the step-by-step solution
First check that the zero vector belongs to the set. Second, choose arbitrary and in the set and prove that remains in it. Third, choose an arbitrary real scalar and arbitrary in the set and prove that remains in it. The remaining vector-space laws are inherited from .
Question 2
Show that U = {(t, −t): t ∈ R} is closed under the linear combination a u + b v.
Show the step-by-step solution
Take arbitrary and in , with arbitrary real and . Then
Set . The result is , which matches the defining form of . Because the inputs and scalars were arbitrary, this proves closure rather than checking only one example.
Question 3
Use e₁ = (1, 0) and e₂ = (0, 1) to disprove addition closure for unit-normalized vectors.
Show the step-by-step solution
Both inputs have norm , so both belong to the unit-normalized set. Their sum is . Its norm is , which is not . Thus two valid members produce a result outside the set. A single such pair disproves addition closure.
Question 4
Does multiplying every unit vector by −1 disprove scalar closure? If not, choose a scalar that does.
Show the step-by-step solution
No. If , then , so the scalar keeps every unit vector on the sphere. Scalar closure must hold for every real scalar, however. Choosing gives , so lies outside the unit sphere and disproves closure.
Question 5
Why must row normalization handle the zero vector explicitly?
Show the step-by-step solution
The usual formula divides by . For , the norm is , so the formula requests division by zero and is undefined. Code must detect that case and choose a documented policy, such as returning zero, raising an error, or filtering the row. Returning zero is practical in some pipelines, but the result is not unit-normalized.
Question 6
A pipeline averages two unit embeddings and normalizes the average. Why does that not prove the unit sphere is a vector space?
Show the step-by-step solution
First the ordinary average is formed: . Its norm need not be , so this linear combination can leave the sphere. The pipeline then applies a separate normalization map to return to the sphere. Closure tests the original addition and scalar multiplication; adding a repair step changes the operation being studied.
Question 7
The nonnegative quadrant Q = {(x₁, x₂): x₁ ≥ 0 and x₂ ≥ 0} contains zero and is closed under addition. Is Q a real vector space?
Show the step-by-step solution
No. Choose , which belongs to , and the real scalar . Then , whose first coordinate is negative, so the result is outside . The set fails scalar closure even though it contains zero and survives addition.