1

How would one get a view of a PyArrayObject* similar to the following python code?

# n-column array x
# d is the length of each column
print(x.shape)  # => (d, n)

by_column = [x[::,i] for i in range(x.shape[1])]

assert len(by_column) == n

print(by_column[n-1].shape)  # => (d,)

So far my code is this:

// my_array is a PyArrayObject* 
std::vector<PyArrayObject*> columns = {};

npy_intp* dims = my_array->dimensions;
npy_intp* strides = my_array->strides;

std::vector<int> shape = {};
for (int i = 0; &dims[i] != strides; i++){
    shape.push_back(dims[i]);
}

switch (shape.size()) {
    case 1: {
        // handle 1D array by simply iterating
    }
    case 2: {
        int columns = shape[1];
        // What now?
    }
}

I'm having trouble finding any reference to do this in C/C++ in both the documentation and the source code, could you give an example of how one would do this?

The C/C++ API for numpy seems really convoluted when compared to something like std::vector, and the documentation isn't very beginner-friendly either, so any references to easier guides would be appreciated too.

Martmists
  • 152
  • 1
  • 6

1 Answers1

2

You should access the internal structure of PyArrayObject via the PyArray_XXX functions like PyArray_NDIM. To get the contents of a sequence, you use PyObject_GetItem with a tuple key, where in your use case the tuple will have a PySliceObject as the first element.

mattip
  • 2,360
  • 1
  • 13
  • 13