numpy 2.0.0
src/multiarray/shape.c File Reference
#include <Python.h>
#include "structmember.h"
#include "numpy/arrayobject.h"
#include "numpy/arrayscalars.h"
#include "numpy/npy_math.h"
#include "npy_config.h"
#include "numpy/npy_3kcompat.h"
#include "ctors.h"
#include "shape.h"

Defines

#define PY_SSIZE_T_CLEAN
#define _MULTIARRAYMODULE
#define NPY_NO_PREFIX
#define PyAO   PyArrayObject

Functions

static int _check_ones (PyArrayObject *self, int newnd, intp *newdims, intp *strides)
static int _fix_unknown_dimension (PyArray_Dims *newshape, intp s_original)
static int _attempt_nocopy_reshape (PyArrayObject *self, int newnd, intp *newdims, intp *newstrides, int fortran)
static void _putzero (char *optr, PyObject *zero, PyArray_Descr *dtype)
NPY_NO_EXPORT PyObject * PyArray_Resize (PyArrayObject *self, PyArray_Dims *newshape, int refcheck, NPY_ORDER fortran)
NPY_NO_EXPORT PyObject * PyArray_Newshape (PyArrayObject *self, PyArray_Dims *newdims, NPY_ORDER order)
NPY_NO_EXPORT PyObject * PyArray_Reshape (PyArrayObject *self, PyObject *shape)
NPY_NO_EXPORT PyObject * PyArray_Squeeze (PyArrayObject *self)
NPY_NO_EXPORT PyObject * PyArray_SwapAxes (PyArrayObject *ap, int a1, int a2)
NPY_NO_EXPORT PyObject * PyArray_Transpose (PyArrayObject *ap, PyArray_Dims *permute)
int _npy_stride_sort_item_comparator (const void *a, const void *b)
NPY_NO_EXPORT void PyArray_CreateSortedStridePerm (PyArrayObject *arr, _npy_stride_sort_item *strideperm)
NPY_NO_EXPORT PyObject * PyArray_Ravel (PyArrayObject *a, NPY_ORDER order)
NPY_NO_EXPORT PyObject * PyArray_Flatten (PyArrayObject *a, NPY_ORDER order)

Define Documentation

#define _MULTIARRAYMODULE
#define NPY_NO_PREFIX
#define PY_SSIZE_T_CLEAN
#define PyAO   PyArrayObject

Function Documentation

static int _attempt_nocopy_reshape ( PyArrayObject self,
int  newnd,
intp newdims,
intp newstrides,
int  fortran 
) [static]
attempt to reshape an array without copying data
This function should correctly handle all reshapes, including axes of length 1. Zero strides should work but are untested.
If a copy is needed, returns 0 If no copy is needed, returns 1 and fills newstrides

System Message: ERROR/3 (<string>, line 8) Unexpected indentation.

<blockquote> with appropriate strides</blockquote>

The "fortran" argument describes how the array should be viewed during the reshape, not how it is stored in memory (that information is in self->strides).
If some output dimensions have length 1, the strides assigned to them are arbitrary. In the current implementation, they are the stride of the next-fastest index.

fprintf(stderr, "_attempt_nocopy_reshape( ("); for (oi=0; oi<oldnd; oi++) fprintf(stderr, "(d,d), ", olddims[oi], oldstrides[oi]); fprintf(stderr, ") -> ("); for (ni=0; ni<newnd; ni++) fprintf(stderr, "(d,*), ", newdims[ni]); fprintf(stderr, "), fortran=d)n", fortran);
different total sizes; no hope
the current code does not handle 0-sized arrays, so give up
not contiguous enough
C order
not contiguous enough
C order
fprintf(stderr, "success: _attempt_nocopy_reshape ("); for (oi=0; oi<oldnd; oi++) fprintf(stderr, "(d,d), ", olddims[oi], oldstrides[oi]); fprintf(stderr, ") -> ("); for (ni=0; ni<newnd; ni++) fprintf(stderr, "(d,d), ", newdims[ni], newstrides[ni]); fprintf(stderr, ")n");

