Mathematical Foundations

III.9 · Calculus

What does a curved map look like up close?

Push a small square through a nonlinear map. Near a point the curved image is almost a parallelogram — the image of the square under one matrix, the Jacobian.

Predict a vector map with its local linear transformation

Input plane: point p, displacement δ and a small square

Input point p = (1, 0.5), displacement δ = (0.25, 0.25) and a square of half-side 0.25 around p.

Output near F(p): actual image versus the Jacobian prediction

F(p) = (0.75, 1). The grey curved shape is the actual image of the input square; the purple parallelogram is its Jacobian image. The orange arrow is the actual change F(p + δ) − F(p); the dashed purple segment ends at the prediction F(p) + Jδ = (1, 1.75).

Shape: outputs × inputs
2 × 2 (input ℝ², output ℝ²)
Output at p
(0.75, 1)
Column 1: response to a unit x-step
(2, 1)
Column 2: response to a unit y-step
(−1, 2)
Row 1: gradient of the first output
(2, −1)
Local area factor
5
Linear prediction
(1, 1.75)
Independent evaluation
(1, 1.875)
Squared prediction error (exact)
0.015625
Relative error
0.353553
Wrong-shape product
undefinedShape mismatch: J is 2×2 but the vector has 3 entries; J·v needs exactly 2.

Exact values use =; binary64 approximations use ≈. Undefined values state their reason.

Near p the map acts like the matrix J: the predicted change Jδ = (0.25, 0.75) is close to the actual change; the error is second order.

F(x,y)=[x2−y22xy]F(x,y)=\begin{bmatrix}x^{2}-y^{2}\\2xy\end{bmatrix}
JF(1,0.5)=[∂xF1∂yF1∂xF2∂yF2]=[2−112](2×2)J_F(1,0.5)=\begin{bmatrix}\partial_x F_1&\partial_y F_1\\\partial_x F_2&\partial_y F_2\end{bmatrix}=\begin{bmatrix}2&-1\\1&2\end{bmatrix}\quad(2\times2)
F(p)+Jδ=[0.751]+[2−112][0.250.25]=[11.75]F(p)+J\delta=\begin{bmatrix}0.75\\1\end{bmatrix}+\begin{bmatrix}2&-1\\1&2\end{bmatrix}\begin{bmatrix}0.25\\0.25\end{bmatrix}=\begin{bmatrix}1\\1.75\end{bmatrix}
F(p+δ)=[11.875],F(p+δ)−(F(p)+Jδ)=[00.125]F(p+\delta)=\begin{bmatrix}1\\1.875\end{bmatrix},\qquad F(p+\delta)-\big(F(p)+J\delta\big)=\begin{bmatrix}0\\0.125\end{bmatrix}

Evaluate F at p

F(p) = (0.75, 1).

F(1,0.5)=[0.751]F(1,0.5)=\begin{bmatrix}0.75\\1\end{bmatrix}

from −2 to 2, up to 2 decimal places

from −2 to 2, up to 2 decimal places

from −1 to 1, up to 2 decimal places

from −1 to 1, up to 2 decimal places

Drag the point p or the displaced point p + δ in the input plane. In the output plane, grey is the true image of the small square, purple its Jacobian image; orange is the actual change, dashed purple the prediction Jδ.

The complex-square map F(x,y)=(x2−y2, 2xy)F(x,y)=(x^2-y^2,\,2xy) bends the plane. Around p=(1,12)p=(1,\tfrac12) the small grey square becomes a slightly curved shape, and the purple parallelogram — its image under the matrix JJ — almost covers it. Try Large displacement: the prediction now misses visibly. Then Singular at the origin: J=0J=0 and the square collapses.

Intuition: zoom until it is linear

A differentiable function of one variable looks like a line when you zoom in. A differentiable map of the plane looks like a linear transformation when you zoom in: small grids become grids of parallelograms. That linear transformation is the Jacobian matrix. Its columns say where the two input step directions go; its determinant says how small areas are scaled.

Definition

For F:Rn→RmF:\mathbb R^n\to\mathbb R^m with components F1,…,FmF_1,\dots,F_m, the Jacobian at pp is the m×nm\times n matrix

JF(p)=[∂Fi∂xj(p)],J_F(p)=\left[\frac{\partial F_i}{\partial x_j}(p)\right],

with one row per output and one column per input. Row ii is the gradient of FiF_i; column jj is the response of all outputs to the jj-th input. Differentiability means

F(p+δ)=F(p)+Jδ+o(∥δ∥).F(p+\boldsymbol\delta)=F(p)+J\boldsymbol\delta+o(\|\boldsymbol\delta\|).

Mechanics: what the explorer computes

