问题
I need a c++ library to compute the polygamma function for complex arguments. After some googling that brought me to this
https://scicomp.stackexchange.com/questions/23194/i-am-searching-for-c-code-of-the-complex-polygamma-function/23195/
I decided to try to call the julia library from c++.
In order to embedd julia in c++ I followed the example at
julia-lang embedding into c.
where julia is used to calculate the sqrt(2)
.
This works fine... how can I generalize the example to work for my case?
How can I load an external package as SpecialFunctions in c++?
How can I pass complex arguments?
It should work as polygamma( int n , complex<double> z )
.
Here is what I tried (considering only real arguments for the moment)
#include <julia.h>
JULIA_DEFINE_FAST_TLS()
jl_module_t* jl_specialfunctions_module = (jl_module_t*) jl_get_binding(jl_main_module, jl_symbol("SpecialFunctions"));
jl_function_t* func2 = jl_get_function(jl_specialfunctions_module,"polygamma");
jl_value_t *argument1 = jl_box_int64(1);
jl_value_t *argument2 = jl_box_float64(2.0);
jl_value_t *arguments[2] = { argument1 , argument2 };
jl_value_t *ret = jl_call(func2, arguments, 2);
if (jl_typeis(ret, jl_float64_type)) {
double ret_unboxed = jl_unbox_float64(ret);
cout << "julia = " << ret_unboxed << endl;
}
else {
printf("ERROR: unexpected return type from sqrt(::Float64)\n");
}
jl_atexit_hook(0);
The code compiles but gives Segmentation Fault error
in expression starting at no file:0
ptrhash_get at /buildworker/worker/package_linux64/build/src/support/ptrhash.c:26
unknown function (ip: 0xffffffffffffffff)
Allocations: 390564 (Pool: 390440; Big: 124); GC: 0
[4] 10669 segmentation fault (core dumped) ./ggv.run
Do you have any idea or reference I can look up to solve it? Thanks in advance
Federico
EDIT: After molbdnilo suggestion I checked the jl_specialfunction_module ptr and it a NULL ptr. How should I properly load the Specialfunctions module?
来源:https://stackoverflow.com/questions/54647101/how-to-call-a-julia-method-defined-in-an-imported-package-from-c