<p>Since the question was somewhat clarified I will formalize both the question and the answer for future readers.
Having a differentiable scalar field <span class="math-container">$f : \mathbb{R}^4 \rightarrow \mathbb{R}$</span> we want to find the gradient of the field with respect to <span class="math-container">$\theta, \phi$</span> on the 2-manifold defined parametrically by: 
<span class="math-container">$$(x(\theta,\phi), y(\theta,\phi) z(\theta,\phi), w(\theta,\phi)) = (r_0\cos\theta, r_0\sin\theta, r_1\cos\phi,r_1\sin\phi)$$</span>
Where <span class="math-container">$r_0,r_1$</span> are constants. We can compute the partial derivatives as follows:
<span class="math-container">$$\frac{\partial f}{\partial \theta} = \frac{\partial f}{\partial x}\frac{\partial x}{\partial \theta} + \frac{\partial f}{\partial y}\frac{\partial y}{\partial \theta} + \frac{\partial f}{\partial z}\frac{\partial z}{\partial \theta} + \frac{\partial f}{\partial w}\frac{\partial w}{\partial \theta} \\
= -r_0\frac{\partial f}{\partial x}\sin\theta + r_0\frac{\partial f}{\partial y}\cos\theta$$</span>
<span class="math-container">$$\frac{\partial f}{\partial \phi} = 
\frac{\partial f}{\partial x}\frac{\partial x}{\partial \phi} + \frac{\partial f}{\partial y}\frac{\partial y}{\partial \phi} + \frac{\partial f}{\partial z}\frac{\partial z}{\partial \phi} + \frac{\partial f}{\partial w}\frac{\partial w}{\partial \phi} \\ = -r_1\frac{\partial f}{\partial x}\sin\phi + r_1\frac{\partial f}{\partial y}\cos\phi$$</span></p>

<p>Assuming that the gradient <span class="math-container">$\nabla f = (\frac{\partial f}{\partial x},\frac{\partial f}{\partial y},\frac{\partial f}{\partial z},\frac{\partial f}{\partial w})$</span> is known, it is straightforward to compute the 2d gradient.</p>

