0

What is the simple and effective way to create c contiguous array from python object? Suppose I wish to create C++ Matrix class, that can be constructed using python object.

template<typename T>
struct Matrix
{
    Matrix(PyObject* obj)
    {
        // extract nrows, ncols, allocate data and copy content
    }

    T*  data;
    int nrows;
    int ncols;
};

And this constructor should accept python objects such as:

[[0., 1.], [1. 0.]]
((1, 2), (2, 3), (3, 4))
numpy.zeros((3, 5))
DikobrAz
  • 3,557
  • 4
  • 35
  • 53
  • 1
    I think, numpy does this for you, doesn't it? – User Mar 13 '13 at 15:23
  • @User So far I found this function PyArray_ContiguousFromObject, here is nice [tutorial](http://dsnra.jpl.nasa.gov/software/Python/numpydoc/nunmpy-13.html). If I understood correctly it can be used to convert python object to array of desired type. Then I can memcpy array data to my storage. I am not sure about the performance of this approach. – DikobrAz Mar 13 '13 at 15:48
  • I am not sure, too. Try it out and mesure the performance! I believe that numpy is very fast but I do not know. – User Mar 13 '13 at 18:35

0 Answers0