I created my own 2D array class in C++ and got it to work with Python using SWIG. I also extended it to have some basic functionality like NumPy arrays. For example, I created a method in my Array2D class, getitem, which returns the elements of the array:
%extend Array2D{
double __getitem__(vector<int> v) {
return (*self)[v[0]][v[1]];
}
}
After compiling the C++ extension using SWIG, I can import this into Python and get an Array2D object:
>>> import myclass
>>> A = myclass.Array2D(2,2,1.0)
which creates a 2x2 matrix of ones. Because of my extension method, I can retrieve individual elements just like with NumPy.
>>> A[0,1]
>>> 1.0
This is great and all, but when I try to use NumPy's mean function, I get the following error:
>>> mean(A)
>>> TypeError: in method 'dblArray2D___getitem__', argument 2 of type 'std::vector< int >'
It seems that NumPy's mean function doesn't know what to do with std::vector. What do I need to do with my getitem method or SWIG interface file to get my Array2D to use NumPy's mean function?
vby reference, or even constant reference