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

How do I limit the number of coefficients used? #774

Open
YichengDWu opened this issue May 15, 2022 · 4 comments
Open

How do I limit the number of coefficients used? #774

YichengDWu opened this issue May 15, 2022 · 4 comments

Comments

@YichengDWu
Copy link

Thank you for your incredible library!

I'm using it to solve the wave equation but it runs a bit slow. So I'd like to restrict the number of coefficients used, how should I do that?

Here is my code:

function we_dirichlet(c, t, x, s)
    """
    s: center of the Gaussian pulse
    """
    dx = Domain(x[1] .. x[2])
    dt = Domain(t[1] .. t[2])
    d = dx × dt
    Dx = Derivative(d, [1, 0])
    Dt = Derivative(d, [0, 1])
    # need to specify both ic and its derivative
    B = [I  ldirichlet(dt), I  lneumann(dt), ldirichlet(dx)  I, rdirichlet(dx)  I]

    u0 = Fun(x -> exp(-(x - s)^2), dx)
    uₜ0 = Fun(x -> -2 * c * (x - s), dx) * u0
    QR = qr([B; Dt^2 - c * c * Dx^2])
    u = \(QR, [u0; uₜ0; 0; 0; 0]; tolerance=1E-4)
    return u
end

u =  we_dirichlet(2.0, (0, 20), (-8, 8), 0.1)
# ncoefficients(u) = 8771
@jishnub
Copy link
Member

jishnub commented Jul 6, 2022

Have you tried chop?

@GrigorySarnitsky
Copy link

Should there be a method to explicitly limit the number of coefficients? E.g. so far I do just e.g.

f_smoother = Fun(f.space, f.coefficients[1:256])

That is it would be nice to have chop based on the number of coefficients rather than their values.

@GrigorySarnitsky
Copy link

Oh, I have found the solution, just use Fun(f, domain, n) to limit the number of coefficients to n while constructing approximation for f.

xs = 0:0.01:2π
plot(xs, [sin.(xs), Fun(x -> sin.(x), 0..2π, 4).(xs)])

@jishnub
Copy link
Member

jishnub commented Jan 9, 2025

Perhaps we may document this, as the question comes up frequently. Setting a tolerance might be one way to go.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants