numpy  2.0.0
src/multiarray/compiled_base.c File Reference
#include <Python.h>
#include <structmember.h>
#include <string.h>
#include "numpy/arrayobject.h"
#include "numpy/npy_3kcompat.h"
#include "numpy/npy_math.h"
#include "npy_config.h"
#include "templ_common.h"

Defines

#define NPY_NO_DEPRECATED_API   NPY_API_VERSION
#define _MULTIARRAYMODULE
#define LIKELY_IN_CACHE_SIZE   8
#define _TESTDOC1(typebase)   (Py_TYPE(obj) == &Py##typebase##_Type)
#define _TESTDOC2(typebase)   (Py_TYPE(obj) == Py##typebase##_TypePtr)
#define _ADDDOC(typebase, doc, name)

Functions

static int check_array_monotonic (const double *a, npy_int lena)
static void minmax (const npy_intp *data, npy_intp data_len, npy_intp *mn, npy_intp *mx)
NPY_NO_EXPORT PyObject * arr_bincount (PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwds)
NPY_NO_EXPORT PyObject * arr_digitize (PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwds)
NPY_NO_EXPORT PyObject * arr_insert (PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwdict)
static npy_intp binary_search_with_guess (const npy_double key, const npy_double *arr, npy_intp len, npy_intp guess)
NPY_NO_EXPORT PyObject * arr_interp (PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwdict)
NPY_NO_EXPORT PyObject * arr_interp_complex (PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwdict)
static int sequence_to_arrays (PyObject *seq, PyArrayObject **op, int count, char *paramname)
static int ravel_multi_index_loop (int ravel_ndim, npy_intp *ravel_dims, npy_intp *ravel_strides, npy_intp count, NPY_CLIPMODE *modes, char **coords, npy_intp *coords_strides)
NPY_NO_EXPORT PyObject * arr_ravel_multi_index (PyObject *self, PyObject *args, PyObject *kwds)
static int unravel_index_loop_corder (int unravel_ndim, npy_intp *unravel_dims, npy_intp unravel_size, npy_intp count, char *indices, npy_intp indices_stride, npy_intp *coords)
static int unravel_index_loop_forder (int unravel_ndim, npy_intp *unravel_dims, npy_intp unravel_size, npy_intp count, char *indices, npy_intp indices_stride, npy_intp *coords)
NPY_NO_EXPORT PyObject * arr_unravel_index (PyObject *self, PyObject *args, PyObject *kwds)
NPY_NO_EXPORT PyObject * arr_add_docstring (PyObject *NPY_UNUSED(dummy), PyObject *args)
static NPY_INLINE void pack_inner (const char *inptr, npy_intp element_size, npy_intp n_in, npy_intp in_stride, char *outptr, npy_intp n_out, npy_intp out_stride)
static PyObject * pack_bits (PyObject *input, int axis)
static PyObject * unpack_bits (PyObject *input, int axis)
NPY_NO_EXPORT PyObject * io_pack (PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwds)
NPY_NO_EXPORT PyObject * io_unpack (PyObject *NPY_UNUSED(self), PyObject *args, PyObject *kwds)

Define Documentation

#define _ADDDOC (   typebase,
  doc,
  name 
)
Value:
do {                               \
        Py##typebase##Object *new = (Py##typebase##Object *)obj;        \
        if (!(doc)) {                                                   \
            doc = docstr;                                               \
        }                                                               \
        else {                                                          \
            PyErr_Format(PyExc_RuntimeError, "%s method %s", name, msg); \
            return NULL;                                                \
        }                                                               \
    } while (0)
for npy_mul_with_overflow_intp
#define _TESTDOC1 (   typebase)    (Py_TYPE(obj) == &Py##typebase##_Type)
#define _TESTDOC2 (   typebase)    (Py_TYPE(obj) == Py##typebase##_TypePtr)
#define LIKELY_IN_CACHE_SIZE   8
#define NPY_NO_DEPRECATED_API   NPY_API_VERSION

Function Documentation

NPY_NO_EXPORT PyObject* arr_add_docstring ( PyObject *  NPY_UNUSEDdummy,
PyObject *  args 
)
Can only be called if doc is currently NULL
Don't add docstrings
Get "subdescr"

