numpy 2.0.0
src/multiarray/item_selection.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 "common.h"
#include "ctors.h"
#include "lowlevel_strided_loops.h"

Defines

#define PY_SSIZE_T_CLEAN
#define _MULTIARRAYMODULE
#define NPY_NO_PREFIX
#define PyAO   PyArrayObject
#define _check_axis   PyArray_CheckAxis
#define SWAPAXES(op, ap)
#define SWAPBACK(op, ap)
#define SWAPINTP(a, b)   {intp c; c=(a); (a) = (b); (b) = c;}
#define SWAPAXES2(ap)
#define SWAPBACK2(ap)

Functions

NPY_NO_EXPORT PyObject * PyArray_TakeFrom (PyArrayObject *self0, PyObject *indices0, int axis, PyArrayObject *ret, NPY_CLIPMODE clipmode)
NPY_NO_EXPORT PyObject * PyArray_PutTo (PyArrayObject *self, PyObject *values0, PyObject *indices0, NPY_CLIPMODE clipmode)
NPY_NO_EXPORT PyObject * PyArray_PutMask (PyArrayObject *self, PyObject *values0, PyObject *mask0)
NPY_NO_EXPORT PyObject * PyArray_Repeat (PyArrayObject *aop, PyObject *op, int axis)
NPY_NO_EXPORT PyObject * PyArray_Choose (PyArrayObject *ip, PyObject *op, PyArrayObject *ret, NPY_CLIPMODE clipmode)
static int _new_sort (PyArrayObject *op, int axis, NPY_SORTKIND which)
static PyObject * _new_argsort (PyArrayObject *op, int axis, NPY_SORTKIND which)
static int qsortCompare (const void *a, const void *b)
NPY_NO_EXPORT int PyArray_Sort (PyArrayObject *op, int axis, NPY_SORTKIND which)
static int argsort_static_compare (const void *ip1, const void *ip2)
NPY_NO_EXPORT PyObject * PyArray_ArgSort (PyArrayObject *op, int axis, NPY_SORTKIND which)
NPY_NO_EXPORT PyObject * PyArray_LexSort (PyObject *sort_keys, int axis)
static void local_search_left (PyArrayObject *arr, PyArrayObject *key, PyArrayObject *ret)
static void local_search_right (PyArrayObject *arr, PyArrayObject *key, PyArrayObject *ret)
NPY_NO_EXPORT PyObject * PyArray_SearchSorted (PyArrayObject *op1, PyObject *op2, NPY_SEARCHSIDE side)
NPY_NO_EXPORT PyObject * PyArray_Diagonal (PyArrayObject *self, int offset, int axis1, int axis2)
NPY_NO_EXPORT PyObject * PyArray_Compress (PyArrayObject *self, PyObject *condition, int axis, PyArrayObject *out)
NPY_NO_EXPORT npy_intp PyArray_CountNonzero (PyArrayObject *self)
NPY_NO_EXPORT PyObject * PyArray_Nonzero (PyArrayObject *self)

Variables

static PyArrayObjectglobal_obj
static char * global_data

Define Documentation

#define _check_axis   PyArray_CheckAxis

Referenced by PyArray_TakeFrom().

#define _MULTIARRAYMODULE
#define NPY_NO_PREFIX
#define PY_SSIZE_T_CLEAN
#define PyAO   PyArrayObject
#define SWAPAXES (   op,
  ap 
)
Value:
{                                      \
        orign = (ap)->nd-1;                                     \
        if (axis != orign) {                                    \
            (op) = (PyAO *)PyArray_SwapAxes((ap), axis, orign); \
            Py_DECREF((ap));                                    \
            if ((op) == NULL) return NULL;                      \
        }                                                       \
        else (op) = (ap);                                       \
    }
