PyUblas solves one main difficulty of developing hybrid numerical codes in Python and C++: It integrates two major linear algebra libraries across the two languages, namely numpy and Boost.Ublas. In Python, you are working with native numpy arrays, whereas in C++, PyUblas lets you work with matrix and vector types immediately derived from and closely integrated with Ublas. And best of all: There’s no copying at the language boundary.
PyUblas is built using and meant to be used with Boost Python.
PyUblas also has its own web page.
Ok, here’s a simple sample extension:
#include <pyublas/numpy.hpp>
pyublas::numpy_vector<double> doublify(pyublas::numpy_vector<double> x)
{
return 2*x;
}
BOOST_PYTHON_MODULE(sample_ext)
{
boost::python::def("doublify", doublify);
}
and some Python that uses it:
import numpy
import sample_ext
import pyublas # not explicitly used--but makes converters available
vec = numpy.ones((5,), dtype=float)
print vec
print sample_ext.doublify(vec)
and this is what gets printed:
[ 1. 1. 1. 1. 1.]
[ 2. 2. 2. 2. 2.]