# Computing derivatives with Fourier transforms


```python
import matplotlib.pyplot as plt
import numpy as np
import os
import sys
sys.path.append(os.path.join(os.getcwd(), '../build/'))
import pydeft as deft

plt.rc('font', family='serif')
plt.rc('xtick', labelsize='x-small')
plt.rc('ytick', labelsize='x-small')
```

### Computing the gradient

The gradient of a function and its Fourier transform are related by

\begin{equation}
\nabla w(\mathbf{r}) \xrightarrow[\text{Fourier transform}]{} i \mathbf{k} \tilde{w}(\mathbf{k}) .
\end{equation}

The following code uses DEFT to compute the gradient of $w(\mathbf{r}) = \cos(4\pi x)$, defined on a simple cubic lattice with unit spacing. The result should be $-4\pi\sin(4\pi x)$ along the $x$-direction and zero along the $y$- and $z$- directions.


```python
# create box and data array
box_vectors = np.eye(3)
grd_pts = 25
box = deft.Box(box_vectors)
data = deft.Double3D([grd_pts, grd_pts, grd_pts])

# get f(r) = cos(4*pi*x) on grid
for i in range(grd_pts):
    data[i,:,:] = np.cos(4.0*np.pi*float(i)/grd_pts)

# compute gradients
grad_x = deft.gradient_x(data, box)
grad_y = deft.gradient_y(data, box)
grad_z = deft.gradient_z(data, box)

# plot
x = np.linspace(0,1,500)
fig = plt.figure(figsize=[9,3.5])
plt.plot(x, -4.0*np.pi*np.sin(4.0*np.pi*x), color='0.5', ls = 'solid', linewidth = 2)
plt.plot(np.arange(grd_pts)/grd_pts, grad_x[:,0,0], '.k', markersize = 8)
plt.plot(np.arange(grd_pts)/grd_pts, grad_y[:,0,0], '+k', markersize = 8)
plt.plot(np.arange(grd_pts)/grd_pts, grad_z[:,0,0], 'xk', markersize = 8)
plt.xlim([-0.01, 1.01])
plt.xlabel('$x$-axis [one lattice cell]')
plt.legend(['$-4\pi\sin(4\pi x)$', '$\\nabla_x w(\mathbf{r})$', '$\\nabla_y w(\mathbf{r})$', '$\\nabla_z w(\mathbf{r})$'], loc=(1.05,0.25))
plt.tight_layout()
plt.show()
```

### Computing the Laplacian

The Laplacian of a function and its Fourier transform are related by

\begin{equation}
\nabla^2 w(\mathbf{r})
\xrightarrow[\text{Fourier transform}]{} 
- k^2 \tilde{w}(\mathbf{k}) .
\end{equation}

The following code uses DEFT to compute the Laplacian of $w(\mathbf{r}) = \cos(4\pi x)$, defined on a simple cubic lattice with unit spacing. The result should be $-16\pi^2\cos(4\pi x)$.


```python
# create box and data array
box_vectors = np.eye(3)
grd_pts = 25
box = deft.Box(box_vectors)
data = deft.Double3D([grd_pts, grd_pts, grd_pts])

# get f(r) = cos(4*pi*x) on grid
for i in range(grd_pts):
    data[i,:,:] = np.cos(4.0*np.pi*float(i)/grd_pts)

# compute laplacian
lapl = deft.laplacian(data, box)

# plot
x = np.linspace(0,1,500)
plt.rc('font', size=14)
fig = plt.figure(figsize=[9,3.5])
plt.plot(x, -16.0*np.pi**2*np.cos(4.0*np.pi*x), color='0.5', ls = 'solid', linewidth = 2)
plt.plot(np.arange(grd_pts)/grd_pts, lapl[:,0,0], '.k', markersize = 8)
plt.xlim([-0.01, 1.01])
plt.xlabel('$x$-axis [one lattice cell]')
plt.legend(['$-16\pi^2\cos(4\pi x)$', '$\\nabla^2 w(\mathbf{r})$'], loc=(1.03,0.4))
plt.tight_layout()
plt.show()
```

90a576a2b09ef292c1f01e0ca1bc138e5ad54911
ipynb
Jupyter Notebook
example/derivatives.ipynb
cw-tan/deft
abb4d23fa0bb53031c13daef9942bceba4afd655
MIT
example/derivatives.ipynb
cw-tan/deft
abb4d23fa0bb53031c13daef9942bceba4afd655
MIT
example/derivatives.ipynb
cw-tan/deft
abb4d23fa0bb53031c13daef9942bceba4afd655
MIT
Qwen/Qwen-72B
1. YES
2. YES