Passing numpy arrays between python and c++ using cython

Passing numpy arrays between Python and c++ using Cython is a handy way of taking advantage of the ease and flexibility of python with the speed of c++.

This code illustrates how simple it is to pass n-dimensional (or in this case 2D) numpy arrays from python to c++ vectors and back, allowing computationally expensive code to be easily written in c++ and linked to high level python code.

The example code is hosted as a gist here and is an adaption from the standard Cython example  but also includes the 2D numpy example

Rectangle.cpp and Rectangle.h is a an example c++ class to be wrapped

This is wrapped using cython with an interface file rect.pyx and compiled using in the standard cython way using the command:

python build_ext --inplace

Then run the demo:


Shows the standard examples as well as how to pass 2D arrays between python and cython