Forum: GNU Radio vectors of vectors in python

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Achilleas A. (Guest)
on 2007-02-02 06:48
(Received via mailing list)
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<int> > d_PS;
public:
   const std::vector< std::vector<int> > & 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
Michael D. (Guest)
on 2007-02-07 17:31
(Received via mailing list)
On Feb 1, 2007, at 11:47 PM, Achilleas A. wrote:
> private:
>   std::vector< std::vector<int> > d_PS;
> public:
>   const std::vector< std::vector<int> > & 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<int> VecInt;
typedef std::vector<VecInt> 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<int> > V2Int;

won't work because SWIG can't name the internal "std::vector<int>"
data.  SWIG takes the approach of "separate and conquer".  Hope this
helps! - MLD
Eric B. (Guest)
on 2007-02-07 18:24
(Received via mailing list)
On Wed, Feb 07, 2007 at 10:31:05AM -0500, Michael D. wrote:
> typedef std::vector<int> 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<int>, <gr_complex>, <float> 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<int> >

work.

You may want to call it

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

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
This topic is locked and can not be replied to.