Julius Ikkala

How I learned to stop worrying and love the Jacobian matrix

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→R2f: \mathbb{R}^2 \to \mathbb{R}^2 (or float2 f(float2 p) for the code-oriented people). The Jacobian matrix is then:

Jf(p)=[∂f1(p)∂p1∂f1(p)∂p2∂f2(p)∂p1∂f2(p)∂p2] \mathbf{J_f}(p) = \begin{bmatrix} \frac{\partial f_1(p)}{\partial p_1} & \frac{\partial f_1(p)}{\partial p_2} \\ \frac{\partial f_2(p)}{\partial p_1} & \frac{\partial f_2(p)}{\partial p_2} \end{bmatrix}

where f1(p)f_1(p) is f(p).x and p1p_1 is p.x. To get some kind of intuitive hold on the concept, Jf(p)\mathbf{J_f}(p) is the linear transformation matrix? that ff applies to space around pp.

As a result, if you want to know how much ff "stretches" at pp, you take the determinant of this Jacobian matrix to get the Jacobian determinant, ∣Jf(p)∣|\mathbf{J_f}(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.

Hank Hill at the computer graphics center having trouble with 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.

Finally, the excellent book "Physically Based Rendering: From Theory To Implementation" was what made the usefulness of Jacobians clear to me. We very rarely care about the Jacobian matrix itself, but its determinant is ubiquitous in graphics.

You cannot escape the Jacobian.

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≈1N∑i=1Nf(xi)p(xi) \int_\Omega f(x) dx \approx \frac{1}{N}\sum_{i=1}^{N} \frac{f(x_i)}{p(x_i)}

where xi∈Ωx_i \in \Omega is a random variable with the distribution defined by the probability density function p(xi)p(x_i). When we're sampling xix_i, we usually start by plucking a random uniformly distributed sample ξi\xi_i in the primary sample space [0,1)N[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)x_i = T(\xi_i). So it turns out that:

p(xi)=p(T(ξi))=p(ξi)∣JT(ξi)∣=1∣JT(ξi)∣p(x_i) = p(T(\xi_i)) = \frac{p(\xi_i)}{|\mathbf{J_T}(\xi_i)|} = \frac{1}{|\mathbf{J_T}(\xi_i)|}.

PDFs are just the inverses of Jacobian determinants, and these days you can actually get a PDF automatically for a given mapping function in shader code.

Example: equirectangular environment map

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 360 image with equirectangular projection, depicting a sunset seen from rocky hills.
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)f(p) to map our texture coordinate pp to a direction vector. HDRIs are often stored with the equirectangular projection:?

g(p)=[−cos⁡(2πp1)⋅sin⁡(πp2)cos⁡(πp2)−sin⁡(2πp1)⋅sin⁡(πp2)] g(p) = \begin{bmatrix} -\cos(2 \pi p_1) \cdot \sin(\pi p_2) \\ \cos(\pi p_2) \\ -\sin(2 \pi p_1) \cdot \sin(\pi p_2) \\ \end{bmatrix}

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→R3g: \mathbb{R}^2 \to \mathbb{R}^3. We can luckily just add an extra parameter that we don't actually care much about:

G(r,p)=r⋅[−cos⁡(2πp1)⋅sin⁡(πp2)cos⁡(πp2)−sin⁡(2πp1)⋅sin⁡(πp2)] G(r, p) = r \cdot \begin{bmatrix} -\cos(2 \pi p_1) \cdot \sin(\pi p_2) \\ \cos(\pi p_2) \\ -\sin(2 \pi p_1) \cdot \sin(\pi p_2) \\ \end{bmatrix}

where rr represents the length of the resulting vector so that it is no longer a unit vector. Now, G:R3→R3G: \mathbb{R}^3 \to \mathbb{R}^3, and we can compute the Jacobian matrix:

Jf=[∂G1(p)∂r∂G1(p)∂p1∂G1(p)∂p2∂G2(p)∂r∂G2(p)∂p1∂G2(p)∂p2∂G3(p)∂r∂G3(p)∂p1∂G3(p)∂p2]=[−cos⁡(2πp1)sin⁡(πp2)2rπsin⁡(2πp1)sin⁡(πp2)−rπcos⁡(2πp1)cos⁡(πp2)cos⁡(πp2)0−rπsin⁡(πp2)−sin⁡(2πp1)sin⁡(πp2)−2rπcos⁡(2πp1)sin⁡(πp2)−rπsin⁡(2πp1)cos⁡(πp2)] \begin{align*} \mathbf{J_f} &= \begin{bmatrix} \frac{\partial G_1(p)}{\partial r} & \frac{\partial G_1(p)}{\partial p_1} & \frac{\partial G_1(p)}{\partial p_2}\\ \frac{\partial G_2(p)}{\partial r} & \frac{\partial G_2(p)}{\partial p_1} & \frac{\partial G_2(p)}{\partial p_2}\\ \frac{\partial G_3(p)}{\partial r} & \frac{\partial G_3(p)}{\partial p_1} & \frac{\partial G_3(p)}{\partial p_2} \end{bmatrix} \\ &= \begin{bmatrix} -\cos(2 \pi p_1) \sin(\pi p_2) & 2r\pi\sin(2 \pi p_1) \sin(\pi p_2) & -r\pi \cos(2 \pi p_1) \cos(\pi p_2) \\ \cos(\pi p_2) & 0 & -r\pi\sin(\pi p_2)\\ -\sin(2 \pi p_1) \sin(\pi p_2) & -2r\pi\cos(2 \pi p_1) \sin(\pi p_2) & -r\pi \sin(2 \pi p_1) \cos(\pi p_2) \\ \end{bmatrix} \end{align*}