Consumes reference to ap (op gets it) op contains a version of the array with axes swapped if local variable axis is not the last dimension. Origin must be defined locally.
#define SWAPAXES2 (   ap)
Value:
{                                                 \
        orign = (ap)->nd-1;                                             \
        if (axis != orign) {                                            \
            SWAPINTP(ap->dimensions[axis], ap->dimensions[orign]);      \
            SWAPINTP(ap->strides[axis], ap->strides[orign]);            \
            PyArray_UpdateFlags(ap, CONTIGUOUS | FORTRAN);              \
        }                                                               \
    }
#define SWAPBACK (   op,
  ap 
)
Value:
{                                      \
        if (axis != orign) {                                    \
            (op) = (PyAO *)PyArray_SwapAxes((ap), axis, orign); \
            Py_DECREF((ap));                                    \
            if ((op) == NULL) return NULL;                      \
        }                                                       \
        else (op) = (ap);                                       \
    }
Consumes reference to ap (op gets it) origin must be previously defined locally. SWAPAXES must have been called previously. op contains the swapped version of the array.
#define SWAPBACK2 (   ap)
Value:
{                                                 \
        if (axis != orign) {                                            \
            SWAPINTP(ap->dimensions[axis], ap->dimensions[orign]);      \
            SWAPINTP(ap->strides[axis], ap->strides[orign]);            \
            PyArray_UpdateFlags(ap, CONTIGUOUS | FORTRAN);              \
        }                                                               \
    }
#define SWAPINTP (   a,
 
)    {intp c; c=(a); (a) = (b); (b) = c;}
These swap axes in-place if necessary

Function Documentation

static PyObject* _new_argsort ( PyArrayObject op,
int  axis,
NPY_SORTKIND  which 
) [static]
static int _new_sort ( PyArrayObject op,
int  axis,
NPY_SORTKIND  which 
) [static]
These algorithms use special sorting. They are not called unless the underlying sort function for the type is available. Note that axis is already valid. The sort functions require 1-d contiguous and well-behaved data. Therefore, a copy will be made of the data if needed before handing it to the sorting routine. An iterator is constructed and adjusted to walk over all but the desired sorting axis.

References _strided_byte_swap(), _unaligned_strided_byte_copy(), PyArrayIterObject_tag::dataptr, PyArray_ITER_NEXT, PyDataMem_FREE, and PyDataMem_NEW.

static int argsort_static_compare ( const void *  ip1,
const void *  ip2 
) [static]
static void local_search_left ( PyArrayObject arr,
PyArrayObject key,
PyArrayObject ret 
) [static]
&#64;brief Use bisection of sorted array to find first entries >= keys.
For each key use bisection to find the first index i s.t. key <= arr[i]. When there is no such index i, set i = len(arr). Return the results in ret. All arrays are assumed contiguous on entry and both arr and key must be of the same comparable type.
&#64;param arr contiguous sorted array to be searched. &#64;param key contiguous array of keys. &#64;param ret contiguous array of intp for returned indices. &#64;return void
static void local_search_right ( PyArrayObject arr,
PyArrayObject key,
PyArrayObject ret 
) [static]
&#64;brief Use bisection of sorted array to find first entries > keys.
For each key use bisection to find the first index i s.t. key < arr[i]. When there is no such index i, set i = len(arr). Return the results in ret. All arrays are assumed contiguous on entry and both arr and key must be of the same comparable type.
&#64;param arr contiguous sorted array to be searched. &#64;param key contiguous array of keys. &#64;param ret contiguous array of intp for returned indices. &#64;return void
NPY_NO_EXPORT PyObject* PyArray_ArgSort ( PyArrayObject op,
int  axis,
NPY_SORTKIND  which 
)
ArgSort an array

Creates new reference op2
Determine if we should use new algorithm or not
ap will contain the reference to op2

NPY_NO_EXPORT PyObject* PyArray_Choose ( PyArrayObject ip,
PyObject *  op,
PyArrayObject ret,
NPY_CLIPMODE  clipmode 
)

Convert all inputs to arrays of a common type Also makes them C-contiguous
Broadcast all arrays to each other, index array at the end.
Set-up return array
we need to make sure and get a copy so the input array is not changed before the error is called

