Take the 2-minute tour ×

my problem "should" be simple but I am still not able to solve it.

I am currently working on a project that requires some heavy computations (done in C++) and some post-simulations data analysis (done in Python).

However, now I am changing the main algorithm and I will need to "cycle" some computations back and forth from C++ and Python. That is, I will need to move back and forth from C++ and Python a matrix of doubles.

In C++ the matrix of data is a "gsl_matrix" object while in python the same matrix is implemented as a "numpy array".

At the moment, I am running my C++ code, saving the matrix to file, reading it from python, writing it back to file and then opening it back again in C++ for further computations.

Since this is VERY inefficient, I would like to ask if somebody can give me an example on how to do it in a "clean" way.

I have been reading (and trying for 10 days) SWIG, Cython, Boost.Python and Boost.Numpy but I'm still not able to crack it.

Does anyone have a worked example to share?



share|improve this question
How do you see things happening? Converting a gsl_matrix struct pointer to a PyArrayObject pointer using PyArray_SimpleNewFromData seems pretty straightforward, but how to handle everything around it depends very much on what/how you want things to happen. –  Jaime Sep 11 '13 at 19:37
well, the idea would be to have something live: " import numpy as np import MYCPP as mycpp DATA = np.loadtxt(...) # here DATA would be a TxN matrix out = mycpp.run(DATA, params) # here out would be a Nx1 vector " In C++, DATA needs to be a gsl_matrix in order to work and the "out" vector returned from the C++ code will be a gsl_vector –  Rene Sep 11 '13 at 19:53
add comment

1 Answer

I think you don't need to implement yourself the wrapper, because you may use pygsl. If you really want to implement your own version, here is the routine from pygsl that might be worth to you

#include <gsl/gsl_matrix_double.h>
#include <gsl/gsl_matrix_complex_double.h>

%include typemaps.i

// gsl_matrix typemaps
%typemap(in) gsl_matrix* %{
  PyArrayObject *_PyMatrix$argnum;
  gsl_matrix_view matrix$argnum;
    _PyMatrix$argnum = (PyArrayObject*)
      PyArray_ContiguousFromObject($input, PyArray_DOUBLE, 2, 2);
    if (_PyMatrix$argnum == NULL)
      return NULL;
      = gsl_matrix_view_array((double*)_PyMatrix$argnum->data,
    $1 = &matrix$argnum.matrix;
share|improve this answer
I am still not sure how to use this. Anyway, I don't think pygsl is a solution. I don't need to call gsl functions, I need to be able to pass a gsl_matrix to my C++ program from python. I.e. I need to make my C++ program "seen" as a python module to send and return gsl_matrices. –  Rene Sep 15 '13 at 6:08
I put this pygsl implementation code because it seems to be the way pygsl sends the python array to GSL ("(double*)_PyMatrix$argnum->data"). So, it seems the conversion to C/c++ gsl_matrix has to be somewhat similar to this. –  Vinicius Miranda Sep 16 '13 at 18:26
I still have no idea of what to do with this. –  Rene Sep 24 '13 at 8:41
add comment

Your Answer


