|
| 1 | +/* see |
| 2 | + http://wiki.scipy.org/Cookbook/C_Extensions/NumPy_arrays and |
| 3 | + http://scipy-lectures.github.io/advanced/interfacing_with_c/interfacing_with_c.html |
| 4 | +*/ |
| 5 | + |
| 6 | +#include <Python.h> |
| 7 | +#include <numpy/arrayobject.h> |
| 8 | +#include <math.h> |
| 9 | + |
| 10 | +/* a static function in C limits its scope to this file -- the linker |
| 11 | + won't complain about clashes */ |
| 12 | +static PyObject* ex_function(PyObject* self, PyObject* args) |
| 13 | +{ |
| 14 | + |
| 15 | + PyArrayObject *in_array, out_array; |
| 16 | + double **iA; **oA; |
| 17 | + int i, j, m, n, dims[2]; |
| 18 | + |
| 19 | + /* parse the inputs -- we need to know what the arguments of our |
| 20 | + call were. We'll assume: |
| 21 | + |
| 22 | + ex_function(array) |
| 23 | + |
| 24 | + and that we return a new array of the same dimensions |
| 25 | + |
| 26 | + In the parsing, you can do O or O! here (from the docs): |
| 27 | + |
| 28 | + O (object) [PyObject *] |
| 29 | + |
| 30 | + Store a Python object (without any conversion) in a C object |
| 31 | + pointer. The C program thus receives the actual object that was |
| 32 | + passed. The object’s reference count is not increased. The |
| 33 | + pointer stored is not NULL. |
| 34 | + |
| 35 | + O! (object) [typeobject, PyObject *] |
| 36 | + |
| 37 | + Store a Python object in a C object pointer. This is similar to |
| 38 | + O, but takes two C arguments: the first is the address of a |
| 39 | + Python type object, the second is the address of the C variable |
| 40 | + (of type PyObject*) into which the object pointer is stored. If |
| 41 | + the Python object does not have the required type, TypeError is |
| 42 | + raised. |
| 43 | + |
| 44 | + O! seems safer and preferred |
| 45 | + |
| 46 | + */ |
| 47 | + |
| 48 | + if (!PyArg_ParseTuple(args, "O!", &PyArray_Type, &iarray)) return NULL; |
| 49 | + if (NULL == iarray) return NULL; |
| 50 | + |
| 51 | + /* check to make sure we are a double type */ |
| 52 | + if (not_doublematrix(iarray)) return NULL; |
| 53 | + |
| 54 | + /* get the dimensions */ |
| 55 | + n = dims[0] = iarray->dimensions[0]; |
| 56 | + m = dims[1] = iarray->dimensions[1]; |
| 57 | + |
| 58 | + /* the new C interface can create iteration "object" using NpyIter, but we |
| 59 | + are not going to do that here, we want to explicitly see the different |
| 60 | + dimensions |
| 61 | + */ |
| 62 | + |
| 63 | + /* make a NumPy double matrix with the same dimensions -- this will |
| 64 | + be contiguous, and will be our output (note, there is also a |
| 65 | + PyArray_NewLikeArray function) */ |
| 66 | + oarray = (PyArrayObject *) PyArray_FromDims(2, dims, NPY_DOUBLE); |
| 67 | + |
| 68 | + /* change contigous arrays into C ** arrays -- we need to have a |
| 69 | + vector of pointers that point to the correct location in the |
| 70 | + contiguous block of memory that stores the multi-dimensional |
| 71 | + array data */ |
| 72 | + iA = (double **) malloc( (size_t) (n*sizeof(double))); |
| 73 | + for (i = 0; i < n; i++) { |
| 74 | + iA[i] = (double *) iarray->data + i*m; |
| 75 | + } |
| 76 | + |
| 77 | + oA = (double **) malloc( (size_t) (n*sizeof(double))); |
| 78 | + for (i = 0; i < n; i++) { |
| 79 | + oA[i] = (double *) oarray->data + i*m; |
| 80 | + } |
| 81 | + |
| 82 | + /* now we can do our manipulation */ |
| 83 | + for (i = 0; i < n; i ++) { |
| 84 | + for (j = 0; j < m; j++) { |
| 85 | + oA[i][j] = iA[i][j]*iA[i][j]; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /* free up the memory we allocated for the array indexing */ |
| 90 | + free (iA); |
| 91 | + free (oA); |
| 92 | + |
| 93 | + /* return our python array */ |
| 94 | + return PyArray_Return(oarray); |
| 95 | + |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +/* this is the table for function names that Python will see */ |
| 100 | +static PyMethodDef numpy_in_cMethods[] = { |
| 101 | + {"example", ex_function, METH_VARARGS}, |
| 102 | + {NULL, NULL} |
| 103 | +}; |
| 104 | + |
| 105 | +/* this tells python what to do when it first imports this module -- |
| 106 | + the name follows directly from the table name above */ |
| 107 | +void initnumpy_in_c() { |
| 108 | + (void) Py_InitModule("numpy_in_c", numpy_in_cMethods); |
| 109 | + import_array(); // this deals with the NumPy stuff |
| 110 | +} |
0 commit comments