Trigonometry

Trigonometry in Graphics and Signals

Every frame in a Unity game involves hundreds of thousands of rotation matrix multiplications. Every token in LLaMA undergoes a RoPE rotation of its embedding vector through cos and sin. Every JPEG pixel passes through a 2D Discrete Cosine Transform. Trigonometry isn't school material for "someday"-it's literally executing right now in code on consumer devices.

  • **Game engines:** every scene object is transformed by rotation matrices R(θ) 60 times per second
  • **LLaMA/GPT-NeoX:** RoPE is the standard positional encoding in modern LLMs; cos/sin rotation applied at every token position
  • **FFT spectrum analysis:** atan2(Im(X[k]), Re(X[k])) gives the phase of each signal harmonic

Предварительные знания

  • Euler's Formula

2D and 3D Rotation Matrices

Rotating a point (x, y) by angle θ counterclockwise is matrix multiplication. The rotation matrix R(θ) is a direct consequence of Euler's formula: multiplying a vector by eⁱᶿ in the complex plane is equivalent to multiplying by the matrix.

**2D rotation matrix:** R(θ) = [[cos θ, −sin θ], [sin θ, cos θ]] [x', y'] = R(θ) · [x, y]ᵀ Properties: det(R) = 1, Rᵀ = R⁻¹ (orthogonal matrix)

In 3D engines, rotation uses either 3×3 matrices (Rx, Ry, Rz for each axis) or quaternions (which avoid gimbal lock). The Y-axis rotation matrix is Ry(θ) = [[cos θ, 0, sin θ], [0, 1, 0], [−sin θ, 0, cos θ]].

The rotation matrix R(θ) is orthogonal. What does this imply about R⁻¹?

Fourier Series of Periodic Signals

Any periodic signal with period T can be decomposed into a Fourier series-an infinite sum of sinusoids at frequencies that are integer multiples of the fundamental f₀ = 1/T. Square waves, sawtooth waves, any repeating shape-all can be decomposed.

The Gibbs phenomenon: a Fourier series always overshoots by approximately 9% at a jump discontinuity, regardless of the number of harmonics. It's a mathematical property, not an algorithm bug. In image processing, it causes ringing artifacts around sharp edges in JPEG.

A square wave contains only odd harmonics. What happens when even harmonics are added?

RoPE Positional Encoding and atan2

RoPE (Rotary Position Embedding) is the positional encoding method used in modern transformers (LLaMA, GPT-NeoX). The idea: rotate query vector q and key vector k by an angle that depends on the token's position. The dot product of rotated_q and rotated_k then depends only on the difference of positions.

The atan2(y, x) function returns the angle in (−π, π], using the signs of both arguments to determine the correct quadrant. Unlike arctan(y/x), it never divides by zero when x = 0, and it distinguishes (1, 1) from (−1, −1). Use atan2 in all production code.

What is the key property of RoPE positional encoding?

Key Ideas

  • **Rotation matrix:** R(θ) = [[cos θ, −sin θ], [sin θ, cos θ]], det = 1, R⁻¹ = Rᵀ; composes as R(α)·R(β) = R(α+β)
  • **Fourier series:** any periodic signal = sum of sin/cos harmonics; Gibbs phenomenon-~9% overshoot at discontinuities regardless of harmonic count
  • **RoPE:** rotate q and k by angle m·θᵢ per pair; the dot product depends only on position difference-enabling relative position awareness
  • **atan2(y, x):** always use instead of arctan(y/x) in code; returns angle in (−π, π] with correct quadrant

Related Topics

Trigonometry in graphics and signals applies all previous topics:

  • Euler's Formula — Rotation matrices and DFT are direct consequences of eⁱˣ = cos x + i·sin x
  • Inverse Trigonometric Functions — atan2 is the practical, quadrant-aware version of arctan-the same concept with both signs tracked
  • Trigonometry in Interviews — Angle between vectors, rotation, and cross product-all interview problems use rotation matrices and atan2

Вопросы для размышления

  • RoPE lets transformers generalize to sequences longer than seen during training. How does the property that dot products depend only on position differences enable this?
  • What is the relationship between complex multiplication z·eⁱᶿ and matrix multiplication R(θ)·v? Are these the same rotation or different operations?
  • The Gibbs phenomenon causes ringing in JPEG around sharp edges. How do JPEG engineers mitigate this? Hint: think about DCT vs DFT and block boundaries.

Связанные уроки

  • dsp-01
Trigonometry in Graphics and Signals

0

1

Sign In