Skip to content

Commit aedb6ad

Browse files
Merge pull request #229 from ErikQQY/master
Fix forward and inverse application in FFTW tutorial
2 parents a389e25 + b03b0be commit aedb6ad

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

docs/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
[deps]
22
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
3+
FFTW = "7a1cc6ca-52ef-59f5-83cd-3a7055c09341"
34
SciMLOperators = "c0aeaf25-5076-4817-a8d5-81caf7dfa961"
45

56
[compat]
67
Documenter = "0.27, 1"
8+
FFTW = "1.7"
79
SciMLOperators = "0.2, 0.3"

docs/src/tutorials/fftw.md

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ derivative of a function.
77

88
## Copy-Paste Code
99

10-
```
10+
```@example fft
1111
using SciMLOperators
1212
using LinearAlgebra, FFTW
1313
@@ -23,6 +23,12 @@ k = rfftfreq(n, 2π*n/L) |> Array
2323
m = length(k)
2424
P = plan_rfft(x)
2525
26+
fwd(u, p, t) = P * u
27+
bwd(u, p, t) = P \ u
28+
29+
fwd(du, u, p, t) = mul!(du, P, u)
30+
bwd(du, u, p, t) = ldiv!(du, P, u)
31+
2632
F = FunctionOperator(fwd, x, im*k;
2733
T=ComplexF64,
2834
@@ -50,7 +56,7 @@ in the West), a common Fourier transform library. Next, we define an equispaced
5056
trivial example, we already know the derivative, `du`, and write it down to later test our
5157
FFT wrapper.
5258

53-
```
59+
```@example fft_explanation
5460
using SciMLOperators
5561
using LinearAlgebra, FFTW
5662
@@ -70,18 +76,23 @@ object that can be applied to inputs that are like `x` as follows: `xhat = trans
7076
and `LinearAlgebra.mul!(xhat, transform, x)`. We also get `k`, the frequency modes sampled by
7177
our finite grid, via the function `rfftfreq`.
7278

73-
```
79+
```@example fft_explanation
7480
k = rfftfreq(n, 2π*n/L) |> Array
7581
m = length(k)
76-
tr = plan_rfft(x)
82+
P = plan_rfft(x)
7783
```
7884

7985
Now we are ready to define our wrapper for the FFT object. To `FunctionOperator`, we
8086
pass the in-place forward application of the transform,
8187
`(du,u,p,t) -> mul!(du, transform, u)`, its inverse application,
8288
`(du,u,p,t) -> ldiv!(du, transform, u)`, as well as input and output prototype vectors.
8389

84-
```
90+
```@example fft_explanation
91+
fwd(u, p, t) = P * u
92+
bwd(u, p, t) = P \ u
93+
94+
fwd(du, u, p, t) = mul!(du, P, u)
95+
bwd(du, u, p, t) = ldiv!(du, P, u)
8596
F = FunctionOperator(fwd, x, im*k;
8697
T=ComplexF64,
8798
@@ -98,15 +109,12 @@ SciMLOperators. Below, we form the derivative operator, and cache it via the fun
98109
`cache_operator` that requires an input prototype. We can test our derivative operator
99110
both in-place, and out-of-place by comparing its output to the analytical derivative.
100111

101-
```
112+
```@example fft_explanation
102113
ik = im * DiagonalOperator(k)
103114
Dx = F \ ik * F
104115
116+
Dx = cache_operator(Dx, x)
117+
105118
@show ≈(Dx * u, du; atol=1e-8)
106119
@show ≈(mul!(copy(u), Dx, u), du; atol=1e-8)
107120
```
108-
109-
```
110-
≈(Dx * u, du; atol = 1.0e-8) = true
111-
≈(mul!(copy(u), Dx, u), du; atol = 1.0e-8) = true
112-
```

0 commit comments

Comments
 (0)