@@ -7,7 +7,7 @@ derivative of a function.
77
88## Copy-Paste Code
99
10- ```
10+ ``` @example fft
1111using SciMLOperators
1212using LinearAlgebra, FFTW
1313
@@ -23,6 +23,12 @@ k = rfftfreq(n, 2π*n/L) |> Array
2323m = length(k)
2424P = 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+
2632F = 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
5056trivial example, we already know the derivative, ` du ` , and write it down to later test our
5157FFT wrapper.
5258
53- ```
59+ ``` @example fft_explanation
5460using SciMLOperators
5561using LinearAlgebra, FFTW
5662
@@ -70,18 +76,23 @@ object that can be applied to inputs that are like `x` as follows: `xhat = trans
7076and ` LinearAlgebra.mul!(xhat, transform, x) ` . We also get ` k ` , the frequency modes sampled by
7177our finite grid, via the function ` rfftfreq ` .
7278
73- ```
79+ ``` @example fft_explanation
7480k = rfftfreq(n, 2π*n/L) |> Array
7581m = length(k)
76- tr = plan_rfft(x)
82+ P = plan_rfft(x)
7783```
7884
7985Now we are ready to define our wrapper for the FFT object. To ` FunctionOperator ` , we
8086pass 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)
8596F = 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
99110both in-place, and out-of-place by comparing its output to the analytical derivative.
100111
101- ```
112+ ``` @example fft_explanation
102113ik = im * DiagonalOperator(k)
103114Dx = 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