Referenced by test_interrupt().

NPY_NO_EXPORT PyObject* arr_bincount ( PyObject *  NPY_UNUSEDself,
PyObject *  args,
PyObject *  kwds 
)
arr_bincount is registered as bincount.
bincount accepts one, two or three arguments. The first is an array of non-negative integers The second, if present, is an array of weights, which must be promotable to double. Call these arguments list and weight. Both must be one-dimensional with len(weight) == len(list). If weight is not present then bincount(list)[i] is the number of occurrences of i in list. If weight is present then bincount(self,list, weight)[i] is the sum of all weight[j] where list [j] == i. Self is not used. The third argument, if present, is a minimum length desired for the output array.
handle empty list

Referenced by _PyArray_GetSigintBuf().

NPY_NO_EXPORT PyObject* arr_digitize ( PyObject *  NPY_UNUSEDself,
PyObject *  args,
PyObject *  kwds 
)
digitize(x, bins, right=False) returns an array of integers the same length as x. The values i returned are such that bins[i - 1] <= x < bins[i] if bins is monotonically increasing, or bins[i - 1] > x >= bins[i] if bins is monotonically decreasing. Beyond the bounds of bins, returns either i = 0 or i = len(bins) as appropriate. If right == True the comparison is bins [i - 1] < x <= bins[i] or bins [i - 1] >= x > bins[i]
PyArray_SearchSorted will make <cite>x</cite> contiguous even if we don't
TODO: <cite>bins</cite> could be strided, needs change to check_array_monotonic
PyArray_SearchSorted needs an increasing array
If bins is decreasing, ret has bins from end, not start

Referenced by _PyArray_GetSigintBuf().

NPY_NO_EXPORT PyObject* arr_insert ( PyObject *  NPY_UNUSEDself,
PyObject *  args,
PyObject *  kwdict 
)
Returns input array with values inserted sequentially into places indicated by the mask

<

zero if null array

Referenced by _PyArray_GetSigintBuf().

NPY_NO_EXPORT PyObject* arr_interp ( PyObject *  NPY_UNUSEDself,
PyObject *  args,
PyObject *  kwdict 
)
Get left and right fill values.
binary_search_with_guess needs at least a 3 item long array
only pre-calculate slopes if there are relatively few of them.

Referenced by _PyArray_GetSigintBuf().

NPY_NO_EXPORT PyObject* arr_interp_complex ( PyObject *  NPY_UNUSEDself,
PyObject *  args,
PyObject *  kwdict 
)
As for arr_interp but for complex fp values
Get left and right fill values.
binary_search_with_guess needs at least a 3 item long array
only pre-calculate slopes if there are relatively few of them.

Referenced by _PyArray_GetSigintBuf().

NPY_NO_EXPORT PyObject* arr_ravel_multi_index ( PyObject *  self,
PyObject *  args,
PyObject *  kwds 
)
ravel_multi_index implementation - see add_newdocs.py
Get the multi_index into op

Referenced by _PyArray_GetSigintBuf().

NPY_NO_EXPORT PyObject* arr_unravel_index ( PyObject *  self,
PyObject *  args,
PyObject *  kwds 
)
unravel_index implementation - see add_newdocs.py
Create the return array with a layout compatible with the indices and with a dimension added to the end for the multi-index
Remove the multi-index and inner loop
Now make a tuple of views, one per index

Referenced by test_interrupt().

static npy_intp binary_search_with_guess ( const npy_double  key,
const npy_double arr,
npy_intp  len,
npy_intp  guess 
) [static]
&#64;brief find index of a sorted array such that arr[i] <= key < arr[i + 1].
If an starting index guess is in-range, the array values around this index are first checked. This allows for repeated calls for well-ordered keys (a very common case) to use the previous index as a very good guess.
If the guess value is not useful, bisection of the array is used to find the index. If there is no such index, the return values are:

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

<blockquote> key < arr[0] -- -1 key == arr[len - 1] -- len - 1 key > arr[len - 1] -- len</blockquote>