The maps are polynomial, so JJ, F(p)F(p), the prediction F(p)+JδF(p)+J\boldsymbol\delta and the independent evaluation F(p+δ)F(p+\boldsymbol\delta) are exact; the squared error is an exact rational. The output plane is a local window centred near F(p)F(p). Its curved shape samples the true image of the square’s boundary; the parallelogram and the two column arrows are drawn from JJ. The Lift preset maps R2→R3\mathbb R^2\to\mathbb R^3: its 3×23\times2 Jacobian multiplies 2-vectors and returns 3-vectors, so the plane shows only two of the three outputs and says so.

Derivation: from partial derivatives to a matrix

Each component is a scalar function, so by III.8, Fi(p+δ)≈Fi(p)+∇Fi⋅δF_i(p+\boldsymbol\delta)\approx F_i(p)+\nabla F_i\cdot\boldsymbol\delta. Stacking the mm rows gives the matrix–vector product JδJ\boldsymbol\delta. Multiplying by δ=δ1e1+δ2e2\boldsymbol\delta=\delta_1\mathbf e_1+\delta_2\mathbf e_2 combines the columns: Jδ=δ1Je1+δ2Je2J\boldsymbol\delta=\delta_1J\mathbf e_1+\delta_2J\mathbf e_2. Shapes must agree: an m×nm\times n matrix multiplies only an nn-vector.

Worked example

For F=(x2−y2, 2xy)F=(x^2-y^2,\,2xy) at p=(1,12)p=(1,\tfrac12):

J=[2x−2y2y2x]=[2−112],J=\begin{bmatrix}2x&-2y\\2y&2x\end{bmatrix}=\begin{bmatrix}2&-1\\1&2\end{bmatrix},

so det⁡J=5\det J=5.

F(p)=(34,1)F(p)=(\tfrac34,1). With δ=(14,14)\boldsymbol\delta=(\tfrac14,\tfrac14), Jδ=(14,34)J\boldsymbol\delta=(\tfrac14,\tfrac34), so the prediction is (1,74)(1,\tfrac74). The independent evaluation is F(54,34)=(1,158)F(\tfrac54,\tfrac34)=(1,\tfrac{15}8). The error (0,18)(0,\tfrac18) is second order: halving δ\boldsymbol\delta quarters it.

Replay the worked derivation

Evaluate F at p

F(p) = (0.75, 1).

F(1,0.5)=[0.751]F(1,0.5)=\begin{bmatrix}0.75\\1\end{bmatrix}
Replay: evaluate F, assemble J, predict and compare with an independent evaluation.

Singular and rectangular Jacobians

At the origin the complex square has J=0J=0: every first-order change vanishes and the square shrinks by a factor of order ∥δ∥\|\boldsymbol\delta\| more than linear scaling predicts. A square Jacobian with det⁡J=0\det J=0 is not invertible, so the map cannot be locally inverted there. A rectangular Jacobian has no determinant; for R2→R3\mathbb R^2\to\mathbb R^3 the image of a small square is a small parallelogram tilted in space. Multiplying a 2×22\times2 Jacobian by a 3-vector is rejected as a shape mismatch.

Executable reference code

The reference builds exact Jacobians from partial derivatives with Fraction, enforces shapes and checks the second-order error. The NumPy version compares F(p+δ)−F(p)F(p+\boldsymbol\delta)-F(p) with JδJ\boldsymbol\delta for shrinking δ\boldsymbol\delta and confirms that the error ratio decreases.

code/python/calculus_jacobian.py
"""Exact Jacobian of F(x, y) = (x**2 - y**2, 2xy), shape checks and the second-order error (III.9)."""
from fractions import Fraction as F

F_map = lambda x, y: (x * x - y * y, 2 * x * y)
J = lambda x, y: ((2 * x, -2 * y), (2 * y, 2 * x))


def mat_vec(A, v):
    if any(len(row) != len(v) for row in A):
        raise ValueError(f"shape mismatch: {len(A)}x{len(A[0])} times a {len(v)}-vector")
    return tuple(sum(a * b for a, b in zip(row, v)) for row in A)


p, d = (F(1), F(1, 2)), (F(1, 4), F(1, 4))
pred = tuple(a + b for a, b in zip(F_map(*p), mat_vec(J(*p), d)))
actual = F_map(p[0] + d[0], p[1] + d[1])
assert pred == (1, F(7, 4)) and actual == (1, F(15, 8))

# Halving the displacement quarters the error (it is second order).
def err(scale):
    dd = (d[0] * scale, d[1] * scale)
    a = F_map(p[0] + dd[0], p[1] + dd[1]); q = tuple(x + y for x, y in zip(F_map(*p), mat_vec(J(*p), dd)))
    return sum((x - y) ** 2 for x, y in zip(a, q))


