BRAGA

02 Writing02-02 Notes02-02-280

Sine

Published 16 Apr 20268 min read
Contents
  1. 01Deep Breakdown
  2. 02Flowchart
  3. 03Real-world use
  4. 04The formula, piece by piece
  5. 05The formula in words
  6. 06Critical Concepts
  7. 07Step-by-step calculation (tiny example)
  8. 08Code examples
ddx(sin⁡x)=cos⁡x\frac{d}{dx}(\sin x) = \cos x
(1)

Deep Breakdown

What it tells me (Situation Awareness): The derivative of sine is cosine: the rate of change of a sine wave at any point is the value of the cosine at that same point. This creates the cycle sin⁡→cos⁡→−sin⁡→−cos⁡→sin⁡\sin \to \cos \to -\sin \to -\cos \to \sin: differentiate four times and you're back to sin⁡\sin. In DA/DS: sinusoidal features appear in time series (seasonal decomposition, Fourier features), positional encodings in transformers (sinusoidal PE in "Attention is All You Need"), signal processing, and physics-informed neural networks. The derivative is used when computing gradients through sinusoidal encodings or when differentiating Fourier-based loss functions.

Flowchart

f(x) = sin(x)f'(x) = cos(x)f''(x) = -sin(x)f'''(x) = -cos(x)f''''(x) = sin(x) =f(x): 4-cycleChain rule:d/dx(sin(f(x))) =cos(f(x)) * f'(x)d/dx(sin(2*pi*x/T)) =(2*pi/T)*cos(2*pi*x/T)Positionalencodinggradient:cos(pos/10000^{2i/d_model})

Real-world use

Where it is used

Fourier feature maps for tabular data (random Fourier features for kernel approximation); positional encoding in transformers (sinusoidal PEpos,2i=sin⁡(pos/100002i/d)\text{PE}_{pos,2i} = \sin(pos/10000^{2i/d})); time series seasonality decomposition (differentiating Asin⁡(2πft+ϕ)A\sin(2\pi ft + \phi)); physics-informed neural networks (PDEs with sinusoidal solutions).

Problem it solves

When a model includes sinusoidal terms (e.g., seasonal Fourier features, positional encodings), backpropagation through those terms requires the derivative d/dx(sin⁡x)=cos⁡xd/dx(\sin x) = \cos x. Without it, gradients cannot flow through sinusoidal feature transformations.

How it solves the problem

From first principles: d/dx(sin⁡x)=lim⁡h→0[sin⁡(x+h)−sin⁡x]/hd/dx(\sin x) = \lim_{h\to0}[\sin(x+h)-\sin x]/h. Using sin⁡(x+h)=sin⁡xcos⁡h+cos⁡xsin⁡h\sin(x+h) = \sin x \cos h + \cos x \sin h: the limit becomes sin⁡x⋅0+cos⁡x⋅1=cos⁡x\sin x \cdot 0 + \cos x \cdot 1 = \cos x, using lim⁡h→0cos⁡h=1\lim_{h\to0}\cos h = 1 and lim⁡h→0sin⁡h/h=1\lim_{h\to0}\sin h / h = 1.

Example in a business

A DS uses sinusoidal Fourier features for a demand forecasting model: ϕk(t)=sin⁡(2πkt/T)\phi_k(t) = \sin(2\pi k t / T) where TT is the seasonal period. When fitting via gradient descent, the gradient w.r.t. time-shift parameter uses d/dtsin⁡(2πkt/T)=(2πk/T)cos⁡(2πkt/T)d/dt \sin(2\pi k t/T) = (2\pi k/T)\cos(2\pi k t/T). This is the chain rule applied to d/dx(sin⁡x)=cos⁡xd/dx(\sin x) = \cos x.

The formula, piece by piece

  • sin⁡x\sin x: sine function; xx in radians; period 2π2\pi; range [−1,1][-1, 1]
  • cos⁡x\cos x: cosine function; result of differentiating sine; also period 2π2\pi; range [−1,1][-1, 1]
  • The derivative is a phase shift: cos⁡x=sin⁡(x+π/2)\cos x = \sin(x + \pi/2); differentiating shifts the wave by π/2\pi/2
  • Chain rule: d/dx(sin⁡f(x))=cos⁡(f(x))⋅f′(x)d/dx(\sin f(x)) = \cos(f(x)) \cdot f'(x); keep the argument, multiply by inner derivative
  • Examples: d/dx(sin⁡(2x))=2cos⁡(2x)d/dx(\sin(2x)) = 2\cos(2x); d/dx(sin⁡(x2))=2xcos⁡(x2)d/dx(\sin(x^2)) = 2x\cos(x^2)
  • Second derivative: d2/dx2(sin⁡x)=−sin⁡xd^2/dx^2(\sin x) = -\sin x; so sin⁡x\sin x satisfies y′′+y=0y'' + y = 0 (simple harmonic oscillator ODE)
  • Euler formula connection: eix=cos⁡x+isin⁡xe^{ix} = \cos x + i \sin x; differentiating: ieix=−sin⁡x+icos⁡xie^{ix} = -\sin x + i\cos x; real part: −sin⁡x=d/dx(cos⁡x)-\sin x = d/dx(\cos x); imaginary part: cos⁡x=d/dx(sin⁡x)\cos x = d/dx(\sin x)

The formula in words

"The slope of the sine curve at any point equals the cosine value at that point. Where sine is at its peak (x=π/2x=\pi/2), cosine (and hence the slope) is zero — a horizontal tangent. Where sine crosses zero (x=0x=0), cosine is 1 — the steepest positive slope." Tiny examples: d/dx(sin⁡x)∣x=0=cos⁡(0)=1d/dx(\sin x)|_{x=0} = \cos(0) = 1 (maximum slope at origin). d/dx(sin⁡x)∣x=π/2=cos⁡(π/2)=0d/dx(\sin x)|_{x=\pi/2} = \cos(\pi/2) = 0 (flat top of wave). d/dx(3sin⁡(5x))=15cos⁡(5x)d/dx(3\sin(5x)) = 15\cos(5x) (chain rule).

Critical Concepts

  • 4-cycle of derivatives: sin⁡→cos⁡→−sin⁡→−cos⁡→sin⁡\sin \to \cos \to -\sin \to -\cos \to \sin. This means d4k/dx4k(sin⁡x)=sin⁡xd^{4k}/dx^{4k}(\sin x) = \sin x for any integer k≥0k \ge 0.
  • Sinusoidal ODE: y′′=−yy'' = -y (simple harmonic oscillator). Solutions: Asin⁡(x)+Bcos⁡(x)A\sin(x) + B\cos(x). Used in physics-informed neural networks modeling oscillatory systems.
  • Fourier series gradient: d/dx∑k[akcos⁡(kx)+bksin⁡(kx)]=∑k[−kaksin⁡(kx)+kbkcos⁡(kx)]d/dx \sum_k [a_k\cos(kx) + b_k\sin(kx)] = \sum_k [-ka_k\sin(kx) + kb_k\cos(kx)]. Differentiating a Fourier series swaps sin⁡↔cos⁡\sin \leftrightarrow \cos and multiplies by frequency kk.
  • Transformer positional encoding: PE(pos,2i)=sin⁡(pos/100002i/dmodel)\text{PE}(pos, 2i) = \sin(pos/10000^{2i/d_\text{model}}).

Gradient w.r.t. position: cos⁡(pos/100002i/dmodel)/100002i/dmodel\cos(pos/10000^{2i/d_\text{model}}) / 10000^{2i/d_\text{model}}. This allows attention patterns to generalize to unseen sequence positions.

Step-by-step calculation (tiny example)

Differentiate f(x)=2sin⁡(3x)−sin⁡(x2)f(x) = 2\sin(3x) - \sin(x^2).

Step 1: First term — chain rule on sin⁡(3x)\sin(3x): d/dx(2sin⁡(3x))=2cos⁡(3x)⋅3=6cos⁡(3x)d/dx(2\sin(3x)) = 2\cos(3x) \cdot 3 = 6\cos(3x).

Step 2: Second term — chain rule on sin⁡(x2)\sin(x^2): d/dx(−sin⁡(x2))=−cos⁡(x2)⋅2x=−2xcos⁡(x2)d/dx(-\sin(x^2)) = -\cos(x^2) \cdot 2x = -2x\cos(x^2).

Step 3: Combine: f′(x)=6cos⁡(3x)−2xcos⁡(x2)f'(x) = 6\cos(3x) - 2x\cos(x^2).

Code examples

Beginner example (Python)

python
import numpy as np

def sine_deriv(x: float) -> float:
    """d/dx(sin(x)) = cos(x)."""
    return np.cos(x)   # Verify numerically
x_vals = np.linspace(0, 2 * np.pi, 9)
h = 1e-7
print(f"{'x (rad)':>10} {'cos(x)':>10} {'numerical':>12} {'match':>7}")
for x in x_vals:
    analytic = sine_deriv(x)
numerical = (np.sin(x + h) - np.sin(x - h)) / (2 * h)
match = abs(analytic - numerical) < 1e-5
print(f"{x:>10.4f} {analytic:>10.5f} {numerical:>12.5f} {str(match):>7}")   # Sinusoidal Fourier feature gradient
print("\n--- Fourier feature gradient example ---")
period = 12   # monthly seasonality
k_vals = [1, 2, 3]
t = 3   # month 3
for k in k_vals:
    feature = np.sin(2 * np.pi * k * t / period)
gradient = (2 * np.pi * k / period) * np.cos(2 * np.pi * k * t / period)
print(f"k={k}: sin(2pi*{k}*t/12) = {feature:.4f}, gradient = {gradient:.4f}")

Beginner example (R)

r
# d/dx(sin(x)) = cos(x)
sine_deriv <- function(x) cos(x)
x_vals <- seq(0, 2 * pi, length.out = 9)
h <- 1e-7
cat(sprintf("%-10s %-10s %-12s\n", "x (rad)", "cos(x)", "numerical"))
for (x in x_vals) { analytic <- sine_deriv(x)
  numerical <- (sin(x + h) - sin(x - h)) / (2 * h)
  cat(sprintf("%-10.4f %-10.5f %-12.5f\n", x, analytic, numerical))
}  # Fourier feature example
cat("\n--- Fourier feature gradient ---\n")
period <- 12
for (k in 1:3) { t <- 3
  grad <- (2 * pi * k / period) * cos(2 * pi * k * t / period)
  cat(sprintf("k=%d: gradient = %.4f\n", k, grad))
}

Professional example (Python)

python
import numpy as np
np.random.seed(42)   # -- Sinusoidal positional encoding gradient in transformer attention ----------
# PE(pos, 2i) = sin(pos / 10000^{2i/d_model})
# Gradient w.r.t. pos: cos(pos/10000^{2i/d_model}) / 10000^{2i/d_model}
D_MODEL = 16   # small for illustration
MAX_SEQ_LEN = 50

def positional_encoding(seq_len: int, d_model: int) -> np.ndarray:
    """Transformer sinusoidal positional encoding (Vaswani et al., 2017)."""
    pos = np.arange(seq_len)[:, np.newaxis]   # (seq_len, 1)
    i = np.arange(0, d_model, 2)[np.newaxis, :]   # (1, d_model/2)
    div_term = 10000 ** (2 * i / d_model)   # (1, d_model/2)
    pe = np.zeros((seq_len, d_model))
    pe[:, 0::2] = np.sin(pos / div_term)   # even dims: sin
    pe[:, 1::2] = np.cos(pos / div_term)   # odd dims: cos
    return pe

def positional_encoding_gradient(seq_len: int, d_model: int) -> np.ndarray:
    """Gradient of PE w.r.t. position (d_PE/d_pos). d/d_pos sin(pos/D) = cos(pos/D)/D (chain rule: d/dx(sin(x))=cos(x)) d/d_pos cos(pos/D) = -sin(pos/D)/D """
    pos = np.arange(seq_len)[:, np.newaxis]
    i = np.arange(0, d_model, 2)[np.newaxis, :]
    div_term = 10000 ** (2 * i / d_model)
    d_pe = np.zeros((seq_len, d_model))
    d_pe[:, 0::2] = np.cos(pos / div_term) / div_term   # d/d_pos(sin) = cos/D
    d_pe[:, 1::2] = -np.sin(pos / div_term) / div_term   # d/d_pos(cos) = -sin/D
    return d_pe
pe = positional_encoding(MAX_SEQ_LEN, D_MODEL)
d_pe = positional_encoding_gradient(MAX_SEQ_LEN, D_MODEL)
print("Sinusoidal Positional Encoding: d/dx(sin(x)) = cos(x) in action")
print(f"\nPE shape: {pe.shape} (seq_len={MAX_SEQ_LEN}, d_model={D_MODEL})")
print(f"\nPE values at selected positions (first 4 dims):")
print(f"{'pos':>5} {'dim0 (sin)':>12} {'dim1 (cos)':>12} {'dim2 (sin)':>12} {'dim3 (cos)':>12}")
for pos in [0, 5, 10, 25, 49]:
    print(f"{pos:>5} {pe[pos,0]:>12.5f} {pe[pos,1]:>12.5f} {pe[pos,2]:>12.5f} {pe[pos,3]:>12.5f}")
print(f"\nGradient d(PE)/d(pos) at pos=10 (chain rule: cos/div_term for sin dims):")
print(f"{'dim':>5} {'gradient':>12} {'div_term':>12}")
for dim_idx, (actual_dim, is_sin) in enumerate([(0, True), (2, True), (4, True)]):
    i_val = actual_dim // 2
div_t = 10000 ** (2 * i_val / D_MODEL)
grad = d_pe[10, actual_dim]
print(f"{actual_dim:>5} {grad:>12.8f} {div_t:>12.2f}")
print(f"\nKey: d/d_pos(sin(pos/D)) = cos(pos/D)/D -- uses d/dx(sin(x)) = cos(x)")

Source: 1300 p. 221

#calculus#differential-calculus#table-of-standard-derivatives

Keyboard shortcuts

Single keys, ignored while typing in a field or with Cmd, Ctrl or Alt held.

0
Home
1 2 3 4
Work, Writing, Tools, About
j / k
Next / previous section on this page
n / p
Next / previous project or essay
u
Up one level
l
Deutsch
t
Light or dark theme
?
This list
Esc
Close