NPY_NO_EXPORT PyObject* PyArray_Compress ( PyArrayObject self,
PyObject *  condition,
int  axis,
PyArrayObject out 
)
Compress
NPY_NO_EXPORT npy_intp PyArray_CountNonzero ( PyArrayObject self)
Counts the number of non-zero elements in the array <blockquote> Returns -1 on error.</blockquote>

If it's a trivial one-dimensional loop, don't use an iterator
If the array has size zero, return zero (the iterator rejects size zero arrays)
Otherwise create and use an iterator to count the nonzeros
Get the pointers for inner loop iteration
Iterate over all the elements to count the nonzeros

Referenced by array_empty_like().

NPY_NO_EXPORT PyObject* PyArray_Diagonal ( PyArrayObject self,
int  offset,
int  axis1,
int  axis2 
)
Diagonal

insert at the end
count = ceil((stop-start)/step)
my_diagonal = [] for i in range (s [0]) : my_diagonal.append (diagonal (a [i], offset)) return array (my_diagonal)

Referenced by array_variance().

NPY_NO_EXPORT PyObject* PyArray_LexSort ( PyObject *  sort_keys,
int  axis 
)
LexSort an array providing indices that will sort a collection of arrays lexicographically. The first key is sorted on first, followed by the second key -- requires that arg"merge"sort is available for each sort_key
Returns an index array that shows the indexes for the lexicographic sort along the given axis.

Now we can check the axis
single element case
Now do the sorting

Referenced by PyArray_GetEndianness().

NPY_NO_EXPORT PyObject* PyArray_Nonzero ( PyArrayObject self)
Nonzero <blockquote> TODO: In NumPy 2.0, should make the iteration order a parameter.</blockquote>

Allocate the result as a 2D array
If it's a one-dimensional result, don't use an iterator
Build an iterator tracking a multi-index, in C order
Get the pointers for inner loop iteration
Get the multi-index for each non-zero element
Treat zero-dimensional as shape (1,)
Create views into ret, one for each dimension
Directly switch to one dimensions (dimension 1 is 1 anyway)

NPY_NO_EXPORT PyObject* PyArray_PutMask ( PyArrayObject self,
PyObject *  values0,
PyObject *  mask0 
)
Put values into an array according to a mask.

<

zero if null array

NPY_NO_EXPORT PyObject* PyArray_PutTo ( PyArrayObject self,
PyObject *  values0,
PyObject *  indices0,
NPY_CLIPMODE  clipmode 
)
Put values into an array
NPY_NO_EXPORT PyObject* PyArray_Repeat ( PyArrayObject aop,
PyObject *  op,
int  axis 
)
Repeat the array.

nd == 0
Construct new array

NPY_NO_EXPORT PyObject* PyArray_SearchSorted ( PyArrayObject op1,
PyObject *  op2,
NPY_SEARCHSIDE  side 
)
Numeric.searchsorted(a,v)

need ap1 as contiguous array and of right type
need ap2 as contiguous array and of right type
ret is a contiguous array of intp type to hold returned indices
check that comparison function exists

References PyArrayObject::dimensions, PyArray_Dims::len, MAX_DIMS, MIN, PyArray_Dims::ptr, PyAO, PyArray_DATA, PyArray_INTP, PyArray_IterNew(), PyArray_New(), PyArray_Transpose(), and PyArray_Type.

Referenced by array_argsort().

NPY_NO_EXPORT int PyArray_Sort ( PyArrayObject op,
int  axis,
NPY_SORTKIND  which 
)
Sort an array in-place

Determine if we should use type-specific algorithm or not
Store global -- allows re-entry -- restore before leaving

<

Should update op if needed

NPY_NO_EXPORT PyObject* PyArray_TakeFrom ( PyArrayObject self0,
PyObject *  indices0,
int  axis,
PyArrayObject ret,
NPY_CLIPMODE  clipmode 
)
static int qsortCompare ( const void *  a,
const void *  b 
) [static]

Variable Documentation

char* global_data [static]
Be sure to save this global_compare when necessary