diff --git a/src/gen/gsl_types.jl b/src/gen/gsl_types.jl index 90622df..cd60c59 100644 --- a/src/gen/gsl_types.jl +++ b/src/gen/gsl_types.jl @@ -876,7 +876,7 @@ defined in `gsl_math.h`. """ mutable struct gsl_function function_::Ptr{Cvoid} - params::Ptr{Cvoid} + params end @doc md""" diff --git a/src/manual_wrappers.jl b/src/manual_wrappers.jl index c73e789..fa9311b 100644 --- a/src/manual_wrappers.jl +++ b/src/manual_wrappers.jl @@ -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 diff --git a/test/numdiff.jl b/test/numdiff.jl index eb1c445..97292ae 100644 --- a/test/numdiff.jl +++ b/test/numdiff.jl @@ -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 diff --git a/test/quadrature.jl b/test/quadrature.jl index b428412..218ad92 100644 --- a/test/quadrature.jl +++ b/test/quadrature.jl @@ -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[]