Troubles with arrays, proposal for Solution
Dear Numerical Python Users, It is now clear to me, after the comments of Travis Oliphant, Paul Dubois and Phil Austin, that in EVERY C-file that uses NumPy extensions, the "import_array" statement must be present. However, after some try-outs with parts my code, it seems not a good idea to allow a diffusion of the "import_array" statement and "PyArrayObject" throughout the "C" code. The code becomes unclear by the mixing of Python concepts and C concepts. Therefore it is, in my opinion, a good rule to limit the use of NumPy to the interface between python and "C" code; i.e. the function where the python parameters are read and the PyArrayObject is created. Do more experienced NumPy users agree??? The code below illustrates the limited use of NumPy parts. Wim Vanroose ///////////////////////// //file: arraytest.h ////////////////////// #include "Python.h" #include "arrayobject.h" double *myTest(void); ////////////////////// // file: arraytest.c //////////////////// #include "arraytest.h" double * myTest(void ){ double *result; ... return result; } //////////////////// //file: test.c //////////////// #include "arraytest.h" static PyObject *function(PyObject *self, PyObject *args){ ... int dimensions[2]; dimensions[0] = N; dimensions[1] = N; PyArrayObject *result ; result = (PyArrayObject *)PyArray_FromDims(2,dimensions,PyArray_DOUBLE); double *data; data = myTest(); memcpy(result->data,data,N*N*sizeof(double)); return PyArray_Return(result); } static PyMethodDef testMethods[] = { {"test",function,1}, {NULL,NULL} }; extern "C" { void inittest(){ PyObject *m,*d; m = Py_InitModule("test", testMethods); import_array(); d = PyModule_GetDict(m); }
participants (1)
-
Vanroose Wim