Now, because we actually just cared about unit vectors, we can set r=1r=1 and compute the determinant:

∣Jf∣=2π2cos⁡(2πp1)2cos⁡(πp2)2sin⁡(πp2)+2π2cos⁡(2πp1)2sin⁡(πp2)3+2π2sin⁡(2πp1)2sin⁡(πp2)cos⁡(πp2)2+2π2sin⁡(2πp1)2sin⁡(πp2)3 \begin{align*} |\mathbf{J_f}| &= 2\pi^2 \cos(2\pi p_1)^2 \cos(\pi p_2)^2 \sin(\pi p_2) + 2\pi^2\cos(2\pi p_1)^2\sin(\pi p_2)^3 \\ &+ 2\pi^2\sin(2\pi p_1)^2\sin(\pi p_2)\cos(\pi p_2)^2 + 2\pi^2\sin(2\pi p_1)^2\sin(\pi p_2)^3 \end{align*}

We can simplify this a lot with one trigonometric identity, cos⁡2x+sin⁡2x=1\cos^2 x + \sin^2 x = 1:

∣Jf∣=2π2cos⁡(πp2)2sin⁡(πp2)(cos⁡(2πp1)2+sin⁡(2πp1)2)+2π2sin⁡(πp2)3(cos⁡(2πp1)2+sin⁡(2πp1)2)=2π2sin⁡(πp2)(cos⁡(πp2)2+sin⁡(πp2)2)=2π2sin⁡(πp2) \begin{align*} |\mathbf{J_f}| &= 2\pi^2\cos(\pi p_2)^2\sin(\pi p_2)(\cos(2\pi p_1)^2 + \sin(2\pi p_1)^2) \\ & + 2\pi^2\sin(\pi p_2)^3(\cos(2\pi p_1)^2 + \sin(2\pi p_1)^2) \\ &= 2\pi^2\sin(\pi p_2) (\cos(\pi p_2)^2 + \sin(\pi p_2)^2) \\ &= 2\pi^2\sin(\pi p_2) \end{align*}

Next, we can quickly check if this result makes sense. We know that the surface area of a unit sphere is 4π4\pi. When we map our [0,1)2[0, 1)^2 unit square to the sphere, that means that overall it should get scaled by a factor of 4π4 \pi. Or more formally,

∫01∫01∣Jf(p1,p2)∣dp1dp2=∫01∫012π2sin⁡(πp2)dp1dp2=2π2∫01sin⁡(πp2)dp2=2π2⋅2π=4π \begin{align*} \int_0^1 \int_0^1 |\mathbf{J_f}(p_1, p_2)| dp_1 dp_2 &= \int_0^1 \int_0^1 2\pi^2\sin(\pi p_2) dp_1 dp_2\\ &= 2\pi^2 \int_0^1 \sin(\pi p_2) dp_2 = 2\pi^2 \cdot \frac{2}{\pi} = 4\pi \end{align*}

Now, to calculate the average color of the image properly, we can weigh each pixel by its Jacobian:

14π∫01∫01c(p1,p2)⋅∣Jf(p1,p2)∣dp1dp2≈14πwh∑i=0w−1∑j=0h−1c(i+0.5w,j+0.5h)⋅∣Jf(i+0.5w,j+0.5h)∣=π2wh∑i=0w−1∑j=0h−1c(i+0.5w,j+0.5h)⋅sin⁡(π⋅j+0.5h) \begin{align*} &\frac{1}{4\pi}\int_0^1 \int_0^1 c(p_1, p_2) \cdot |\mathbf{J_f}(p_1, p_2)| dp_1 dp_2 \\ &\approx \frac{1}{4\pi w h}\sum_{i=0}^{w-1}\sum_{j=0}^{h-1} c\left(\frac{i+0.5}{w}, \frac{j+0.5}{h}\right) \cdot \left|\mathbf{J_f}\left(\frac{i+0.5}{w}, \frac{j+0.5}{h}\right)\right| \\ &= \frac{\pi}{2 w h}\sum_{i=0}^{w-1}\sum_{j=0}^{h-1} c\left(\frac{i+0.5}{w}, \frac{j+0.5}{h}\right) \cdot \sin\left(\pi \cdot \frac{j+0.5}{h} \right) \end{align*}

Or in terms of code:

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⁡\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⁡\arctan mess. Maybe I'll write about that some day.


  1. Tomas Akenine-Möller et al. "Improved Shader and Texture Level of Detail Using Ray Cones". 2021. ↩︎