[C++-sig] Re: regarding your post entitled " Numeric--factory function" onBoost.Python

Faheem Mitha faheem at email.unc.edu
Wed Nov 3 05:18:48 CET 2004



On Tue, 2 Nov 2004, Faheem Mitha wrote:

>
> Hi Li,
>
> Thanks for your helpful reply.
[snip]

Hi Li,

I solved it! (I think). I was using a function that converted a matrix to 
a list of lists, where each list corresponded to a row of the matrix. Just 
looking at the example below (in my earlier mail)

************************************************************************
object new_array()
{
     return numeric::array(
         make_tuple(
             make_tuple(1,2,3)
           , make_tuple(4,5,6)
           , make_tuple(7,8,9)
             )
         );
}
*************************************************************************

made me realise that wrapping the final return result (list of lists) in a 
numeric::array might work, since apparently the constructor accepted this 
kind of syntax. Well, apparently it does, though I don't understand why. I 
don't see this constructor documented anywhere. Also, I don't understand 
why my previous attempts don't work.

See function below.

If you can enlighten me about any of this, please do so. Thanks.

                                                                Faheem.

*************************************************************************
// Convert (C++) numeric matrix to (Python) list.
boost::python::object cppmat2pynumarr(Array2D<double> A)
{
   int i, j;
   int dim1 = A.dim1();
   int dim2 = A.dim2();

   boost::python::list lst;
   boost::python::list rowlst;

   // Copy matrix to python list
   for(i=0; i<dim1; i++)
     {
       for(j=0; j<dim2; j++)
 	{
 	  double Aij = A[i][j];
 	  rowlst.append(Aij);
 	}
       lst.append(rowlst);
       rowlst = boost::python::list();
     }
   return boost::python::numeric::array(lst);
}



More information about the Cplusplus-sig mailing list