Numpy – C

C-NumPy tutorials

http://www.penzilla.net/tutorials/python/numpyc/

Numpy arrays and c

http://www.scipy.org/Cookbook/C_Extensions/NumPy_arrays

Numpy C API

Returning simple variables: http://docs.python.org/c-api/arg.html#Py_BuildValue

Array types in C: http://docs.scipy.org/doc/numpy/reference/c-api.types-and-structures.html#PyArrayObject

Create a numpy array: http://docs.scipy.org/doc/numpy/user/c-info.how-to-extend.html?highlight=pyarray_simplenew#PyArray_SimpleNew

Parsing arguments and building values: http://docs.python.org/c-api/arg.html

Parsing arrays given as paramaters: PyArray_ContiguousFromObject http://dsnra.jpl.nasa.gov/software/Python/numpydoc/numpy-14.html

A simple C extension for python: Returning values

#include <Python.h>
#include <arrayobject.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/* Method of the python object */
static PyObject *testfunc(PyObject *self, PyObject *args){
/* Return a value to python */
return Py_BuildValue(“i”, 1);
}

/* Table of methods implemented */
static PyMethodDef mainMethods[] = {
{“testfunc”, testfunc, METH_VARARGS},
{NULL, NULL}     /* Sentinel – marks the end of this structure */
};

/* Initialization of the function */
void initmain(void){
(void) Py_InitModule(“main”, mainMethods);
import_array();  // Must be present for NumPy.  Called first after above line.
}

See: http://docs.python.org/c-api/arg.html#Py_BuildValue

Returning Arrays

#include <Python.h>
#include <arrayobject.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/* Method of the python object */
static PyObject *testfunc(PyObject *self, PyObject *args){
int    N    = 10;
npy_intp dim[2] = {10,10};
// create the array
PyArrayObject *array = (PyArrayObject *) PyArray_SimpleNew(2, dim, PyArray_INT);
// fill the data
int    *buffer = (int*)array->data;
int    i;
for (i =0; i<10*10; i++){
buffer[i] = i;
}

/* Return an array to python */
return PyArray_Return(array);
}

/* Table of methods implemented */
static PyMethodDef mainMethods[] = {
{“testfunc”, testfunc, METH_VARARGS},
{NULL, NULL}     /* Sentinel – marks the end of this structure */
};

/* Initialization of the function */
void initmain(void){
(void) Py_InitModule(“main”, mainMethods);
import_array();  // Must be present for NumPy.  Called first after above line.
}

Parsing arguments

#include <Python.h>
#include <arrayobject.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* Method of the python object */
static PyObject *testfunc(PyObject *self, PyObject *args){
double    d1,d2;
int    status    = PyArg_ParseTuple(args, “dd”, &d1, &d2); /* Two doubles */
return Py_BuildValue(“d”,d1+d2);
}
/* Table of methods implemented */
static PyMethodDef mainMethods[] = {
{“testfunc”, testfunc, METH_VARARGS},
{NULL, NULL}     /* Sentinel – marks the end of this structure */
};
/* Initialization of the function */
void initmain(void){
(void) Py_InitModule(“main”, mainMethods);
import_array();  // Must be present for NumPy.  Called first after above line.
}

Array as a function argument

#include <Python.h>
#include <arrayobject.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* Method of the python object */
static PyObject *testfunc(PyObject *self, PyObject *args){
PyObject *arrayobject; //PyObject
int    status    = PyArg_ParseTuple(args, “O”, &arrayobject); /* PyObject */
//we want an  PyArray_INT which is 2×2.
PyArrayObject *array    = (PyArrayObject *) PyArray_ContiguousFromObject(arrayobject, PyArray_INT, 2, 2);
return PyArray_Return(array);
}
/* Table of methods implemented */
static PyMethodDef mainMethods[] = {
{“testfunc”, testfunc, METH_VARARGS},
{NULL, NULL}     /* Sentinel – marks the end of this structure */
};
/* Initialization of the function */
void initmain(void){
(void) Py_InitModule(“main”, mainMethods);
import_array();  // Must be present for NumPy.  Called first after above line.
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: