|
#include <stdio.h> |
|
#include <Python.h> |
|
#include "numpy/ndarraytypes.h" |
|
#include "numpy/ufuncobject.h" |
|
#include "numpy/npy_3kcompat.h" |
|
|
|
// Module method definitions |
|
static PyObject* hello_world_c(PyObject *self, PyObject *args) { |
|
printf("Hello, world!\n"); |
|
Py_RETURN_NONE; |
|
} |
|
|
|
|
|
static PyObject* hello_numpy_c(PyObject *dummy, PyObject *args) |
|
{ |
|
PyObject *arg1=NULL; |
|
PyObject *arr1=NULL; |
|
int nd; |
|
|
|
if (!PyArg_ParseTuple(args, "O", &arg1)) |
|
return NULL; |
|
|
|
arr1 = PyArray_FROM_OTF(arg1, NPY_DOUBLE, NPY_IN_ARRAY); |
|
/* |
|
* my code starts here |
|
*/ |
|
nd=PyArray_NDIM(arr1); |
|
|
|
npy_intp *sp=PyArray_SHAPE(arr1); |
|
|
|
printf("array dimentsion: %ld\n",*sp); |
|
|
|
printf("Print array elements:\n"); |
|
|
|
for (int i=0; i<*sp; i++) |
|
{ |
|
printf("%lf ",*((npy_double*)PyArray_GETPTR1(arr1,i))); |
|
} |
|
|
|
printf("\n"); |
|
|
|
if (arr1 == NULL) |
|
return NULL; |
|
|
|
nd = PyArray_NDIM(arr1); //number of dimensions |
|
|
|
Py_DECREF(arr1); |
|
|
|
return PyInt_FromLong(nd); |
|
} |
|
|
|
|
|
static PyMethodDef hello_methods[] = { |
|
{ |
|
"hello_python", hello_world_c, METH_VARARGS, |
|
"Print 'hello xxx'" |
|
}, |
|
{ |
|
"hello_numpy", hello_numpy_c, METH_VARARGS, |
|
"numpy function tester", |
|
}, |
|
{NULL, NULL, 0, NULL} |
|
}; |
|
|
|
|
|
static struct PyModuleDef hello_definition = { |
|
PyModuleDef_HEAD_INIT, |
|
"hello", |
|
"A Python module that prints 'hello world' from C code.", |
|
-1, |
|
hello_methods |
|
}; |
|
|
|
|
|
PyMODINIT_FUNC PyInit_hello(void) { |
|
Py_Initialize(); |
|
import_array(); |
|
return PyModule_Create(&hello_definition); |
|
} |