How to use Julia special functions inside c++

I answered this question in the other stackoverflow thread.

Basically, the easiest thing to do is to use the Julia @cfunction construct to let Julia compile the code into a C++ function pointer, which you can then call normally without worrying about unboxing etcetera.

(For passing complex numbers, @cfunction can exploit the fact that C++ std::complex<double> and Julia Complex{Float64} have identical memory representations.)


Thanks to @Matt B, I looked into Julia codes and see how these modules are there. So the following could be one possible solution.

#include <julia.h>
#include<iostream>
JULIA_DEFINE_FAST_TLS() 

int main(){
  jl_init();

  jl_eval_string("using SpecialFunctions");
  jl_module_t* SpecialFunctions =(jl_module_t*)jl_eval_string("SpecialFunctions");

  jl_function_t *func2 = jl_get_function(SpecialFunctions, "polygamma");

  // arguments to pass to 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 *ret2 = jl_call(func2, arguments, 2);

  if (jl_typeis(ret2, jl_float64_type)){
    double ret_unboxed = jl_unbox_float64(ret2);
    std::cout << "\n julia result = " << ret_unboxed << std::endl;
  }
  else{
    std::cout<<"hello error!!"<<std::endl;
  }

  jl_atexit_hook(0);
  return 0;
 }

Now I need to see how can I pass complex numbers to the argument of polygamma which is why all these fuss about :) !

Tags:

C++

Julia