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 "npy_pycompat.h"
#include "ctors.h"
#include "shape.h"

Defines

#define PY_SSIZE_T_CLEAN
#define NPY_NO_DEPRECATED_API   NPY_API_VERSION
#define _MULTIARRAYMODULE

Functions

static int _check_ones (PyArrayObject *self, int newnd, npy_intp *newdims, npy_intp *strides)
static int _fix_unknown_dimension (PyArray_Dims *newshape, npy_intp s_original)
static int _attempt_nocopy_reshape (PyArrayObject *self, int newnd, npy_intp *newdims, npy_intp *newstrides, int is_f_order)
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 order)
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_SqueezeSelected (PyArrayObject *self, npy_bool *axis_flags)
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 (int ndim, npy_intp *shape, npy_intp *strides, npy_stride_sort_item *out_strideperm)
static NPY_INLINE npy_intp s_intp_abs (npy_intp x)
NPY_NO_EXPORT void PyArray_CreateMultiSortedStridePerm (int narrays, PyArrayObject **arrays, int ndim, int *out_strideperm)
NPY_NO_EXPORT PyObject * PyArray_Ravel (PyArrayObject *arr, NPY_ORDER order)
NPY_NO_EXPORT PyObject * PyArray_Flatten (PyArrayObject *a, NPY_ORDER order)
NPY_NO_EXPORT PyObject * build_shape_string (npy_intp n, npy_intp *vals)
NPY_NO_EXPORT void PyArray_RemoveAxesInPlace (PyArrayObject *arr, npy_bool *flags)

Define Documentation

#define NPY_NO_DEPRECATED_API   NPY_API_VERSION

Function Documentation

static int _attempt_nocopy_reshape ( PyArrayObject self,
int  newnd,
npy_intp newdims,
npy_intp newstrides,
int  is_f_order 
) [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 "is_f_order" argument describes how the array should be viewed during the reshape, not how it is stored in memory (that information is in PyArray_STRIDES(self)).
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, "), is_f_order=d)n", is_f_order);
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,
npy_intp newdims,
npy_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,
npy_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 PyArray_SHAPE(), PyArray_STRIDES, and s_intp_abs().

static void _putzero ( char *  optr,
PyObject *  zero,
PyArray_Descr dtype 
) [static]
NPY_NO_EXPORT PyObject* build_shape_string ( npy_intp  n,
npy_intp vals 
)
See shape.h for parameters documentation
Negative dimension indicates "newaxis", which can be discarded for printing if it's a leading dimension. Find the first non-"newaxis" dimension.

Referenced by broadcast_strides().

NPY_NO_EXPORT void PyArray_CreateMultiSortedStridePerm ( int  narrays,
PyArrayObject **  arrays,
int  ndim,
int *  out_strideperm 
)
Creates a sorted stride perm matching the KEEPORDER behavior of the NpyIter object. Because this operates based on multiple input strides, the 'stride' member of the npy_stride_sort_item would be useless and we simply argsort a list of indices instead.
The caller should have already validated that 'ndim' matches for every array in the arrays list.
Initialize the strideperm values to the identity.
This is the same as the custom stable insertion sort in the NpyIter object, but sorting in the reverse order as in the iterator. The iterator sorts from smallest stride to biggest stride (Fortran order), whereas here we sort from biggest stride to smallest stride (C order).
Set swap even if it's not ambiguous already, because in the case of conflicts between different operands, C-order wins.
Only set swap if it's still ambiguous
A comparison has been done, so it's no longer ambiguous
If the comparison was unambiguous, either shift 'ipos' to 'i1' or stop looking for an insertion point
Insert out_strideperm[i0] into the right place

References NPY_CORDER, and PyArray_Newshape().

NPY_NO_EXPORT void PyArray_CreateSortedStridePerm ( int  ndim,
npy_intp shape,
npy_intp strides,
npy_stride_sort_item out_strideperm 
)
This function populates the first ndim 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

Referenced by allocate_reduce_result(), get_decsrcref_transfer_function(), and PyArray_GetDTypeCopySwapFn().

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 order 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 NPY_FALSE.

Referenced by PyArray_CreateMultiSortedStridePerm().

NPY_NO_EXPORT PyObject* PyArray_Ravel ( PyArrayObject arr,
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
Removes the axes flagged as True from the array,

modifying it in place. If an axis flagged for removal has a shape entry bigger than one, this effectively selects index zero for that axis.

WARNING: If an axis flagged for removal has a shape equal to zero,
the array will point to invalid memory. The caller must validate this!

For example, this can be used to remove the reduction axes from a reduction result once its computation is complete.

Compress the dimensions and strides
The final number of dimensions
Update contiguous flags
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  order 
)
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
return a new view of the array object with all of its unit-length
dimensions squeezed out if needed, otherwise return the same array.
If there were no ones to squeeze out, return the same array
If self isn't not a base class ndarray, call its __array_wrap__ method

Referenced by array_squeeze().

NPY_NO_EXPORT PyObject* PyArray_SqueezeSelected ( PyArrayObject self,
npy_bool axis_flags 
)
Just like PyArray_Squeeze, but allows the caller to select a subset of the size-one dimensions to squeeze out.
Verify that the axes requested are all of size one
If there were no axes to squeeze out, return the same array
If self isn't not a base class ndarray, call its __array_wrap__ method
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 PyArray_DATA(ap).
point at true owner of memory:
fix the dimensions and strides of the return-array

Referenced by PyArray_ArgMax().