JuliaApproximation / FrameFun.jl

Exploring practical possibilities of approximating functions with frames rather than with a basis
Other
23 stars 7 forks source link

replicating front-page example #31

Open jagunther opened 1 year ago

jagunther commented 1 year ago

Hi!

First of all: Thanks a lot for making your code available :)

I want to implement 1D Fourier-extension of some functions and came across this library last week. I did a fresh install of Julia and the FrameFun, BasisFunctions and DomainSets packages. The first thing I did was to run the example code from the front-page (I run it as a script from the command-line):

gr();

B = Fourier(61) → -1..1
D = -0.5..0.5
f = x->x
F = Fun(f,B,D)

plot(F,layout = 2)

p = plot!(F,f,subplot=2)
display(p)
readline()

However, contrary to the plot on the front-page, the resulting function is only plotted in the inner interval, see figure attached.

frontpage_code_result

I did the following to get the approximation on the whole interval:

gr();

B = Fourier(61) → -1..1
D = -0.5..0.5
f = x->x
F_ = Fun(f, B, D)
F = expansion(B, F_.coefficients)

plot(F,layout = 2)
p = plot!(F,f,subplot=2)
display(p)
readline()

with the result

frontpage_code_adapted_result

This looks better, but it is not the same result as I see on the front-page.

What do I need to change?

daanhb commented 1 year ago

Hi @jagunther, sorry for the slow reply here!

It seems we haven't updated the front page in a while. Indeed, somewhere along the way we changed the way plotting an "extension frame" works. It only shows the part of the approximation that actually approximates the given function (here the interval [-0.5,0.5]) and nothing outside.

The reason your plot on the extension [-1,1] differs from the one on the front page is because the extension is not unique. The "Fourier extension problem" is in fact solved using randomized linear algebra, hence you might even get a different result every time. It should always approximate the given function f on [-0.5,0.5] to high accuracy however.

About plotting the extension: at one point we had a plot_extension=true optional argument, but that seems to have disappeared. In your plotting example you manually create a Fourier basis, rescaled to the interval [-1,1]. This is implicit in the dictionary (more general than a basis) of the expansion F:

julia> dictionary(F)
(-0.5..0.5) → (Fourier(61) → (-1.0..1.0))

julia> superdict(dictionary(F))
Fourier(61) → (-1.0..1.0)

It would be slightly neater if we defined, say,

super(F::Expansion) = Expansion(superdict(dictionary(F)), coefficients(F))

then you could simply write plot(super(F)).