How I learned to stop worrying and love the Jacobian matrix
2026-09-09
The Jacobian matrix
is one of those concepts that would belong somewhere below the waterline in a
"computer graphics iceberg". It's one of those things underpinning a massive
amount of computer graphics with many practicioners not even realizing it. You
can get surprisingly far without ever even encountering them, although that can
make you prone to subtle mistakes.
Jacobians are not a particularly complex concept if you already know what a
derivative and what a matrix is. They're basically just derivatives for functions
with multiple inputs and outputs, or "vector-valued functions". This turns out
to be a matrix, as it consists of the partial derivative of each "output" of the
function wrt. each "input".
Say you have a function f:R2→R2 (or
float2 f(float2 p) for the code-oriented people). The Jacobian matrix is then:
where f1(p) is f(p).x and p1 is p.x. To get some kind of intuitive
hold on the concept, Jf(p) is the linear transformation
matrix?
that f applies to space around p.
As a result, if you want to know how much
f "stretches" at p, you take the determinant of this Jacobian matrix to get
the Jacobian determinant, ∣Jf(p)∣. The Jacobian determinant also
happens to be the thing that curiously appears in graphics time and time again
if you dig deep enough.
I used to worry about Jacobians
I started my graphics programming journey during my teenage years and learned graphics
by coding. Back before hardware ray tracing hit the scene, you could basically
implement a feature-complete, modern real-time renderer without ever having
heard of Jacobians.
When I started implementing Monte Carlo stuff (like path tracing), I finally
started seeing Jacobians mentioned in relevant research papers. Initially, I
was somewhat dumbfounded as to how the authors even realized that they needed to
apply a "Jacobian determinant" to correct for something. This caused a certain
level of uneasiness, as I started worrying if I've missed cases where I haven't
corrected for something where I would've needed the Jacobian determinant
(spoiler: I had). Still, I usually glossed over those parts and took whatever
equations the paper authors landed at as a given.
I eventually learned what Jacobians are on a linear algebra course in university,
but even then, it wasn't immediately obvious what I would want to use them for.
Of course, these days "differentiable rendering" would be the obvious knee-jerk
answer, where the need for such "vector derivative" is rather self-evident.
Jacobians turn up everywhere. For example, you run into them when implementing
a simple path tracer with any kind of importance sampling. The basic Monte Carlo
integration formula for importance sampling is as follows:
∫Ωf(x)dx≈N1i=1∑Np(xi)f(xi)
where xi∈Ω is a random variable with the distribution defined by
the probability density function p(xi). When we're sampling xi, we usually
start by plucking a random uniformly distributed sample ξi in the
primary sample space[0,1)N?
(or for coding-oriented people, a list of random floats from 0 to 1) and map it
such that xi=T(ξi). So it turns out that:
One of the cases where my Jacobian skill issues had had an adverse effect was
in relation to environment maps / HDRIs. These are usually used as "background
images" in computer graphics?.
A HDRI called "Cape Hill" from Poly Haven. Notice how the image stretches near the top and bottom.
When we map this 2D image onto an infinitely distant sphere surrounding the
camera, we need a function f(p) to map our texture coordinate p to a
direction vector. HDRIs are often stored with the equirectangular projection:?
My actual use cases were related to spherical harmonics,
but let's keep it simple and say we just want to compute the average color of
this image.?
One might assume that this problem is completely trivial; let's just take the
average of each pixel's color. However, doing so would actually produce
incorrect results if we consider the image to actually be spherical! That is
because while the pixels do have equal sizes in the 2D image, they are no longer
the same size when mapped to a sphere using the equirectangular projection.
The equirectangular projection isn't equal-area.
Pixels towards the top and bottom of the image cover a smaller region on the
sphere than the ones near the center. Or in other words, they're distributed
more densely towards the poles of the sphere.
Luckily, we have just the tool to determine how the pixels get scaled when they
are mapped to the sphere: the Jacobian determinant! Although, there is one
little snag here. We can only compute the determinant for square matrices, but
it looks like our function will have a 2x3 matrix instead, as g:R2→R3.
We can luckily just add an extra parameter that we don't actually care much about:
Next, we can quickly check if this result makes sense. We know that the surface
area of a unit sphere is 4π. When we map our [0,1)2 unit square
to the sphere, that means that overall it should get scaled by a factor of 4π.
Or more formally,
float3 sumColor = float3(0);for (int i = 0; i < w; ++i)for (int j = 0; j < h; ++j){ float u = (i + 0.5) / w; float v = (j + 0.5) / h; sumColor += readTexture(u, v) * sin(M_PI * v);}float3 avgColor = sumColor * M_PI / (2 * w * h);
From this, it is hopefully clear that results can be extremely different
compared to just naively averaging pixels without any weighting. Without the
sin(M_PI * v) weight, the poles will get massively overrepresented in the average.
Learning to love the Jacobians
Jacobians help tie concepts together on a fundamental level. It reveals that
mapping and importance sampling are ultimately the same thing. It also gives us
the tools to understand what "equal-area projection" truly entails: it's just a
mapping that has a constant Jacobian determinant. A lot of problems can be
significantly simplified by using a Jacobian determinants.
In the above environment map averaging exercise, it would've been possible for
us to also compute the solid angle of each texel and use that as the weight.
However, that easily gets far more complicated and numerically unstable
(typically involving differences between 4 arctan functions), and may be
fundamentally less correct if you consider pixels to be point samples instead of "little squares".
?
Having a good handle on Jacobians can be useful in a lot of surprising
circumstances. Recently, I found a good use for them in the calculation of the
initial cone angle for the "ray cone"-based mipmapping approximation[1], exactly
to avoid a similar arctan mess. Maybe I'll write about that some day.