<p>You can't drop <span class="math-container">$dx$</span>, since it &quot;normalizes&quot; the difference in function value <span class="math-container">$df$</span>. Numerically, you assign a value to <span class="math-container">$dx$</span>, vall it <span class="math-container">$\Delta x$</span>. Then you approximate the derivative as <span class="math-container">$\frac{f(x+\Delta x)-f(x)}{\Delta x}$</span>.</p>
<p>The paper suggests that <span class="math-container">$f$</span> is already known as input data, if I'm not mistaken.</p>
<p>In a grid, you only know <span class="math-container">$f$</span> in determined coordinates (or cells). In one dimension, you function might be defined in an interval <span class="math-container">$[a,b]$</span>. This interval is discretized into <span class="math-container">$N$</span> cells, indexed using <span class="math-container">$i$</span>. So you values are <span class="math-container">$\{ f_i \}_{i=0}^N$</span>.</p>
<p>First, understand how your points are distributed. Including the boundary: <span class="math-container">$x_i = a + i \frac{b-a}{N-1}$</span> or only looking at the cell center: <span class="math-container">$x_i = a + \frac{b-a}{2N} + i \frac{b-a}{N}$</span>. For these two cases, the distance between two gridpoints where <span class="math-container">$f$</span> is known is different. But lets assume the first case, then <span class="math-container">$dx \approx \Delta x = \frac{b-a}{N-1}$</span>. This becomes your normalizing distance.</p>
<p>Then you must approximate the derivative. Note that your boundaries are &quot;problematic&quot; and one must be handled separately. We can approximate the first order derivative for <span class="math-container">$1 \leq i&lt;N-1$</span> as: <span class="math-container">$\frac{df(x_i)}{dx} \approx \frac{f_{i+1}-f_i}{\Delta x}$</span>. As you might figure out, when <span class="math-container">$i=N$</span> we can't index <span class="math-container">$i+1$</span>. So we use the reverse derivative: <span class="math-container">$\frac{df(x_i)}{dx} \approx \frac{f_N-f_{N-1}}{\Delta x}$</span>. Note that you might want higher order derivatives instead to avoid artifacts.</p>
<p>In higher dimensions you apply the same logic on each separate axis or index. If <span class="math-container">$f(\vec{x}): \mathbb{R}^3 \rightarrow \mathbb{R}$</span>, then we will index <span class="math-container">$f$</span> as <span class="math-container">$f_{ijk}$</span> or whatever characters you want to use.</p>
<p>Edit: corrected a mistake</p>

CC BY-SA 4.0
2023-03-06T22:56:09.140Z
Are there any concrete examples you can point to? I'm not able to follow most of that :(
Mote Zart
CC BY-SA 4.0
2023-03-07T09:46:34.613Z
I implemented a small demo at shadertoy: https://www.shadertoy.com/view/msy3Rh
Mathis
CC BY-SA 4.0
2023-03-07T14:37:46.983Z
That's an awesome demo!
Mote Zart
CC BY-SA 4.0
2023-03-06T09:02:54.687Z
2023-03-06T09:02:54.687Z
Mathis
<p>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 <span class="math-container">$f$</span> is in my case. In reviewing examples, I'm seeing the <span class="math-container">$f$</span> function being provided. Like in the Stewart Calculus <a href="https://math.libretexts.org/Bookshelves/Calculus/Map%3A_Calculus__Early_Transcendentals_(Stewart)/14%3A_Partial_Derivatives/14.03%3A_Partial_Derivatives" rel="nofollow noreferrer">textbook</a>, the first given example defines a function <span class="math-container">$(,)=^2−3+2^2−4+5−12$</span></p>
<p><strong>My Basic Computation in 1D</strong></p>
<p>I'm told that we have <span class="math-container">$f(x)$</span> (black squiggly)</p>
<p><a href="https://i.sstatic.net/hgy96.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/hgy96.jpg" alt="enter image description here" /></a></p>
<p>Then for the derivative at any point on <span class="math-container">$x$</span>, say <span class="math-container">$x_i$</span>, I've been told I can use:</p>
<p><span class="math-container">$f`(x_i) = df(x_i)/dx = [f(x_i) - f(x_i-1)]/dx$</span>.</p>
<p>I'm told <span class="math-container">$dx$</span> is constant, so it is dropped:</p>
<p><span class="math-container">$f`(x_i) = df(x_i)/dx = [f(x_i) - f(x_i-1)]$</span>.</p>
<p>But what is <span class="math-container">$f$</span>?</p>
<p>What if I have <span class="math-container">$x_i=2$</span>, for example? Or any number, since that <em>is</em> the info I do know. Please explain what I do, or how I ascertain <span class="math-container">$f$</span>.</p>
<p><strong>Working in 3D</strong></p>
<p>Once I understand the basics, I need to compute the values for 3D, meaning I need partial derivatives for <span class="math-container">$x$</span>, <span class="math-container">$y$</span> and <span class="math-container">$z$</span>. I've been given the formulas to achieve this:</p>
<p><span class="math-container">$\frac{}{} =\frac{1}{2}(f(x_{i+1}, y_j, z_k,) - f(x_{i-1}, y_j, z_k))$</span>
<span class="math-container">$\frac{}{y} =\frac{1}{2}(f(x_i, y_{j+1}, z_k,) - f(x_i, y_{j-1}, z_k))$</span>
<span class="math-container">$\frac{}{z} =\frac{1}{2}(f(x_i, y_j, z_{k+1}) - f(x_i, y_j, z_{k-1}))$</span></p>
<p>I have the required <span class="math-container">$x$</span>, <span class="math-container">$y$</span> and <span class="math-container">$z$</span> ranges, so if I can understand the math, my goal is compute the three above values.</p>
<p>Note: based on this <a href="https://graphics.stanford.edu/papers/volume-cga88/volume.pdf" rel="nofollow noreferrer">paper</a></p>

CC BY-SA 4.0
2023-03-05T20:28:45.787Z
$f$ is some function defined on your voxel grid. For example a density.
lightxbulb
<raytracing><mathematics><volume-rendering>
How to compute discrete partial derivatives in 3D
Mote Zart
# How to compute discrete partial derivatives in 3D

(Asked by: username_0 on 2023-03-05)

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](https://math.libretexts.org/Bookshelves/Calculus/Map%3A_Calculus__Early_Transcendentals_(Stewart)/14%3A_Partial_Derivatives/14.03%3A_Partial_Derivatives), 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)

[![enter image description here](https://i.sstatic.net/hgy96.jpg)](https://i.sstatic.net/hgy96.jpg)

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](https://graphics.stanford.edu/papers/volume-cga88/volume.pdf)

**Tags:** raytracing, mathematics, volume-rendering

> **Comments:**
> **username_1**: $f$ is some function defined on your voxel grid. For example a density.  

---

## 1 answers

### Answer by username_2 on 2023-03-06. Score: 1

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<N-1$ 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

> **Comments:**
> **username_0**: Are there any concrete examples you can point to? I'm not able to follow most of that :(  
> **username_2**: I implemented a small demo at shadertoy: https://www.shadertoy.com/view/msy3Rh  
> **username_0**: That's an awesome demo!  

---

