3

From specification of a numpy array at here:

typedef struct PyArrayObject {
    PyObject_HEAD
    char *data;
    int nd;
    npy_intp *dimensions;
    npy_intp *strides;
    PyObject *base;
    PyArray_Descr *descr;
    int flags;
    PyObject *weakreflist;
} PyArrayObject;

When I look at the specification of a numpy array, I don't see that it stores number of elements of the array. Is that really the case?

What is the advantage of not storing that?

Thank you.

2
  • 2
    It appears as if it stores arrays of both dimensions and strides, however. In most implementations involving array operations, this is the information actually required rather than total number of items which does nothing to help you access them systematically. Commented Sep 7, 2016 at 19:46
  • I guess I get this now. When the size of array is large, multiplying the dimensions to get the size would only take a tiny fraction of time for the whole calculation. Commented Sep 9, 2016 at 15:14

2 Answers 2

5

The size (that is, the total number of elements in the array) is computed as the product of the values in the array dimensions. The length of that array is nd.

In the C code that implements the core of numpy, you'll find many uses of the macro PyArray_SIZE(obj). Here's the definition of that macro:

#define PyArray_SIZE(m) PyArray_MultiplyList(PyArray_DIMS(m), PyArray_NDIM(m))

The advantage of not storing it is, well, not storing redundant data.

Sign up to request clarification or add additional context in comments.

Comments

2

Look at PyArray_ArrayDescr *PyArray_Descr.subarray:

If this is non- NULL, then this data-type descriptor is a C-style contiguous array of another data-type descriptor. In other-words, each element that this descriptor describes is actually an array of some other base descriptor. This is most useful as the data-type descriptor for a field in another data-type descriptor. The fields member should be NULL if this is non- NULL (the fields member of the base descriptor can be non- NULL however). The PyArray_ArrayDescr structure is defined using

typedef struct {
    PyArray_Descr *base;
    PyObject *shape;      /* <-------- */
} PyArray_ArrayDescr;

and:

PyObject *PyArray_ArrayDescr.shape

The shape (always C-style contiguous) of the sub-array as a Python tuple.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.