Always in the study of my wrapper for ACME.jl, I need to convert 1d and 2d array of float64 from julia (libjulia/c-api) to armadillo C++ (arma::vec / arma::mat).
In julia console, when I dump the array I need I get :
julia> model.c
1×2 Array{Float64,2}:
-9.4e-8 0.0
This is my code to convert the array to armadillo matrix (assuming that type is always double precision aka Float64), the code is not optimized, more theorical :
static inline arma::mat make_arma_mat (jl_value_t* array_ptr)
{
arma::mat container;
if (jl_is_array(array_ptr))
{
jl_array_t* array_jl = (jl_array_t*)array_ptr;
// Get array pointer
double* raw = (double*)jl_array_ptr(array_jl);
// Get number of dimensions
int ndims = jl_array_ndims(array_jl);
if (ndims != 2)
throw std::exception("make_arma_mat: container is not a 2d-array");
// Get the size of the i-th dim
size_t nrows = jl_array_dim(array_jl, 0);
size_t ncols = jl_array_dim(array_jl, 1);
container = arma::mat(nrows, ncols);
// Fill array with data
for (size_t c = 0; c<ncols; c++)
{
for (size_t r = 0; r<nrows; r++)
{
double value = raw[r+nrows*c];
container(r,c) = value;
}
}
}
return container;
}
The code work fine for me, I get a correct matrix except with the values precision.
This is the resulted armadillo matrix dump :
c
0 0
I don’t understand why ? A simple test show that the libjulia float64 can be box and unbox with good resolution :
jl_init (NULL);
double cpp_64 = 1.8e-15;
jl_value_t* arg = jl_box_float64(cpp_64);
double julia_64 = jl_unbox_float64(arg);
std::cout << julia_64 << std::endl;
Output :
1.8e-15
The array ‘eltype
’ value is 8, I cast the raw array ptr to ‘double
’, maybe this is not the right way to get the full precision?
Thank you very much for your answer.