boost::multi_array SWIG binding
Hi all, I'm fairly new to C++ and learning as I go, but there is very little info out there on my particular problem. I am trying to turn a model I've written in C++ into a python module using SWIG. The main problems I'm having are initializing Boost multiarray containers then getting data into and out of them. Here is some sample code: #include <iostream> #include <vector> #include <algorithm> #include "boost/multi_array.hpp" typedef boost::multi_array<double,2> array_typef; typedef boost::multi_array<int,2> array_typei; class G2G_hydro_model { public: array_typei A; array_typef S; G2G_hydro_model(int A_ncols,int A_nrows,int S_ncols,int S_nrows):, A(boost::extents[A_ncols][A_nrows]), S(boost::extents[S_ncols][S_nrows]) {}; ~G2G_hydro_model(void) {}; void read_floating(int x, int y, array_typef& data, double& element); void write_floating(int x, int y, array_typef& data, double& element); void write_integer(int x, int y, array_typei& data, int& element); void functions_to_do_stuff(array_typef&,array_typei&) }; void G2G_hydro_model::read_floating(int x, int y, array_typef data, double element) {element = data[x][y];} void G2G_hydro_model::write_floating(int x, int y, double element, array_typef data) {data[x][y] = element;} void G2G_hydro_model::write_integer(int x, int y, int element, array_typei data) {data[x][y] = element;} Code is pushed through SWIG to create bindings, then compiled as a linked library. Compiles ok with no warnings and Python accepts the module. But when I try to use with Python code like this: import sys import numpy as np import G2G h = G2G4.G2G_hydro_model(A.cols,A.nrows,S.cols,S.rows) element = 9 for x in range(0,A.cols) for y in range(0,A.rows) h.write_integer(x, y, element, h.A) Unfortunately I get the following error message: python: /usr/include/boost/multi_array/multi_array_ref.hpp:481: boost::multi_array_ref<T, NumDims>& boost::multi_array_ref<T, NumDims>::operator=(const ConstMultiArray&) [with ConstMultiArray = boost::multi_array<double, 2ul, std::allocator<double> >, T = double, long unsigned int NumDims = 2ul]: Assertion `std::equal(other.shape(), other.shape()+this->num_dimensions(), this->shape())' failed. I think I have assigned the array extents incorrectly, but I'm not sure of the proper way to do this as part of a C++ class. Any ideas, I'm starting to pull my hair out, I hope there is a simple answer. Many Thanks Niall -- __________________________________________________________________ Niall Duncan, Postgraduate Student Institute for Energy Systems, School of Engineering and Electronics The University of Edinburgh www.see.ed.ac.uk -- The University of Edinburgh is a charitable body, registered in Scotland, with registration number SC005336.
AMDG NJ Duncan wrote:
I'm fairly new to C++ and learning as I go, but there is very little info out there on my particular problem.
I am trying to turn a model I've written in C++ into a python module using SWIG. The main problems I'm having are initializing Boost multiarray containers then getting data into and out of them.
Here is some sample code:
<snip>
Unfortunately I get the following error message:
python: /usr/include/boost/multi_array/multi_array_ref.hpp:481: boost::multi_array_ref<T, NumDims>& boost::multi_array_ref<T, NumDims>::operator=(const ConstMultiArray&) [with ConstMultiArray = boost::multi_array<double, 2ul, std::allocator<double> >, T = double, long unsigned int NumDims = 2ul]: Assertion `std::equal(other.shape(), other.shape()+this->num_dimensions(), this->shape())' failed.
I think I have assigned the array extents incorrectly, but I'm not sure of the proper way to do this as part of a C++ class.
I don't think that's the problem. Something is assuming that boost::multi_array has a normal assignment operator--it doesn't. You can only assign multi-arrays that have the same dimensions. Since there are no assignments in the code you posted, it's hard to tell where the problem is. If you can get a stack trace from the assertion, the error will probably be easy to spot.
Any ideas, I'm starting to pull my hair out, I hope there is a simple answer.
In Christ, Steven Watanabe
participants (2)
-
NJ Duncan
-
Steven Watanabe