I'm having some problems with the use of multi_array's with python.
What I'm trying to do is to export some functions that create
multi_array's and pass these multi_array's to other functions that will
extract other array's from them and display them.
Lets say I have this as an example.
//ArrayRef2D.h typedef for a 2D multi_array of doubles
#ifndef ARRAYREF2D_H_
#define ARRAYREF2D_H_
#include <boost/multi_array.hpp>
typedef boost::const_multi_array_ref < double, 2 >
const_array_type2D_ref_d_t;
typedef boost::multi_array < double, 2 > array_type2D_d_t;
#endif /*ARRAYREF2D_H_*/
//Fstd.h 2D multi_array generator
#ifndef FSTD_H_
#define FSTD_H_
#include "ArrayRef2D.h"
#include <string>
using namespace std;
class Fstd
{
public:
Fstd(string filename) : filename_(filename){}
array_type2D_d_t get2Dfield()
{
array_type2D_d_t matrix(boost::extents[2][2]);
matrix[0][0] = 0;
matrix[0][1] = -1;
matrix[1][0] = 20;
matrix[1][1] = -30;
return matrix;
}
private:
string filename_;
};
#endif /*FSTD_H_*/
//MatrixAlgorithm2D.h provides methods for displaying and
manipulating matrices
#ifndef MATRIXALGORITHM2D_H_
#define MATRIXALGORITHM2D_H_
#include "ArrayRef2D.h"
#include <cassert>
#include <cmath>
#include <iostream>
using namespace std;
class MatrixAlgorithm2D {
public:
static array_type2D_d_t windChill(const_array_type2D_ref_d_t TT,
const_array_type2D_ref_d_t UV)
{
assert((TT.shape()[0] == UV.shape()[0]) &&
(TT.shape()[1] == UV.shape()[1]));
array_type2D_d_t
RE(boost::extents[TT.shape()[0]][TT.shape()[1]]);
for (unsigned int x = 0; x < TT.shape()[0]; x++) {
for (unsigned int y = 0; y < TT.shape()[1]; y++) {
if ((TT[x][y] <= 0.0) && (UV[x][y] >=
5.0)) {
RE[x][y] = ( 13.1200 + 0.6215 * (TT[x][y]) +
(0.3965 * (TT[x][y]) - 11.3700) * (pow((UV[x][y]),0.16)));
} else {
RE[x][y] = (TT[x][y]);
}
}
}
return RE;
}
static void display_d(const_array_type2D_ref_d_t a)
{
for (unsigned int x = 0; x < a.shape()[0]; x++) {
for (unsigned int y = 0; y < a.shape()[1]; y++) {
cout << a[x][y];
if(y < a.shape()[1] - 1) {
cout << ", ";
}
}
cout<<endl;
}
}
};
#endif /*MATRIXALGORITHM2D_H_*/
//matrix.cpp boost.python interface file generated with pyste
// Boost Includes
==============================================================
#include "boost/python.hpp"
#include "boost/cstdint.hpp"
// Includes
====================================================================
#include "Fstd.h"
#include "MatrixAlgorithm2D.h"
// Using
=======================================================================
using namespace boost::python;
// Module
======================================================================
BOOST_PYTHON_MODULE(matrix)
{
class_< Fstd >("Fstd", init< const Fstd& >())
.def(init< std::string >())
.def("get2Dfield", &Fstd::get2Dfield)
;
class_< MatrixAlgorithm2D >("MatrixAlgorithm2D", init<
>())
.def(init< const MatrixAlgorithm2D& >())
.def("windChill", &MatrixAlgorithm2D::windChill)
.def("display_d", &MatrixAlgorithm2D::display_d)
.staticmethod("windChill")
.staticmethod("display_d")
;
}
I generated my so file with bjam and then
Python 2.3.5 (#2, Sep 4 2005, 22:01:42)
[GCC 3.3.5 (Debian 1:3.3.5-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import matrix
>>> dir(matrix)
['Fstd', 'MatrixAlgorithm2D', '__doc__', '__file__', '__name__']
>>> m = matrix.Fstd("HELLO")
>>> l = m.get2Dfield()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: No to_python (by-value) converter found for C++ type:
boost::multi_array<double, 2, std::allocator<double> >
So you see I can't use my code, what am I doing wrong?
I would have liked to declare 2 arrays with get2Dfield() and pass these
array to my windChill function to get another 2d matrix which I could
then display with my display_d function.