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 setup.py in the standard cython way using the command:

python setup.py build_ext --inplace

Then run the demo:

python python_test.py

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


Related Posts

The move towards library agnostic machine learning

Introduction to Keras Autoencoders

Live tracking of python 3 usage based on a small set of scientific libraries

Thoughts on web tools for visualisation in medical image analysis research

Packaging a python app for Ubuntu using dh-virtualenv

Drag and drop files into a GUI using python and pyside

Interactive visualisations for research papers

Jabref gtk theme

Rendering volumes on the web with x3dom