Vectors of vectors in python

Hi,

In the fsm class in gr-trellis I want to change one of the
defined matrices to be a vector of vectors instead of
an one-dimensional vector that used to be, ie,
instead of

private:
std::vector< int > d_PS;
public:
const std::vector< int > & PS () const { return d_PS; }

to make it something like

private:
std::vector< std::vector > d_PS;
public:
const std::vector< std::vector > & PS () const { return d_PS; }

This works fine, except that in python, the method
PS() does not return a vector of vectors…
(I have modified accordingly the .i file)
Is there anyway to fix this?

Thanks
Achilleas

On Feb 1, 2007, at 11:47 PM, Achilleas A. wrote:

private:
std::vector< std::vector > d_PS;
public:
const std::vector< std::vector > & PS () const { return d_PS; }

Here’s a quick, partial answer. According to the SWIG docs &
tutorials, what one needs to do is something like (these could be in
struct’s of class’es instead):

typedef std::vector VecInt;
typedef std::vector VecInt2;

Then in Python (after compilation via SWIG -> C++), something like:

import VecStuff
a = VecInt ([1 2 3])
b = VecInt ([4 5 6])
c = VecInt2 (a, b)

Please note that ‘a’ and ‘b’ above do -not- need to be the same
length, not for C++ nor SWIG nor Python, since this is not a “matrix”
but just a vector of vectors.

The exact details remain to be figured out. The basic idea is that,
in order for SWIG (and thus Python) to know what type of structure is
being dealt with, one has to name all of the relevant parts of the
structure. Thus

std::vector< std::vector > V2Int;

won’t work because SWIG can’t name the internal “std::vector”
data. SWIG takes the approach of “separate and conquer”. Hope this
helps! - MLD

On Wed, Feb 07, 2007 at 10:31:05AM -0500, Michael D. wrote:

typedef std::vector VecInt;
length, not for C++ nor SWIG nor Python, since this is not a “matrix”
data. SWIG takes the approach of “separate and conquer”. Hope this
helps! - MLD

Note that std::vector, <gr_complex>, and more are already
handled in gnuradio.i

You may be missing only a template instantiate in the .i file to have

const std::vector< std::vector >

work.

You may want to call it

const std::vector< const std::vector >

In that case, swig may just “do the right thing” and automatically
generate the correct typemap since it’s free to create new copies of
the nested vectors. I would expect (but haven’t tested) that this
would allow you to call from python using syntax like this:

foo(((1,2,3), (4,5,6), (7,8,9,10)))

Eric