# How to compute discrete partial derivatives in 3D

I'm supposed to compute partial derivatives for 3D volume rendering (in ray tracing). I'm bad at math and so I can't understand what $$f$$ is in my case. In reviewing examples, I'm seeing the $$f$$ function being provided. Like in the Stewart Calculus textbook, the first given example defines a function $$𝑓(𝑥,𝑦)=𝑥^2−3𝑥𝑦+2𝑦^2−4𝑥+5𝑦−12$$

My Basic Computation in 1D

I'm told that we have $$f(x)$$ (black squiggly) Then for the derivative at any point on $$x$$, say $$x_i$$, I've been told I can use:

$$f(x_i) = df(x_i)/dx = [f(x_i) - f(x_i-1)]/dx$$.

I'm told $$dx$$ is constant, so it is dropped:

$$f(x_i) = df(x_i)/dx = [f(x_i) - f(x_i-1)]$$.

But what is $$f$$?

What if I have $$x_i=2$$, for example? Or any number, since that is the info I do know. Please explain what I do, or how I ascertain $$f$$.

Working in 3D

Once I understand the basics, I need to compute the values for 3D, meaning I need partial derivatives for $$x$$, $$y$$ and $$z$$. I've been given the formulas to achieve this:

$$\frac{𝜕𝑓}{𝜕𝑥} =\frac{1}{2}(f(x_{i+1}, y_j, z_k,) - f(x_{i-1}, y_j, z_k))$$ $$\frac{𝜕𝑓}{𝜕y} =\frac{1}{2}(f(x_i, y_{j+1}, z_k,) - f(x_i, y_{j-1}, z_k))$$ $$\frac{𝜕𝑓}{𝜕z} =\frac{1}{2}(f(x_i, y_j, z_{k+1}) - f(x_i, y_j, z_{k-1}))$$

I have the required $$x$$, $$y$$ and $$z$$ ranges, so if I can understand the math, my goal is compute the three above values.

Note: based on this paper

• $f$ is some function defined on your voxel grid. For example a density. Mar 5 at 20:28

You can't drop $$dx$$, since it "normalizes" the difference in function value $$df$$. Numerically, you assign a value to $$dx$$, vall it $$\Delta x$$. Then you approximate the derivative as $$\frac{f(x+\Delta x)-f(x)}{\Delta x}$$.

The paper suggests that $$f$$ is already known as input data, if I'm not mistaken.

In a grid, you only know $$f$$ in determined coordinates (or cells). In one dimension, you function might be defined in an interval $$[a,b]$$. This interval is discretized into $$N$$ cells, indexed using $$i$$. So you values are $$\{ f_i \}_{i=0}^N$$.

First, understand how your points are distributed. Including the boundary: $$x_i = a + i \frac{b-a}{N-1}$$ or only looking at the cell center: $$x_i = a + \frac{b-a}{2N} + i \frac{b-a}{N}$$. For these two cases, the distance between two gridpoints where $$f$$ is known is different. But lets assume the first case, then $$dx \approx \Delta x = \frac{b-a}{N-1}$$. This becomes your normalizing distance.

Then you must approximate the derivative. Note that your boundaries are "problematic" and one must be handled separately. We can approximate the first order derivative for $$1 \leq i as: $$\frac{df(x_i)}{dx} \approx \frac{f_{i+1}-f_i}{\Delta x}$$. As you might figure out, when $$i=N$$ we can't index $$i+1$$. So we use the reverse derivative: $$\frac{df(x_i)}{dx} \approx \frac{f_N-f_{N-1}}{\Delta x}$$. Note that you might want higher order derivatives instead to avoid artifacts.

In higher dimensions you apply the same logic on each separate axis or index. If $$f(\vec{x}): \mathbb{R}^3 \rightarrow \mathbb{R}$$, then we will index $$f$$ as $$f_{ijk}$$ or whatever characters you want to use.

Edit: corrected a mistake

• Are there any concrete examples you can point to? I'm not able to follow most of that :( Mar 6 at 22:56
• I implemented a small demo at shadertoy: shadertoy.com/view/msy3Rh Mar 7 at 9:46
• That's an awesome demo! Mar 7 at 14:37