assert err(F(1, 2)) == err(F(1)) / 16                  # squared error scales by (1/2)**4
try:
    mat_vec(J(*p), (F(1), F(0), F(0)))
except ValueError:
    pass
else:
    raise AssertionError("a 2x2 Jacobian must reject a 3-vector")
code/python/calculus_jacobian_numpy.py
"""Linearisation error ratio ||F(p+d) - F(p) - J d|| / ||d|| shrinks with d (III.9)."""
import numpy as np

def F(v):
    x, y = v
    return np.array([x * x - y * y, 2 * x * y])

def J(v):
    x, y = v
    return np.array([[2 * x, -2 * y], [2 * y, 2 * x]])

p = np.array([1.0, 0.5])
direction = np.array([1.0, 1.0]) / np.sqrt(2)
ratios = []
for s in 2.0 ** -np.arange(1, 12):
    d = s * direction
    ratios.append(np.linalg.norm(F(p + d) - F(p) - J(p) @ d) / np.linalg.norm(d))
assert np.all(np.diff(ratios) < 0) and ratios[-1] < 1e-3
assert J(p).shape == (2, 2) and np.isclose(np.linalg.det(J(p)), 5.0)

Explore

On Linear map, the prediction is exact for every δ\boldsymbol\delta: the Jacobian of a linear map is the map. On Bend, notice that the second row never changes: F2=yF_2=y has gradient (0,1)(0,1). Move δ\boldsymbol\delta along e1\mathbf e_1 and watch the orange arrow align with the first column.

Common mistakes

Transposing the Jacobian (rows are outputs, columns are inputs). Multiplying in the wrong order: JδJ\boldsymbol\delta, not δJ\boldsymbol\delta J. Expecting a determinant from a non-square Jacobian. Trusting the linear prediction far from pp.

Numerical considerations

Near-singular Jacobians make local inversion (Newton’s method for systems) sensitive: small output errors become large input errors, as with ill-conditioned matrices in Part II. Finite-difference Jacobians cost one extra evaluation per input column and inherit the step-size trade-off of III.14.

Engineering and ML connections

The Jacobian of each layer maps input perturbations to output perturbations; backpropagation multiplies transposed Jacobians from the loss backward (III.10). Robotics uses manipulator Jacobians to relate joint and hand velocities; change-of-variables formulas in probability use ∣det⁡J∣|\det J|.

Exercises

calculation

Fill the Jacobian

F(x, y) = (x² − y², 2xy). Give the Jacobian at (1, 2) row by row.

calculation

Predict with J

J = [[2, −4], [4, 2]] at p with F(p) = (−3, 4). Predict F(p + δ) for δ = (0.1, 0).

calculation

Count the shape

F: ℝ³ → ℝ² is differentiable. How many rows and columns does its Jacobian have?

prediction

Columns versus rows

What does the second column of J tell you?

Choose one answer

prediction

A zero determinant

det J = 0 at the origin for F = (x² − y², 2xy). What follows?

Choose one answer

Summary and checkpoints

The Jacobian is the matrix of all first partial derivatives, m×nm\times n for F:Rn→RmF:\mathbb R^n\to\mathbb R^m. It is the local linear transformation of the map: F(p+δ)≈F(p)+JδF(p+\boldsymbol\delta)\approx F(p)+J\boldsymbol\delta, with columns as input responses and rows as output gradients. Next, III.10 shows how a computer obtains such derivatives automatically from the operations of a program.

The map of the book

97 of 97 chapters written · eight Parts

demo Foundation demo Try the live vector example Open as a page

Part I

Mathematical Language

The notation and mental objects everything else uses.

9 of 9 chapters written

  1. I.1 Scalars, quantities, units, powers and roots available
  2. I.2 Coordinates and points available
  3. I.3 Vectors: components, magnitude, and direction reference chapter available
  4. I.4 Algebraic expressions, equations and inequalities available
  5. I.5 Functions as input → transformation → output available
  6. I.6 Essential function families available
  7. I.7 Sets, intervals, and relations available
  8. I.8 Indexed quantities and summation available
  9. I.9 The notation map for ML available

Part II

Linear Algebra

Matrices as transformations: the strongest foundation in the book.

