Skip to content
AI-grafen
DAI developerMathematics· about 45 min· fundamentals that rarely change· verified 2026-09-20· EN

Trigonometry — the basics

Be able to use sine and cosine and understand periodic functions — the background to sinusoidal positional encoding.

Practise in Mattegrafen ↗ · Matematik 4

Prerequisites

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 2π2\pi radians (360°).

And since x² + y² = 1 for every point on the unit circle you get, for free:

sin⁡2v+cos⁡2v=1\sin^2 v + \cos^2 v = 1

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 2π2\pi radians. The angle is measured as the arc length on the unit circle, which keeps the derivatives clean: ddvsin⁡v=cos⁡v\frac{d}{dv}\sin v = \cos v. With degrees there would be a factor of π/180\pi/180 everywhere.

Values worth knowing without computing:

vv0π/6\pi/6π/4\pi/4π/3\pi/3π/2\pi/2π\pi
sin⁡v\sin v01/222\tfrac{\sqrt2}{2}32\tfrac{\sqrt3}{2}10
cos⁡v\cos v132\tfrac{\sqrt3}{2}22\tfrac{\sqrt2}{2}1/20−1

The general sine function:

y(t)=Asin⁡(2πft+φ)y(t) = A \sin(2\pi f t + \varphi)

ParameterMeansIn sound
AAthe amplitudethe volume
ffthe frequency in hertzthe pitch
φ\varphithe phase shiftwhere in the wave it starts
1/f1/fthe period in secondshow 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 pp a vector where component 2i2i is sin⁡(p/100002i/d)\sin(p / 10000^{2i/d}) and component 2i+12i+1 is cos⁡(⋅)\cos(\cdot). Low ii gives fast oscillations (separating neighbours), high ii 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

All the sources and licences