“A line integral adds up little bits of work as you move along a path in a vector field.”
Line Integral (Vector Field): For a path in a vector field :
Physically: work done by the field along the path.
Surface Integral (brief): For a vector field over a surface ,
measures flux through the surface (think: how much “stuff” passes through).
Concept | ML Example |
---|---|
Work/Path Integral | Energy cost along optimization or RL trajectory |
Surface/Flux | Policy flow, divergence in density estimation |
Visualization | Streamlines in policy-gradient methods, or GAN dynamics |
Let’s use the field:
and the path: a unit circle, .
The analytic result for the work is .
# calc-10-line-surface/work_circle.py
import numpy as np
import matplotlib.pyplot as plt
# --- Vector field F(x, y) = [-y, x]
def F(x, y):
return np.array([-y, x])
# --- Path: unit circle
N = 400
t = np.linspace(0, 2 * np.pi, N)
r = np.stack([np.cos(t), np.sin(t)], axis=1)
drdt = np.stack([-np.sin(t), np.cos(t)], axis=1)
Fs = np.stack([F(x, y) for x, y in r])
# Compute dot(F, dr/dt) at each t
dots = np.sum(Fs * drdt, axis=1)
work = np.trapz(dots, t)
print(f"Work along circle: {work:.5f} (analytic = {2 * np.pi:.5f})")
# --- Plot field, path, streamplot
xv, yv = np.meshgrid(np.linspace(-1.3, 1.3, 24), np.linspace(-1.3, 1.3, 24))
U, V = F(xv, yv)
plt.figure(figsize=(6, 6))
plt.streamplot(xv, yv, U, V, color="gray", density=1.1, linewidth=0.7, arrowsize=1)
plt.plot(r[:, 0], r[:, 1], "r", label="Path (unit circle)")
plt.scatter([0], [0], color="k", s=35, label="Origin")
plt.title("Vector field $[-y, x]$ and circular path")
plt.xlabel("x"); plt.ylabel("y")
plt.legend()
plt.axis("equal")
plt.tight_layout()
plt.show()
Let’s look at the field , where :
This is a classic “downhill to the origin” field.
# calc-10-line-surface/streamplot_grad.py
import numpy as np
import matplotlib.pyplot as plt
def gradF(x, y):
return -2 * x, -2 * y
xv, yv = np.meshgrid(np.linspace(-2, 2, 28), np.linspace(-2, 2, 28))
U, V = gradF(xv, yv)
plt.figure(figsize=(6, 6))
plt.streamplot(xv, yv, U, V, color="blue", density=1.4, linewidth=1)
plt.title(r"Streamplot: $-\nabla f$, $f(x,y)=x^2+y^2$")
plt.xlabel("x"); plt.ylabel("y")
plt.scatter([0], [0], color="k", s=30, label="Minimum")
plt.legend()
plt.axis("equal")
plt.tight_layout()
plt.show()
Streamplots show how “agents” or “probability mass” flow under a vector field.
Put solutions in calc-10-line-surface/
and tag v0.1
.
Next: Calculus 11 — Divergence, Curl, and the Geometry of Probability Flows.