19 of 19 chapters written

  1. II.1 Vector addition and subtraction available
  2. II.2 Scalar multiplication and vector scaling available
  3. II.3 Norms, magnitude, and normalization available
  4. II.4 Dot product, angle, and orthogonality available
  5. II.5 Projection and distance to a direction available
  6. II.6 Linear combinations and span available
  7. II.7 Linear independence, basis, and coordinates available
  8. II.8 Matrices as linear transformations available
  9. II.9 Matrix × vector available
  10. II.10 Matrix × matrix as composition available
  11. II.11 Transpose and orthogonality available
  12. II.12 Determinant: orientation and area/volume scaling available
  13. II.13 Linear systems as intersecting constraints available
  14. II.14 Gaussian elimination and row operations available
  15. II.15 Rank and invertibility available
  16. II.16 Inverse matrices available
  17. II.17 Eigenvalues and eigenvectors available
  18. II.18 Orthogonal bases and least-squares geometry available
  19. II.19 Singular Value Decomposition available

Part III

Calculus

Change, local approximation, and sensitivity made visible.

15 of 15 chapters written

  1. III.1 Limits through approach available
  2. III.2 Continuity and local behavior available
  3. III.3 Derivative as secant → tangent available
  4. III.4 Derivative as rate of change and local linear model available
  5. III.5 Derivative rules available
  6. III.6 Chain rule available
  7. III.7 Multivariable functions and partial derivatives available
  8. III.8 Gradient and directional derivatives available
  9. III.9 Jacobian available
  10. III.10 Computational graphs and automatic differentiation available
  11. III.11 Hessian and curvature available
  12. III.12 Integrals as accumulation available
  13. III.13 Fundamental derivative/integral connection available
  14. III.14 Numerical differentiation available
  15. III.15 Numerical integration available

Part IV

Probability

Uncertainty through sample spaces, evidence, and repetition.

16 of 16 chapters written

  1. IV.1 Random experiments, outcomes, and sample spaces available
  2. IV.2 Events and probability mass available
  3. IV.3 Counting and finite probability where needed available
  4. IV.4 Conditional probability as restricted sample space available
  5. IV.5 Independence available
  6. IV.6 Bayes' theorem as probability-mass update available
  7. IV.7 Random variables available
  8. IV.8 PMF, PDF, and CDF available
  9. IV.9 Bernoulli and Binomial distributions available
  10. IV.10 Uniform and Gaussian distributions available
  11. IV.11 Expectation available
  12. IV.12 Variance and standard deviation available
  13. IV.13 Joint, marginal, and conditional distributions available
  14. IV.14 Covariance and correlation available
  15. IV.15 Law of Large Numbers available
  16. IV.16 Central Limit Theorem available

Part V

Statistics

From observed data to estimates, intervals, and likelihood.

10 of 10 chapters written

  1. V.1 Data summaries: mean, median, mode available
  2. V.2 Spread: variance, standard deviation, robust interpretation available
  3. V.3 Population, sample, and sampling variability available
  4. V.4 Estimators and estimation available
  5. V.5 Confidence intervals through repeated sampling available
  6. V.6 Covariance matrices and multivariate spread available
  7. V.7 Regression fundamentals available
  8. V.8 Likelihood available
  9. V.9 Maximum Likelihood Estimation available
  10. V.10 Maximum A Posteriori estimation available

Part VI

Optimization

Learning as repeated parameter updates governed by an objective.

10 of 10 chapters written

  1. VI.1 Objective functions and optimization landscapes available
  2. VI.2 Local vs global optima and stationary points available
  3. VI.3 Convexity available
  4. VI.4 Gradient descent flagship chapter available
  5. VI.5 Learning rate and convergence behavior available
  6. VI.6 Momentum available
  7. VI.7 Stochastic and mini-batch gradient descent available
  8. VI.8 Adam intuition available
  9. VI.9 Constraints and projected/penalized intuition available
  10. VI.10 Numerical stopping and optimization diagnostics available

Part VII

Information Theory

Logarithms, entropy, and the loss behind token prediction.

8 of 8 chapters written

  1. VII.1 Logarithms and log probabilities in computation available
  2. VII.2 Self-information available
  3. VII.3 Entropy available
  4. VII.4 Cross entropy and negative log-likelihood available
  5. VII.5 KL divergence available
  6. VII.6 Logits and softmax available
  7. VII.7 Numerical stability: log-sum-exp and stable softmax available
  8. VII.8 From token logits to language-model loss available

Part VIII

Numerical Computing

Why equivalent expressions behave differently on real machines.

10 of 10 chapters written

  1. VIII.1 Floating-point representation available
  2. VIII.2 Rounding and representation error available
  3. VIII.3 Error propagation and conditioning available
  4. VIII.4 Catastrophic cancellation available
  5. VIII.5 Overflow and underflow available
  6. VIII.6 Stable algorithms available
  7. VIII.7 Numerical linear algebra realities available
  8. VIII.8 Sparse matrices and sparse structure available
  9. VIII.9 Vectorized computation available
  10. VIII.10 Reproducibility, random seeds, and nondeterminism available