Trigonometry — the basics
Be able to use sine and cosine and understand periodic functions — the background to sinusoidal positional encoding.
Practise in Mattegrafen ↗ · Matematik 4Prerequisites
Intuition
Think of a point going round a circle of radius 1.
- cos(v) is the point's x coordinate.
- sin(v) is its y coordinate.
That is the whole definition. Everything else follows from it.
Since the point comes back to the same place every lap, both functions are periodic: they repeat with a period of one lap, that is radians (360°).
And since x² + y² = 1 for every point on the unit circle you get, for free:
That is Pythagoras' theorem, seen from the circle.
Why it matters for AI: every periodic process — sound waves, seasons, the daily rhythm — is described with sine functions. The Fourier transform decomposes a signal into exactly those. And the transformer's positional encoding uses sine and cosine at different frequencies to give every position in a sequence a unique pattern.
Formal
Radians. One lap is radians. The angle is measured as the arc length on the unit circle, which keeps the derivatives clean: . With degrees there would be a factor of everywhere.
Values worth knowing without computing:
| 0 | ||||||
|---|---|---|---|---|---|---|
| 0 | 1/2 | 1 | 0 | |||
| 1 | 1/2 | 0 | −1 |
The general sine function:
| Parameter | Means | In sound |
|---|---|---|
| the amplitude | the volume | |
| the frequency in hertz | the pitch | |
| the phase shift | where in the wave it starts | |
| the period in seconds | how long one wavelength lasts |
The link to Fourier: every periodic signal can be written as a sum of sine functions with different frequencies, amplitudes and phases. The Fourier transform works out which. That is why trigonometry is the prerequisite for spectrograms.
The link to positional encoding: the transformer needs to know where in the sequence each token sits. One way is to give position a vector where component is and component is . Low gives fast oscillations (separating neighbours), high gives slow ones (separating distant positions) — like the digits of a binary number, but continuous.
Code
import numpy as np
# The unit circle: cos is x, sin is y
for degrees in (0, 30, 45, 60, 90, 180):
v = np.deg2rad(degrees)
print(f"{degrees:3d}° cos={np.cos(v): .3f} sin={np.sin(v): .3f} "
f"sin²+cos²={np.sin(v)**2 + np.cos(v)**2:.1f}")
# 30° cos= 0.866 sin= 0.500 sin²+cos²=1.0
# 90° cos= 0.000 sin= 1.000 sin²+cos²=1.0
# A tone: A · sin(2π f t)
fs, f, A = 16000, 440.0, 0.5
t = np.arange(fs) / fs
tone = A * np.sin(2 * np.pi * f * t)
print(round(float(tone.max()), 2), int(round(1 / (f / fs) ))) # 0.5 36 samples per period
# A sum of sines — the same idea as Fourier, backwards
square = sum(np.sin(2 * np.pi * (2 * k + 1) * f * t) / (2 * k + 1) for k in range(20))
print(round(float(np.abs(square).max()), 2)) # approaching a square wave
# Sinusoidal positional encoding
def poscode(T, d):
p = np.arange(T)[:, None]
i = np.arange(0, d, 2)[None, :]
angle = p / (10000 ** (i / d))
pe = np.zeros((T, d))
pe[:, 0::2], pe[:, 1::2] = np.sin(angle), np.cos(angle)
return pe
pe = poscode(50, 16)
print(round(float(pe[0] @ pe[1]), 2), round(float(pe[0] @ pe[40]), 2)) # 7.71 1.9
The last line shows the point: nearby positions get similar vectors, distant ones get different vectors — entirely without anything having been learnt.
Mastery means
- Uses sine and cosine on the unit circle
- Reads off the amplitude, the frequency and the phase shift
- Connects periodicity to Fourier and to positional encoding
Sign in to do the exercises and build your mastery up.
Sources
- Matteboken (Mattecentrum) — free to read, non-profit association
- Wikipedia — Trigonometric functions (CC BY-SA 4.0) — CC BY-SA 4.0
- Skolverket — About AI in school (in Swedish) — Skolverket's open terms