I have some C code that has the following declaration:
int myfunc(int m, int n, const double **a, double **b, double *c);
So a
is a constant 2D array, b
is a 2D array, and c
is a 1D array, all dynamically allocated. b
and c
do not need to be anything specifically before they are passed to myfunc
, and should be understood as output information. For the purposes of this question, I'm not allowed to change the declaration of myfunc
.
Question 1: How do I convert a given numpy array a_np
into an array a
with the format required by this C function, so that I can call this C function in Cython with a
?
Question 2: Are the declarations for b
and c
below correct, or do they need to be in some other format for the C function to understand them as a 2D and 1D array (respectively)?
My attempt:
myfile.pxd
cdef extern from "myfile.h":
int myfunc(int p, int q, const double **a, double **b, double *c)
mytest.pyx
cimport cython
cimport myfile
import numpy as np
cimport numpy as np
p = 3
q = 4
cdef:
double** a = np.random.random([p,q])
double** b
double* c
myfile.myfunc(p, q, a, b, c)
Then in iPython I run
import pyximport; pyximport.install()
import mytest
The line with the definition of a
gives me the error message Cannot convert Python object to 'double **'
. I don't get any error messages regarding b
or c
, but since I'm unable to run the C function at this time, I'm not sure the declarations of b
and c
are written correctly (that is, in a way that will enable the C function to output a 2D and a 1D array, respectively).
Other attempts: I've also tried following the solution here, but this doesn't work with the double-asterisk type of arrays I have in the myfunc
declaration. The solution here does not apply to my task because I can't change the declaration of myfunc
.
See Question&Answers more detail:
os