System Message: WARNING/2 (<string>, line 12) Block quote ends without a blank line; unexpected unindent.
The array is assumed contiguous and sorted in ascending order.
&#64;param key key value. &#64;param arr contiguous sorted array to be searched. &#64;param len length of the array. &#64;param guess initial guess of index &#64;return index
Handle keys outside of the arr range first
If len <= 4 use linear search. From above we know key >= arr[0] when we start.
check most likely values: guess - 1, guess, guess + 1
last attempt to restrict search to items in cache
key >= arr[guess - 1]
key >= arr[guess]
key >= arr[guess + 1]
key >= arr[guess + 2]
last attempt to restrict search to items in cache
finally, find index by bisection
static int check_array_monotonic ( const double *  a,
npy_int  lena 
) [static]
Returns -1 if the array is monotonic decreasing, +1 if the array is monotonic increasing, and 0 if the array is not monotonic.
Skip repeated values at the beginning of the array
all bin edges hold the same value
Possibly monotonic increasing
last > next, possibly monotonic decreasing
NPY_NO_EXPORT PyObject* io_pack ( PyObject *  NPY_UNUSEDself,
PyObject *  args,
PyObject *  kwds 
)

Referenced by test_interrupt().

NPY_NO_EXPORT PyObject* io_unpack ( PyObject *  NPY_UNUSEDself,
PyObject *  args,
PyObject *  kwds 
)

Referenced by test_interrupt().

static void minmax ( const npy_intp data,
npy_intp  data_len,
npy_intp mn,
npy_intp mx 
) [static]
Find the minimum and maximum of an integer array

References NPY_INTP, PyArray_ContiguousFromAny, and PyArray_SIZE.

static PyObject* pack_bits ( PyObject *  input,
int  axis 
) [static]
Setup output shape
Divide axis dimension by 8 8 -> 1, 9 -> 2, 16 -> 2, 17 -> 3 etc..
Create output array
Setup iterators to iterate over all but given axis
static NPY_INLINE void pack_inner ( const char *  inptr,
npy_intp  element_size,
npy_intp  n_in,
npy_intp  in_stride,
char *  outptr,
npy_intp  n_out,
npy_intp  out_stride 
) [static]
This function packs boolean values in the input array into the bits of a byte array. Truth values are determined as usual: 0 is false, everything else is true.
Loop through the elements of inptr. Determine whether or not it is nonzero.

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

<blockquote> Yes: set corresponding bit (and adjust build value) No: move on</blockquote>

System Message: WARNING/2 (<string>, line 5) Block quote ends without a blank line; unexpected unindent.
Every 8th value, set the value of build and increment the outptr

<

uneven bits

<

assumes n_in > 0
Parameters:
element_size

in bytes

static int ravel_multi_index_loop ( int  ravel_ndim,
npy_intp ravel_dims,
npy_intp ravel_strides,
npy_intp  count,
NPY_CLIPMODE modes,
char **  coords,
npy_intp coords_strides 
) [static]
Inner loop for unravel_index
static int sequence_to_arrays ( PyObject *  seq,
PyArrayObject **  op,
int  count,
char *  paramname 
) [static]
Converts a Python sequence into 'count' PyArrayObjects
seq - Input Python object, usually a tuple but any sequence works. op - Where the arrays are placed. count - How many arrays there should be (errors if it doesn't match). paramname - The name of the parameter that produced 'seq'.
static PyObject* unpack_bits ( PyObject *  input,
int  axis 
) [static]
Handle 0-d array by converting it to a 1-d array
Setup output shape
Multiply axis dimension by 8
Create output array
Setup iterators to iterate over all but given axis
static int unravel_index_loop_corder ( int  unravel_ndim,
npy_intp unravel_dims,
npy_intp  unravel_size,
npy_intp  count,
char *  indices,
npy_intp  indices_stride,
npy_intp coords 
) [static]
C-order inner loop for unravel_index
static int unravel_index_loop_forder ( int  unravel_ndim,
npy_intp unravel_dims,
npy_intp  unravel_size,
npy_intp  count,
char *  indices,
npy_intp  indices_stride,
npy_intp coords 
) [static]
Fortran-order inner loop for unravel_index