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

Variable not defined when using integration_qawc inside a function with local variables. #138

Closed
Sollovin opened this issue Apr 20, 2024 · 3 comments

Comments

@Sollovin
Copy link

Sollovin commented Apr 20, 2024

Hi, I want to calculate the cauchy principle value integration of a three arguments function inside a function with two arguments decided at runtime with local variables. But it seemed that integration_qawc will not find the local variables. Minimal code to repeate the situation is given as follows,

using GSL

fdist::Float64, μ::Float64, kbt::Float64) = 1.0 / (exp((ε - μ)/kbt) + 1.0)

function genΛ2h(i, j, α, ϵ, t, μ, kbt)
    ws_size = 100
    ws = integration_workspace_alloc(ws_size)

    if α == 1
        f = @gsl_function(x -> (1 - fdist(x, μ[1], kbt)))
    else
        f = @gsl_function(x -> (1 - fdist(x, μ[2], kbt)))
    end

    result = Cdouble[0]
    abserr = Cdouble[0]

    integration_qawc(f, -1000, 1000, ϵ[j], 1e-9, 1e-9, 100, ws, result, abserr)

    return - t[i,α] * t[j,α]' * result[1] / (2*π)
end

genΛ2h(1, 1, 1, [1., 1.], [[1e-2 1e-2];[1e-2 1e-2]], [1.5, -1.5], 0.6)

with ERROR: UndefVarError: μ not defined.

Any suggestions for making the code to work?

@yuyichao
Copy link
Collaborator

This is the limitation for the @gsl_function macro and cannot be fixed. On the master version you can just omit the @gsl_function and it would work.

@Sollovin
Copy link
Author

Ok, I got. Thanks a lot for your reply.

But I do find a way to avoid the local variables as follows,

eval(Meta.parse("f = x -> (1 - fdist(x, $(μ[α]), $kbt))"))

#...

integration_qawc(@gsl_function(f), -1000, 1000, ϵ[j], 1e-9, 1e-9, 100, ws, result, abserr)

@yuyichao
Copy link
Collaborator

No please don't do that.

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

2 participants