Geometry and Trigonometry
Geometry and Trigonometry
This chapter builds intuition for vectors as "direction + length," introduces basic trigonometric functions, and uses the spatial intuition of coordinate systems as a foundation for learning "vector spaces" in linear algebra.
Word embeddings, similarity calculations in recommendation systems, and positional encodings in Transformers are all built upon the geometric intuition from this chapter.
Planar Vectors: Direction + Length
What is a vector
Ordinary numbers (scalars) only carry one piece of information: "magnitude," such as "the temperature is 25 degrees."
VectorCarrying two pieces of information simultaneously: direction and length (magnitude).
In a Cartesian coordinate system, a vector is written in coordinate form, representing the position reached by "starting from the origin, moving x units along the x-axis and y units along the y-axis"; the arrow itself encodes the direction and length.
The length (magnitude) of a vector
This is the Pythagorean theorem: the length of a vector is the hypotenuse of the right triangle formed by its components on the x-axis and y-axis.
Vector addition and scalar multiplication
| Operations | Algebraic rules | Geometric meaning |
|---|---|---|
| Addition | Head-to-tail: Connect the start point of the second vector to the end point of the first vector | |
| Scalar multiplication | Only changes the length (reverses direction when k is negative), does not change the line on which the direction lies |
Vector dot product: Measures "how close two directions are"
where is the angle between the two vectors. The sign of the dot product has a clear geometric meaning:
| Dot product sign | Angle range | Geometric meaning |
|---|---|---|
| Directions are roughly the same | ||
| Two vectors are perpendicular (orthogonal) | ||
| Directions are roughly opposite |
Interactive demonstration: Angle between vectors and dot product
Blue vector a is fixed pointing to the right; drag the slider to rotate green vector b and change its length, observing the dot product sign (green indicates similar directions, red indicates opposite directions, gray indicates perpendicular).
Why vectors are crucial for AI
In machine learning, a single sample data (an image, a text embedding, a user feature) is essentially avector in a high-dimensional space..
The larger the dot product (or cosine of the angle) between the embedding vectors of two words, the more semantically similar those two words are, which is the mathematical foundation for word embeddings and recommendation system "similarity calculation."
What each layer in a neural network does is essentially the dot product of the weight vector and the input vector.
Instance
Vector length, dot product, and cosine of the angle (the prototype of word vector similarity)
import
math
Two "word vectors": assuming they are embeddings of two words (simplified to two dimensions)
king
=
(
3
,
4
)
queen
=
(
4
,
3
)
Modulus |v| = sqrt(x² + y²)
norm_king
=
math
.
sqrt
(
3
**
2
4
**
2
)
5
norm_queen
=
math
.
sqrt
(
4
**
2
3
**
2
)
5
(
norm_king
,
norm_queen
)
Dot product a·b = a₁b₁ + a₂b₂
dot
=
king
[
0
]
- queen
[
0
]
- king
[
1
]
- queen
[
1
]
(
dot
)
The cosine of the angle between two vectors is cos θ = (a·b) / (|a||b|), and the closer it is to 1, the closer the directions of the two vectors are.
cos_theta
=
dot /
(
norm_king * norm_queen
)
(
cos_theta
)
The output result of the above code is:
5.0 5.0
24
0.96
A cosine similarity of 0.96 means that the directions of the two vectors are very close, which is the geometric expression of "semantic similarity."
Exercise: Vector, find its length.
Click to view the answer
Basic trigonometric functions: sin and cos
From right-angled triangles to the unit circle
Initially learning trigonometry often starts with the ratios of the sides of a right-angled triangle:
A more general and more suitable way for AI scenarios isUnit circle definition: On a circle with a radius of 1, the coordinates of the point obtained by rotating counterclockwise by an angle from the positive x-axis are .
The advantage of this definition is: it can take any angle (not limited to 0°~90°), and sin and cos continuously "cycle" as $\theta$ changes, exhibitingPeriodicity(After one full rotation of 360°, or radians, the values repeat once).
Click "Play" to see how a point on the unit circle generates a sine wave.
Dynamic demonstration: Unit circle generating a sine wave
A point on the unit circle rotates counter-clockwise with angle θ, and its height (sin θ) is recorded point by point on the right to form a red sine curve; the horizontal projection (cos θ) is marked in blue.
Key properties
| Property | Expression | Meaning |
|---|---|---|
| Boundedness | The value always falls within [-1, 1] regardless of the angle | |
| Periodicity | The value repeats once per full rotation | |
| Quadratic relationship | Direct deduction from the unit circle with a radius of 1 (Pythagorean theorem) |
sin, cos and positional encoding
Transformer models inherently do not know the "order of words" when processing a sequence of text (it processes all words in parallel).
To encode "position" information into the model, Transformer uses the following formula:
Two core properties of sin/cos are used here:
Boundedness: No matter how long the sequence is, the position encoding values will not explode and will always remain between [-1, 1], without interfering with other numerical calculations.
Periodicity + Superposition of Different Frequencies: Different dimensions use sin/cos waveforms of different frequencies, allowing the model to distinguish between "adjacent positions" (high-frequency waveforms change quickly) and perceive "long-distance positional relationships" (low-frequency waveforms change slowly).
This is related to signal processing in theFourier TransformCommon idea: Any complex signal can be represented by a superposition of sine waves of different frequencies.
Interactive demonstration: Positional Encoding Heatmap
The x-axis is the position in the sequence (0~49), the y-axis is the encoding dimension (0~15), and the color is the encoding value. You can see that the waveforms in lower dimensions (top) change quickly, while those in higher dimensions (bottom) change slowly--a superposition of different frequency sin/cos.
Red represents positive values, and blue represents negative values, consistent with the sin/cos waveforms. The encoding dimensions of a real Transformer are usually 512 or even higher; here, only the first 16 dimensions are shown for illustration.
Example
Manual calculation of positional encoding: frequency distribution of the first few dimensions when d=16
import
math
d
=
16
Total encoding dimension (for illustration, the real model is 512+)
The angular frequency of dimension 2i is 1 / 10000^(2i/d)
Low dimension frequency is high (distinguishes adjacent positions), high dimension frequency is low (perceives long distances)
for
i
in
[
0
,
2
,
4
]
:
freq
=
1
/
(
10000
**
(
i / d
)
)
(
i
,
round
(
freq
,
6
)
)
pos=3 sin encoding value on dimension 0: sin(3 * 1.0)
(
round
(
math
.
sin
(
3
1.0
)
,
6
)
)
The output of the above code is:
0 1.0
2 0.316228
4 0.1
0.14112
The frequency of dimension 0 is 1.0, and dimension 4 decays to 0.1--the frequency decays exponentially along the dimension, which is exactly the layout of "high frequency first, low frequency last."
Coordinate system and spatial intuition
The two-dimensional coordinate system is the starting point for understanding "high-dimensional space."
The Cartesian coordinate system uses two mutually perpendicular axes to give a unique "address" to every point on a plane.
This intuitive idea can be directly generalized: a three-dimensional space adds a z-axis, and an n-dimensional space is .
Although n-dimensional space cannot be drawn directly, the algebraic treatment is exactly the same: each coordinate axis ("dimension") is still an independent direction.
What this means for understanding linear algebra's "vector space"
| Two-dimensional intuition | High-dimensional generalization | Applications in AI |
|---|---|---|
| Projection: the component of a vector on an axis | Projection of a high-dimensional vector onto an arbitrary direction | Dot product / orthogonal decomposition is the basis of dimensionality reduction algorithms |
| Orthogonal: perpendicular in two directions and non-interfering | "Orthogonal basis" in high-dimensional space | PCA dimensionality reduction finds mutually orthogonal principal directions |
| Basis: the x-axis and y-axis are the most basic reference directions | Any vector can be written as a combination of basis vectors | The dimension of the embedding space is the number of bases |
The embedding dimension of many language models is hundreds to thousands (e.g., 768 dimensions); you cannot draw this space on paper, but you can "imagine" it using familiar operations in two or three dimensions.
A specific analogy
Imagine you are describing a movie; you can score it across many dimensions such as "action scene intensity," "comedy level," and "plot complexity."
Each movie corresponds to a point (vector) in this "multi-dimensional scoring space."
The more similar two movies are, the "closer" their corresponding vectors are in the space (larger dot product, smaller angle).
This is the geometric intuition behind "similar content recommendation" in recommendation systems, and the source of this intuition is the spatial sense of "how close two points are" in a two-dimensional coordinate system.
Exercise: If a vector has a large projection along the x-axis and an almost zero projection along the y-axis, what direction is this vector generally pointing?
Click to view answer
Direction is almost along the x-axis: it contributes very little in the y direction, indicating it is nearly perpendicular to the y-axis and nearly parallel to the x-axis.
Chapter Summary
| Topic | One-sentence core |
|---|---|
| Plane Vector | Carries two pieces of information, "direction" and "length", and is the basic unit of high-dimensional data (embedding) |
| Dot Product | Measures "how close" two directions are and is the mathematical basis for similarity calculation |
| sin / cos | Coordinates on the unit circle, which have periodicity and boundedness, are mathematical tools for positional encoding |
| Coordinate system | Two-dimensional/three-dimensional spatial intuition is the starting point for understanding n-dimensional vector spaces, projections, orthogonality, etc. |