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

auto wrapper #130

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/gen/gsl_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,7 @@ defined in `gsl_math.h`.
"""
mutable struct gsl_function
function_::Ptr{Cvoid}
params::Ptr{Cvoid}
params
end

@doc md"""
Expand Down
19 changes: 19 additions & 0 deletions src/manual_wrappers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,25 @@ end


## Root finding

# wrapper function
function gsl_function_helper(x::Cdouble, @nospecialize(fn::Any))::Cdouble
try
return Cdouble(fn(x))
catch
return NaN
end
end

function Base.cconvert(::Type{Ref{gsl_function}}, fn)
fptr = @cfunction(gsl_function_helper, Cdouble, (Cdouble, Any))
convert(Ref{gsl_function}, gsl_function(fptr, fn))
end

Base.cconvert(::Type{Ref{gsl_function}}, gslf::gsl_function) =
convert(Ref{gsl_function}, gslf)


# Macros for easier creation of gsl_function and gsl_function_fdf structs
export @gsl_function, @gsl_function_fdf

Expand Down
2 changes: 1 addition & 1 deletion test/numdiff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func(x) = x^3

for deriv in [deriv_central, deriv_forward, deriv_backward]
df,ddf = Cdouble[0], Cdouble[0]
deriv(@gsl_function(func), x, h, df, ddf)
deriv(func, x, h, df, ddf)
@test abs(df_dx - df[]) <= ddf[] <= d2f_dx2*h/2 + 2eps()/h
end
end
Expand Down
5 changes: 2 additions & 3 deletions test/quadrature.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ using GSL

fquad = x -> x^1.5
@testset "Quadrature" begin
fquad = x -> x^1.5
ws_size = 100
ws = integration_cquad_workspace_alloc(ws_size)

ff = @gsl_function(fquad)

a = 0
b = 1
result = Cdouble[0]
abserr = Cdouble[0]
nevals = Csize_t[0]
integration_cquad(ff, a, b, 1e-10, 1e-10, ws, result, abserr, nevals)
integration_cquad(fquad, a, b, 1e-10, 1e-10, ws, result, abserr, nevals)

@test abs(result[] - 1/2.5) < abserr[]

Expand Down
Loading