static int _check_ones ( PyArrayObject self,
int  newnd,
intp newdims,
intp strides 
) [static]
inserts 0 for strides where dimension will be 1

References _putzero(), and NPY_TITLE_KEY.

static int _fix_unknown_dimension ( PyArray_Dims newshape,
intp  s_original 
) [static]
int _npy_stride_sort_item_comparator ( const void *  a,
const void *  b 
)
Sorts items so stride is descending, because C-order is the default in the face of ambiguity.

Sort the absolute value of the strides
Make the qsort stable by next comparing the perm order. (Note that two perm entries will never be equal)

References NPY_CORDER, and PyArray_Newshape().

static void _putzero ( char *  optr,
PyObject *  zero,
PyArray_Descr dtype 
) [static]

References MAX_DIMS.

Referenced by _check_ones().

NPY_NO_EXPORT void PyArray_CreateSortedStridePerm ( PyArrayObject arr,
_npy_stride_sort_item strideperm 
)
This function populates the first PyArray_NDIM(arr) elements of strideperm with sorted descending by their absolute values. For example, the stride array (4, -2, 12) becomes [(2, 12), (0, 4), (1, -2)].

Set up the strideperm values
Sort them

NPY_NO_EXPORT PyObject* PyArray_Flatten ( PyArrayObject a,
NPY_ORDER  order 
)
Flatten
NPY_NO_EXPORT PyObject* PyArray_Newshape ( PyArrayObject self,
PyArray_Dims newdims,
NPY_ORDER  order 
)
Returns a new array with the new shape from the data in the old array --- order-perspective depends on fortran argument. copy-only-if-necessary
New shape for an array

Quick check to make sure anything actually needs to be done
Returns a pointer to an appropriate strides array if all we are doing is inserting ones into the shape, or removing ones from the shape or doing a combination of the two In this case we don't need to do anything but update strides and dimensions. So, we can handle non single-segment cases.
we are really re-shaping not just adding ones to the shape somewhere fix any -1 dimensions and check new-dimensions against old size
sometimes we have to create a new copy of the array in order to get the right orientation and because we can't just re-use the buffer with the data in the order it is in.
no need to copy the array after all
We always have to interpret the contiguous buffer correctly
Make sure the flags argument is set.
replace any 0-valued strides with appropriate value to preserve contiguousness

References FALSE.

Referenced by _npy_stride_sort_item_comparator(), and _strings_richcompare().

NPY_NO_EXPORT PyObject* PyArray_Ravel ( PyArrayObject a,
NPY_ORDER  order 
)
Ravel
Returns a contiguous array

For KEEPORDER, check if we can make a flattened view
If all the strides matched a contiguous layout, return a view

Referenced by PyArray_Concatenate(), and PyArray_CopyAnyIntoOrdered().

NPY_NO_EXPORT PyObject* PyArray_Reshape ( PyArrayObject self,
PyObject *  shape 
)
For back-ward compatability -- Not recommended
Reshape
NPY_NO_EXPORT PyObject* PyArray_Resize ( PyArrayObject self,
PyArray_Dims newshape,
int  refcheck,
NPY_ORDER  fortran 
)
Resize (reallocate data). Only works if nothing else is referencing this
array and it is contiguous. If refcheck is 0, then the reference count is not checked and assumed to be 1. You still must own this data and have no weak-references and no base object.

Reallocate space if needed
Fill new memory with zeros
Different number of dimensions.
Need new dimensions and strides arrays
make new_strides variable

NPY_NO_EXPORT PyObject* PyArray_Squeeze ( PyArrayObject self)
return a new view of the array object with all of its unit-length
dimensions squeezed out if needed, otherwise return the same array.
NPY_NO_EXPORT PyObject* PyArray_SwapAxes ( PyArrayObject ap,
int  a1,
int  a2 
)
SwapAxes
NPY_NO_EXPORT PyObject* PyArray_Transpose ( PyArrayObject ap,
PyArray_Dims permute 
)
Return Transpose.

this allocates memory for dimensions and strides (but fills them incorrectly), sets up descr, and points data at ap->data.
point at true owner of memory:
fix the dimensions and strides of the return-array

Referenced by PyArray_SearchSorted().