[Numpy-discussion] Accessing irregular sized array data from C

Mads Ipsen mads.ipsen at gmail.com
Wed Jul 2 06:15:25 EDT 2014


Hi,

If you setup an M x N array like this

  a = 1.0*numpy.arange(24).reshape(8,3)

you can access the data from a C function like this

void foo(PyObject * numpy_data)
{
    // Get dimension and data pointer
    int const m = static_cast<int>(PyArray_DIMS(numpy_data)[0]);
    int const n = static_cast<int>(PyArray_DIMS(numpy_data)[1]);
    double * const data = (double *) PyArray_DATA(numpy_data);

    // Access data
    ...
}

Now, suppose I have an irregular shaped numpy array like this

  a1 = numpy.array([ 1.0, 2.0, 3.0])
  a2 = numpy.array([-2.0, 4.0])
  a3 = numpy.array([5.0])
  b  = numpy.array([a1,a2,a3])

How can open up the doors to the array data of b on the C-side?

Best regards,

Mads

-- 
+---------------------------------------------------------+
| Mads Ipsen                                              |
+----------------------+----------------------------------+
| Gåsebæksvej 7, 4. tv | phone:              +45-29716388 |
| DK-2500 Valby        | email:      mads.ipsen at gmail.com |
| Denmark              | map  :   www.tinyurl.com/ns52fpa |
+----------------------+----------------------------------+



More information about the NumPy-Discussion mailing list