<p>Note that another possibility is to compute the gradient numerically (in case you don't have the partial derivatives of <span class="math-container">$f$</span>):
<span class="math-container">$$ \frac{\partial f}{\partial \theta} \approx \frac{f(r_0\cos(\theta+h_{\theta}), r_0\sin(\theta+h_{\theta}), r_1\cos\phi,r_1\sin\phi) - f(r_0\cos\theta, r_0\sin\theta, r_1\cos\phi,r_1\sin\phi)}{h_{\theta}}$$</span>
<span class="math-container">$$ \frac{\partial f}{\partial \phi} \approx \frac{f(r_0\cos\theta, r_0\sin\theta, r_1\cos(\phi+h_{\phi}),r_1\sin(\phi+h_{\phi})) - f(r_0\cos\theta, r_0\sin\theta, r_1\cos\phi,r_1\sin\phi)}{h_{\phi}}$$</span>
For <span class="math-container">$h_{\theta}, h_{\phi}$</span> chosen adequately (note that the choice of these parameters is a nontrivial problem).</p>

CC BY-SA 4.0
2019-01-31T15:43:52.293Z
2019-01-31T15:43:52.293Z
lightxbulb
<p>If I use a 4D noise function which returns a noise value and a four-part analytically derived gradient vector, I can create a 2D tile which is seamless (i.e. the opposite edges match) through remapping <code>x,y</code> and <code>z,w</code> coordinates to independent circles, i.e.</p>

<p><span class="math-container">$\ \theta = a*2*\pi$</span><br>
<span class="math-container">$\ \phi = b*2*\pi$</span></p>

<p>where <span class="math-container">$a$</span> and <span class="math-container">$b$</span> are a point on the surface. They should be within the range <span class="math-container">$[0, size)$</span> where size is some arbitrary shared dimension for the surface.</p>

<p>We can then plug <span class="math-container">$\theta$</span> and <span class="math-container">$\phi$</span> into the below functions to get the equivalent point in 4D space.</p>

<p><span class="math-container">$\ x = r_0*\cos(\theta)$</span><br>
<span class="math-container">$\ y = r_0*\sin(\theta)$</span><br>
<span class="math-container">$\ z = r_1*\cos(\phi) $</span><br>
<span class="math-container">$\ w = r_1*\sin(\phi)$</span></p>

<p>Effectively the surface created through this coordinate mapping is a <a href="https://en.wikipedia.org/wiki/Torus#Flat_torus" rel="nofollow noreferrer">flat torus</a>. </p>

<p>However, since the noise function operates in 4D space I'm left with a 4D gradient vector. Is it possible to transform this 4D vector into a 2D vector which describes the gradient of the surface?</p>

CC BY-SA 4.0
2019-01-30T22:10:43.400Z
What surface are you talking about? It's not obvious what you want from your question.
lightxbulb
CC BY-SA 4.0
2019-01-30T22:58:24.770Z
@lightxbulb Added some extra information, and tweaked the title. I'm looking to "unwrap" a torus in four dimensions to create a two-dimensional tile, this tile (or surface) will contain noise values which could be used for any form of procedural texture (such as a height map). It is possible to also retrieve the gradient at each point on this surface, however since they're 4D I don't know how to translate that to 2D (or even if it is possible).
Mark A. Ropper
CC BY-SA 4.0
2019-01-30T23:16:15.150Z
Can you define the map that you use to transform it into 2 dimensions? Because currently you have a surface in 4 dimensions, which you can't even embed in 3d. It's also not obvious what you want to do with the gradient vector. It simply gives you the direction in which your function increases the fastest, and there are infinitely many mappings to 2D.
lightxbulb
CC BY-SA 4.0
2019-01-31T11:40:30.973Z
@lightxbulb [This page](http://ronvalstar.nl/creating-tileable-noise-maps) might help convey the coordinate conversion. I've also added more information to the question. My end goal is something similar to [this picture](https://catlikecoding.com/unity/tutorials/simplex-noise/02-04-3d.png). I'm essentially trying to produce a matching heightmap and normal map?
Mark A. Ropper
CC BY-SA 4.0
2019-01-31T13:36:14.540Z
Ok, I believe that article clarified what you're doing: you're taking a 2-manifold slice out of some 4-dimensional scalar field. What is still unclear is what you want to do with that gradient, because it is indeed 4D. Assuming that you want the gradient of the image, then there are a few possibilities. The obvious is to take finite differences from your image, then you get the 2D gradient. Another possibility is to try and taking the derivative analytically. Let $f(x,y,z,w)$ be your scalar field (the noise function), the I take it that image space for you is really $\theta, \phi$, that is...
lightxbulb
CC BY-SA 4.0
2019-01-31T13:41:10.097Z
You can take the partial derivatives with respect to $\theta, \phi$: $\frac{\partial f}{\partial \theta} = -\frac{\partial f}{\partial x}\sin\theta + \frac{\partial f}{\partial y}\cos\theta$ and similarly:  $\frac{\partial f}{\partial \phi} = -\frac{\partial f}{\partial z}\sin\phi + \frac{\partial f}{\partial w}\cos\phi$ where I have used the chain rule for taking a partial derivative of a multivariable composite function (the last two terms for $\theta$ are $0$, same for the first two of $\phi$). If this answers your question I can formulate it as an answer.
lightxbulb
CC BY-SA 4.0
2019-01-31T14:21:42.270Z
@lightxbulb I'm aiming for the analytical solution, your partial derivatives certainly look like the solution! Thank you!
Mark A. Ropper
CC BY-SA 4.0
2019-01-31T15:45:40.683Z
I had forgotten the $r_0$ and $r_1$ terms from the differentiation, refer to the answer for the correct expression.
lightxbulb
<noise><normal-mapping><vectors>
Finding the normals of a tileable 2D surface extracted from 4D space
Mark A. Ropper
# Finding the normals of a tileable 2D surface extracted from 4D space

(Asked by: username_0 on 2019-01-30)

If I use a 4D noise function which returns a noise value and a four-part analytically derived gradient vector, I can create a 2D tile which is seamless (i.e. the opposite edges match) through remapping `x,y` and `z,w` coordinates to independent circles, i.e.

$\ \theta = a\*2\*\pi$  
$\ \phi = b\*2\*\pi$

where $a$ and $b$ are a point on the surface. They should be within the range $[0, size)$ where size is some arbitrary shared dimension for the surface.

We can then plug $\theta$ and $\phi$ into the below functions to get the equivalent point in 4D space.

$\ x = r\_0\*\cos(\theta)$  
$\ y = r\_0\*\sin(\theta)$  
$\ z = r\_1\*\cos(\phi) $  
$\ w = r\_1\*\sin(\phi)$

Effectively the surface created through this coordinate mapping is a [flat torus](https://en.wikipedia.org/wiki/Torus#Flat_torus).

However, since the noise function operates in 4D space I'm left with a 4D gradient vector. Is it possible to transform this 4D vector into a 2D vector which describes the gradient of the surface?

**Tags:** noise, normal-mapping, vectors

> **Comments:**
> **username_1**: What surface are you talking about? It's not obvious what you want from your question.  
> **username_0**: @username_1 Added some extra information, and tweaked the title. I'm looking to "unwrap" a torus in four dimensions to create a two-dimensional tile, this tile (or surface) will contain noise values which could be used for any form of procedural texture (such as a height map). It is possible to also retrieve the gradient at each point on this surface, however since they're 4D I don't know how to translate that to 2D (or even if it is possible).  
> **username_1**: Can you define the map that you use to transform it into 2 dimensions? Because currently you have a surface in 4 dimensions, which you can't even embed in 3d. It's also not obvious what you want to do with the gradient vector. It simply gives you the direction in which your function increases the fastest, and there are infinitely many mappings to 2D.  
> **username_0**: @username_1 [This page](http://ronvalstar.nl/creating-tileable-noise-maps) might help convey the coordinate conversion. I've also added more information to the question. My end goal is something similar to [this picture](https://catlikecoding.com/unity/tutorials/simplex-noise/02-04-3d.png). I'm essentially trying to produce a matching heightmap and normal map?  
> **username_1**: Ok, I believe that article clarified what you're doing: you're taking a 2-manifold slice out of some 4-dimensional scalar field. What is still unclear is what you want to do with that gradient, because it is indeed 4D. Assuming that you want the gradient of the image, then there are a few possibilities. The obvious is to take finite differences from your image, then you get the 2D gradient. Another possibility is to try and taking the derivative analytically. Let $f(x,y,z,w)$ be your scalar field (the noise function), the I take it that image space for you is really $\theta, \phi$, that is...  
> **username_1**: You can take the partial derivatives with respect to $\theta, \phi$: $\frac{\partial f}{\partial \theta} = -\frac{\partial f}{\partial x}\sin\theta + \frac{\partial f}{\partial y}\cos\theta$ and similarly:  $\frac{\partial f}{\partial \phi} = -\frac{\partial f}{\partial z}\sin\phi + \frac{\partial f}{\partial w}\cos\phi$ where I have used the chain rule for taking a partial derivative of a multivariable composite function (the last two terms for $\theta$ are $0$, same for the first two of $\phi$). If this answers your question I can formulate it as an answer.  
> **username_0**: @username_1 I'm aiming for the analytical solution, your partial derivatives certainly look like the solution! Thank you!  
> **username_1**: I had forgotten the $r_0$ and $r_1$ terms from the differentiation, refer to the answer for the correct expression.  

---

## 1 answers

### Answer by username_1 on 2019-01-31. Score: 2 | Accepted answer

Since the question was somewhat clarified I will formalize both the question and the answer for future readers.
Having a differentiable scalar field $f : \mathbb{R}^4 \rightarrow \mathbb{R}$ we want to find the gradient of the field with respect to $\theta, \phi$ on the 2-manifold defined parametrically by:
$$(x(\theta,\phi), y(\theta,\phi) z(\theta,\phi), w(\theta,\phi)) = (r\_0\cos\theta, r\_0\sin\theta, r\_1\cos\phi,r\_1\sin\phi)$$
Where $r\_0,r\_1$ are constants. We can compute the partial derivatives as follows:
$$\frac{\partial f}{\partial \theta} = \frac{\partial f}{\partial x}\frac{\partial x}{\partial \theta} + \frac{\partial f}{\partial y}\frac{\partial y}{\partial \theta} + \frac{\partial f}{\partial z}\frac{\partial z}{\partial \theta} + \frac{\partial f}{\partial w}\frac{\partial w}{\partial \theta} \\
= -r\_0\frac{\partial f}{\partial x}\sin\theta + r\_0\frac{\partial f}{\partial y}\cos\theta$$
$$\frac{\partial f}{\partial \phi} =
\frac{\partial f}{\partial x}\frac{\partial x}{\partial \phi} + \frac{\partial f}{\partial y}\frac{\partial y}{\partial \phi} + \frac{\partial f}{\partial z}\frac{\partial z}{\partial \phi} + \frac{\partial f}{\partial w}\frac{\partial w}{\partial \phi} \\ = -r\_1\frac{\partial f}{\partial x}\sin\phi + r\_1\frac{\partial f}{\partial y}\cos\phi$$

Assuming that the gradient $\nabla f = (\frac{\partial f}{\partial x},\frac{\partial f}{\partial y},\frac{\partial f}{\partial z},\frac{\partial f}{\partial w})$ is known, it is straightforward to compute the 2d gradient.

Note that another possibility is to compute the gradient numerically (in case you don't have the partial derivatives of $f$):
$$ \frac{\partial f}{\partial \theta} \approx \frac{f(r\_0\cos(\theta+h\_{\theta}), r\_0\sin(\theta+h\_{\theta}), r\_1\cos\phi,r\_1\sin\phi) - f(r\_0\cos\theta, r\_0\sin\theta, r\_1\cos\phi,r\_1\sin\phi)}{h\_{\theta}}$$
$$ \frac{\partial f}{\partial \phi} \approx \frac{f(r\_0\cos\theta, r\_0\sin\theta, r\_1\cos(\phi+h\_{\phi}),r\_1\sin(\phi+h\_{\phi})) - f(r\_0\cos\theta, r\_0\sin\theta, r\_1\cos\phi,r\_1\sin\phi)}{h\_{\phi}}$$
For $h\_{\theta}, h\_{\phi}$ chosen adequately (note that the choice of these parameters is a nontrivial problem).


---

