One function, any precision
FFTL's functions take Float32, Float64 and every other floating-point type, and arrays on the GPU, with the same calls. A function written once against them works for all of them. Here it is a spectral derivative, a method whose accuracy is limited only by the arithmetic, so the precision of the type is what one sees.
using FFTL, CairoMakieThe spectral derivative
For a smooth periodic function sampled at
function spectral_derivative(u::AbstractVector{T}) where {T<:Real}
n = length(u)
U = fft(u)
k = [j <= n ÷ 2 ? j : j - n for j in 0:n-1]
iseven(n) && (U[n÷2+1] = 0)
real(ifft(Complex{T}.(im .* k) .* U))
endspectral_derivative (generic function with 1 method)The test function
function derivative_error(T, n)
x = [2T(π) * j / n for j in 0:n-1]
u = exp.(sin.(x))
maximum(abs, spectral_derivative(u) .- cos.(x) .* u)
end
derivative_error(Float64, 64)8.659739592076221e-15Three precisions
The error falls exponentially with BigFloat is transformed in its own precision (here 256 bits), not converted to Float64, so it keeps falling far below what Float64 can show. Every
setprecision(BigFloat, 256)
ns = 4:2:120
errs = Dict(T => [max(Float64(derivative_error(T, n)), 1e-80) for n in ns]
for T in (Float32, Float64, BigFloat));
fig = Figure(size = (640, 380))
ax = Axis(fig[1, 1]; xlabel = "points n", ylabel = "max error", yscale = log10,
limits = (nothing, (1e-80, 1)))
for T in (Float32, Float64, BigFloat)
lines!(ax, ns, errs[T]; label = string(T))
T === BigFloat || hlines!(ax, eps(T); linestyle = :dash, color = :gray)
end
axislegend(ax; position = :lb)
fig
Float32 and Float64 level off near their
The same function elsewhere
Other floating-point types work the same way, for example Double64 from DoubleFloats.jl or Float128 from Quadmath.jl. Transforms of these types take a generic path without SIMD and threads, which is slower, so they are the tool for checking results, not for production.
On a GPU the arrays come from the GPU package. With KernelAbstractions.jl loaded, the same FFTL functions run there (see GPU); the derivative would be written with broadcasts instead of the comprehension for k:
using FFTL, Metal, KernelAbstractions
u = MtlArray(ComplexF32.(exp.(sin.(2π .* (0:1023) ./ 1024))))
U = FFTL.fft(u)This page was generated using Literate.jl.