Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix forward and inverse application in FFTW tutorial #229

Merged
merged 6 commits into from
Dec 12, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions docs/src/tutorials/fftw.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ derivative of a function.

## Copy-Paste Code

```
```@example fft
using SciMLOperators
using LinearAlgebra, FFTW

Expand All @@ -23,6 +23,12 @@ k = rfftfreq(n, 2π*n/L) |> Array
m = length(k)
P = plan_rfft(x)

fwd(u, p, t) = P * u
bwd(u, p, t) = P \ u

fwd(du, u, p, t) = mul!(du, P, u)
bwd(du, u, p, t) = ldiv!(du, P, u)

F = FunctionOperator(fwd, x, im*k;
T=ComplexF64,

Expand Down Expand Up @@ -50,7 +56,7 @@ in the West), a common Fourier transform library. Next, we define an equispaced
trivial example, we already know the derivative, `du`, and write it down to later test our
FFT wrapper.

```
```@example fft_explanation
using SciMLOperators
using LinearAlgebra, FFTW

Expand All @@ -70,7 +76,7 @@ object that can be applied to inputs that are like `x` as follows: `xhat = trans
and `LinearAlgebra.mul!(xhat, transform, x)`. We also get `k`, the frequency modes sampled by
our finite grid, via the function `rfftfreq`.

```
```@example fft_explanation
k = rfftfreq(n, 2π*n/L) |> Array
m = length(k)
tr = plan_rfft(x)
Expand All @@ -81,7 +87,12 @@ pass the in-place forward application of the transform,
`(du,u,p,t) -> mul!(du, transform, u)`, its inverse application,
`(du,u,p,t) -> ldiv!(du, transform, u)`, as well as input and output prototype vectors.

```
```@example fft_explanation
fwd(u, p, t) = P * u
bwd(u, p, t) = P \ u

fwd(du, u, p, t) = mul!(du, P, u)
bwd(du, u, p, t) = ldiv!(du, P, u)
F = FunctionOperator(fwd, x, im*k;
T=ComplexF64,

Expand All @@ -98,15 +109,15 @@ SciMLOperators. Below, we form the derivative operator, and cache it via the fun
`cache_operator` that requires an input prototype. We can test our derivative operator
both in-place, and out-of-place by comparing its output to the analytical derivative.

```
```@example fft_explanation
ik = im * DiagonalOperator(k)
Dx = F \ ik * F

@show ≈(Dx * u, du; atol=1e-8)
@show ≈(mul!(copy(u), Dx, u), du; atol=1e-8)
```

```
```@example fft_explanation
≈(Dx * u, du; atol = 1.0e-8) = true
≈(mul!(copy(u), Dx, u), du; atol = 1.0e-8) = true
```
Loading