[C++-sig] How to call reshape((M,N),order='F') ?

Sebastian Walter walter at mathematik.hu-berlin.de
Wed Oct 28 20:31:33 CET 2009


> Hello to everyone.
>
> I have this problem.
>
> Assume I'm building a to-Python converter for a 3x2 matrix type,
> FORTRAN storage (these are not the real dimensions of my problem, but
> I give these numbers only to simplify the discussion).
>
> Into the "convert" C++ method, first, I build a list with all the
> matrix elements (lout);
> then, I instantiate a boost::python::numeric::array in this way (note:
> I had previously used
> set_module_and_type("numpy","ndarray"), for using the NumPy's array):
>
> boost::python::numeric::array   v(lout)
>
> Finally, I'd like to call "reshape" on this array, in this way
>
> v.attr("reshape")(make_tuple(3,2),"F");

don't know the answer to your question, but you could do the following:

set the strides of the underlying PyArrayObject manually to match column
major format, i.e.
PyArrayObject  *v_array_obj = reinterpret_cast<PyArrayObject*> ( v.ptr() );
v_array_obj->strides[0] = sizeof(double);
v_array_obj->strides[1] = 3 * sizeof(double);

hope that helps,

Sebastian


>
> But it does not work. It builds, but, on runtime, I obtain the error:
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: an integer is required
>
> I tried to build and re-shape a matrix entirely from Python and I
> discovered that, when using reshape,
> you are forced to give the second parameter as
>
> order = 'C' or 'F'
>
> Now the question is: is it possible to specify a named parameter from
> the C++ code ?
> If the answer is "yes", how ?
>
> Thank you in advance,
> Michele
>
> --
> Michele De Stefano
> http://www.linkedin.com/in/micdestefano
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig at python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>


-- 
Sebastian Walter

Institut fuer Mathematik, Humboldt-Universitaet    Tel: +49 (30) 2093-5869
Rudower Chaussee 25, Adlershof, Berlin             Fax: +49 (30) 2093-5859
Post: Unter den Linden 6, D-10099 Berlin         Email: 
walter at mathematik.hu-berlin.de
WWW: http://www.mathematik.hu-berlin.de/~walter



More information about the Cplusplus-sig mailing list