numpy 2.0.0
|
#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 "shape.h"
#include "buffer.h"
#include "numpymemoryview.h"
#include "lowlevel_strided_loops.h"
Defines | |
#define | PY_SSIZE_T_CLEAN |
#define | _MULTIARRAYMODULE |
#define | NPY_NO_PREFIX |
#define | _COPY_N_SIZE(size) |
#define | PyIntOrLong_Check(obj) (PyInt_Check(obj) || PyLong_Check(obj)) |
#define | FROM_BUFFER_SIZE 4096 |
Typedefs | |
typedef int(* | next_element )(void **, void *, PyArray_Descr *, void *) |
typedef int(* | skip_separator )(void **, const char *, void *) |
Functions | |
static int | fromstr_next_element (char **s, void *dptr, PyArray_Descr *dtype, const char *end) |
static int | fromfile_next_element (FILE **fp, void *dptr, PyArray_Descr *dtype, void *NPY_UNUSED(stream_data)) |
static char * | swab_separator (char *sep) |
static int | fromstr_skip_separator (char **s, const char *sep, const char *end) |
static int | fromfile_skip_separator (FILE **fp, const char *sep, void *NPY_UNUSED(stream_data)) |
static int | _update_descr_and_dimensions (PyArray_Descr **des, npy_intp *newdims, npy_intp *newstrides, int oldnd) |
NPY_NO_EXPORT void | _unaligned_strided_byte_copy (char *dst, npy_intp outstrides, char *src, npy_intp instrides, npy_intp N, int elsize) |
NPY_NO_EXPORT void | _strided_byte_swap (void *p, npy_intp stride, npy_intp n, int size) |
NPY_NO_EXPORT void | byte_swap_vector (void *p, npy_intp n, int size) |
NPY_NO_EXPORT void | copy_and_swap (void *dst, void *src, int itemsize, npy_intp numitems, npy_intp srcstrides, int swap) |
NPY_NO_EXPORT void | _get_array_memory_extents (PyArrayObject *arr, npy_uintp *out_start, npy_uintp *out_end) |
NPY_NO_EXPORT int | _arrays_overlap (PyArrayObject *arr1, PyArrayObject *arr2) |
NPY_NO_EXPORT int | PyArray_MoveInto (PyArrayObject *dst, PyArrayObject *src) |
static int | setArrayFromSequence (PyArrayObject *a, PyObject *s, int dim, npy_intp offset) |
NPY_NO_EXPORT int | PyArray_AssignFromSequence (PyArrayObject *self, PyObject *v) |
static int | discover_itemsize (PyObject *s, int nd, int *itemsize) |
static int | discover_dimensions (PyObject *s, int *maxndim, npy_intp *d, int check_it, int stop_at_string, int stop_at_tuple, int *out_is_object) |
NPY_NO_EXPORT PyObject * | PyArray_NewFromDescr (PyTypeObject *subtype, PyArray_Descr *descr, int nd, npy_intp *dims, npy_intp *strides, void *data, int flags, PyObject *obj) |
NPY_NO_EXPORT PyObject * | PyArray_NewLikeArray (PyArrayObject *prototype, NPY_ORDER order, PyArray_Descr *dtype, int subok) |
NPY_NO_EXPORT PyObject * | PyArray_New (PyTypeObject *subtype, int nd, npy_intp *dims, int type_num, npy_intp *strides, void *data, int itemsize, int flags, PyObject *obj) |
NPY_NO_EXPORT int | _array_from_buffer_3118 (PyObject *obj, PyObject **out) |
NPY_NO_EXPORT int | PyArray_GetArrayParamsFromObject (PyObject *op, PyArray_Descr *requested_dtype, npy_bool writeable, PyArray_Descr **out_dtype, int *out_ndim, npy_intp *out_dims, PyArrayObject **out_arr, PyObject *context) |
NPY_NO_EXPORT PyObject * | PyArray_FromAny (PyObject *op, PyArray_Descr *newtype, int min_depth, int max_depth, int flags, PyObject *context) |
NPY_NO_EXPORT PyObject * | PyArray_CheckFromAny (PyObject *op, PyArray_Descr *descr, int min_depth, int max_depth, int requires, PyObject *context) |
NPY_NO_EXPORT PyObject * | PyArray_FromArray (PyArrayObject *arr, PyArray_Descr *newtype, int flags) |
NPY_NO_EXPORT PyObject * | PyArray_FromStructInterface (PyObject *input) |
NPY_NO_EXPORT PyObject * | PyArray_FromInterface (PyObject *input) |
NPY_NO_EXPORT PyObject * | PyArray_FromArrayAttr (PyObject *op, PyArray_Descr *typecode, PyObject *context) |
NPY_NO_EXPORT PyArray_Descr * | PyArray_DescrFromObject (PyObject *op, PyArray_Descr *mintype) |
NPY_NO_EXPORT PyObject * | PyArray_FromDimsAndDataAndDescr (int nd, int *d, PyArray_Descr *descr, char *data) |
NPY_NO_EXPORT PyObject * | PyArray_FromDims (int nd, int *d, int type) |
NPY_NO_EXPORT PyObject * | PyArray_EnsureArray (PyObject *op) |
NPY_NO_EXPORT PyObject * | PyArray_EnsureAnyArray (PyObject *op) |
NPY_NO_EXPORT int | PyArray_CopyAnyIntoOrdered (PyArrayObject *dst, PyArrayObject *src, NPY_ORDER order) |
NPY_NO_EXPORT int | PyArray_CopyAnyInto (PyArrayObject *dst, PyArrayObject *src) |
NPY_NO_EXPORT int | PyArray_CopyInto (PyArrayObject *dst, PyArrayObject *src) |
NPY_NO_EXPORT PyObject * | PyArray_CheckAxis (PyArrayObject *arr, int *axis, int flags) |
NPY_NO_EXPORT PyObject * | PyArray_Zeros (int nd, npy_intp *dims, PyArray_Descr *type, int fortran) |
NPY_NO_EXPORT PyObject * | PyArray_Empty (int nd, npy_intp *dims, PyArray_Descr *type, int fortran) |
static int | _safe_ceil_to_intp (double value, npy_intp *ret) |
NPY_NO_EXPORT PyObject * | PyArray_Arange (double start, double stop, double step, int type_num) |
static npy_intp | _calc_length (PyObject *start, PyObject *stop, PyObject *step, PyObject **next, int cmplx) |
NPY_NO_EXPORT PyObject * | PyArray_ArangeObj (PyObject *start, PyObject *stop, PyObject *step, PyArray_Descr *dtype) |
static PyArrayObject * | array_fromfile_binary (FILE *fp, PyArray_Descr *dtype, npy_intp num, size_t *nread) |
static PyArrayObject * | array_from_text (PyArray_Descr *dtype, npy_intp num, char *sep, size_t *nread, void *stream, next_element next, skip_separator skip_sep, void *stream_data) |
NPY_NO_EXPORT PyObject * | PyArray_FromFile (FILE *fp, PyArray_Descr *dtype, npy_intp num, char *sep) |
NPY_NO_EXPORT PyObject * | PyArray_FromBuffer (PyObject *buf, PyArray_Descr *type, npy_intp count, npy_intp offset) |
NPY_NO_EXPORT PyObject * | PyArray_FromString (char *data, npy_intp slen, PyArray_Descr *dtype, npy_intp num, char *sep) |
NPY_NO_EXPORT PyObject * | PyArray_FromIter (PyObject *obj, PyArray_Descr *dtype, npy_intp count) |
NPY_NO_EXPORT size_t | _array_fill_strides (npy_intp *strides, npy_intp *dims, int nd, size_t itemsize, int inflag, int *objflags) |
#define _COPY_N_SIZE | ( | size | ) |
for(i=0; i<N; i++) { \ memcpy(tout, tin, size); \ tin += instrides; \ tout += outstrides; \ } \ return
#define _MULTIARRAYMODULE |
#define FROM_BUFFER_SIZE 4096 |
#define NPY_NO_PREFIX |
#define PY_SSIZE_T_CLEAN |
#define PyIntOrLong_Check | ( | obj | ) | (PyInt_Check(obj) || PyLong_Check(obj)) |
typedef int(* next_element)(void **, void *, PyArray_Descr *, void *) |
typedef int(* skip_separator)(void **, const char *, void *) |
NPY_NO_EXPORT size_t _array_fill_strides | ( | npy_intp * | strides, |
npy_intp * | dims, | ||
int | nd, | ||
size_t | itemsize, | ||
int | inflag, | ||
int * | objflags | ||
) |
NPY_NO_EXPORT int _array_from_buffer_3118 | ( | PyObject * | obj, |
PyObject ** | out | ||
) |
NPY_NO_EXPORT int _arrays_overlap | ( | PyArrayObject * | arr1, |
PyArrayObject * | arr2 | ||
) |
static npy_intp _calc_length | ( | PyObject * | start, |
PyObject * | stop, | ||
PyObject * | step, | ||
PyObject ** | next, | ||
int | cmplx | ||
) | [static] |
NPY_NO_EXPORT void _get_array_memory_extents | ( | PyArrayObject * | arr, |
npy_uintp * | out_start, | ||
npy_uintp * | out_end | ||
) |
static int _safe_ceil_to_intp | ( | double | value, |
npy_intp * | ret | ||
) | [static] |
NPY_NO_EXPORT void _unaligned_strided_byte_copy | ( | char * | dst, |
npy_intp | outstrides, | ||
char * | src, | ||
npy_intp | instrides, | ||
npy_intp | N, | ||
int | elsize | ||
) |
Referenced by _new_sort().
static int _update_descr_and_dimensions | ( | PyArray_Descr ** | des, |
npy_intp * | newdims, | ||
npy_intp * | newstrides, | ||
int | oldnd | ||
) | [static] |
static PyArrayObject* array_from_text | ( | PyArray_Descr * | dtype, |
npy_intp | num, | ||
char * | sep, | ||
size_t * | nread, | ||
void * | stream, | ||
next_element | next, | ||
skip_separator | skip_sep, | ||
void * | stream_data | ||
) | [static] |
static PyArrayObject* array_fromfile_binary | ( | FILE * | fp, |
PyArray_Descr * | dtype, | ||
npy_intp | num, | ||
size_t * | nread | ||
) | [static] |
NPY_NO_EXPORT void byte_swap_vector | ( | void * | p, |
npy_intp | n, | ||
int | size | ||
) |
References PyArray_DATA, PyArray_DIMS, PyArray_NDIM, and PyArray_STRIDES.
Referenced by gentype_interface_get(), and LONGDOUBLE_setitem().
static int discover_dimensions | ( | PyObject * | s, |
int * | maxndim, | ||
npy_intp * | d, | ||
int | check_it, | ||
int | stop_at_string, | ||
int | stop_at_tuple, | ||
int * | out_is_object | ||
) | [static] |
static int discover_itemsize | ( | PyObject * | s, |
int | nd, | ||
int * | itemsize | ||
) | [static] |
References PyArray_DIM.
static int fromfile_next_element | ( | FILE ** | fp, |
void * | dptr, | ||
PyArray_Descr * | dtype, | ||
void * | NPY_UNUSEDstream_data | ||
) | [static] |
References _PyArray_Descr::f, and PyArray_ArrFuncs::scanfunc.
static int fromfile_skip_separator | ( | FILE ** | fp, |
const char * | sep, | ||
void * | NPY_UNUSEDstream_data | ||
) | [static] |
static int fromstr_next_element | ( | char ** | s, |
void * | dptr, | ||
PyArray_Descr * | dtype, | ||
const char * | end | ||
) | [static] |
References _PyArray_Descr::f, and PyArray_ArrFuncs::fromstr.
static int fromstr_skip_separator | ( | char ** | s, |
const char * | sep, | ||
const char * | end | ||
) | [static] |
NPY_NO_EXPORT PyObject* PyArray_Arange | ( | double | start, |
double | stop, | ||
double | step, | ||
int | type_num | ||
) |
NPY_NO_EXPORT PyObject* PyArray_ArangeObj | ( | PyObject * | start, |
PyObject * | stop, | ||
PyObject * | step, | ||
PyArray_Descr * | dtype | ||
) |
<
NPY_NO_EXPORT int PyArray_AssignFromSequence | ( | PyArrayObject * | self, |
PyObject * | v | ||
) |
References PyArray_Check, and PyArray_NDIM.
Referenced by PyArray_GetArrayParamsFromObject().
NPY_NO_EXPORT PyObject* PyArray_CheckAxis | ( | PyArrayObject * | arr, |
int * | axis, | ||
int | flags | ||
) |
NPY_NO_EXPORT PyObject* PyArray_CheckFromAny | ( | PyObject * | op, |
PyArray_Descr * | descr, | ||
int | min_depth, | ||
int | max_depth, | ||
int | requires, | ||
PyObject * | context | ||
) |
Referenced by PyArray_CopyAnyIntoOrdered().
NPY_NO_EXPORT int PyArray_CopyAnyInto | ( | PyArrayObject * | dst, |
PyArrayObject * | src | ||
) |
Does not require src and dest to have "broadcastable" shapes (only the same number of elements).
Returns 0 on success, -1 on error.
Referenced by PyArray_CastTo().
NPY_NO_EXPORT int PyArray_CopyAnyIntoOrdered | ( | PyArrayObject * | dst, |
PyArrayObject * | src, | ||
NPY_ORDER | order | ||
) |
References MAX_DIMS, PyArrayObject::nd, PyArray_CheckFromAny(), PyArray_NDIM, and PyArray_Ravel().
NPY_NO_EXPORT int PyArray_CopyInto | ( | PyArrayObject * | dst, |
PyArrayObject * | src | ||
) |
Broadcast to the destination shape if necessary.
Returns 0 on success, -1 on failure.
Referenced by PyArray_CastToType(), and PyArray_FromAny().
NPY_NO_EXPORT PyArray_Descr* PyArray_DescrFromObject | ( | PyObject * | op, |
PyArray_Descr * | mintype | ||
) |
NPY_NO_EXPORT PyObject* PyArray_Empty | ( | int | nd, |
npy_intp * | dims, | ||
PyArray_Descr * | type, | ||
int | fortran | ||
) |
NPY_NO_EXPORT PyObject* PyArray_EnsureAnyArray | ( | PyObject * | op | ) |
Referenced by _strings_richcompare().
NPY_NO_EXPORT PyObject* PyArray_EnsureArray | ( | PyObject * | op | ) |
References NpyIter_Deallocate().
NPY_NO_EXPORT PyObject* PyArray_FromAny | ( | PyObject * | op, |
PyArray_Descr * | newtype, | ||
int | min_depth, | ||
int | max_depth, | ||
int | flags, | ||
PyObject * | context | ||
) |
<blockquote class="first"> !PyArray_CanCastTo(dtype, newtype)) {</blockquote>
Py_DECREF(dtype); Py_XDECREF(newtype); PyErr_SetString(PyExc_TypeError,
<blockquote> "object cannot be safely cast to array " "of required type");</blockquote>
return NULL;
References PyArrayObject::base, PyArrayObject::data, PyArrayObject::descr, PyArrayObject::dimensions, PyArrayObject::flags, PyArrayObject::nd, NPY_ALIGNED, NPY_C_CONTIGUOUS, NPY_ENSUREARRAY, NPY_ENSURECOPY, NPY_F_CONTIGUOUS, NPY_UPDATEIFCOPY, NPY_WRITEABLE, PyArray_CheckExact, PyArray_CopyInto(), PyArray_FLAGS, PyArray_ISWRITEABLE, PyArray_NewFromDescr(), PyArray_Type, and PyArrayObject::strides.
Referenced by array_empty_like(), array_subscript_nice(), einsum_sub_op_from_str(), new_array_for_sum(), PyArray_CompareLists(), and PyArray_FromInterface().
NPY_NO_EXPORT PyObject* PyArray_FromArray | ( | PyArrayObject * | arr, |
PyArray_Descr * | newtype, | ||
int | flags | ||
) |
Referenced by PyArray_GetArrayParamsFromObject(), and PyArray_TakeFrom().
NPY_NO_EXPORT PyObject* PyArray_FromArrayAttr | ( | PyObject * | op, |
PyArray_Descr * | typecode, | ||
PyObject * | context | ||
) |
NPY_NO_EXPORT PyObject* PyArray_FromBuffer | ( | PyObject * | buf, |
PyArray_Descr * | type, | ||
npy_intp | count, | ||
npy_intp | offset | ||
) |
NPY_NO_EXPORT PyObject* PyArray_FromDims | ( | int | nd, |
int * | d, | ||
int | type | ||
) |
NPY_NO_EXPORT PyObject* PyArray_FromDimsAndDataAndDescr | ( | int | nd, |
int * | d, | ||
PyArray_Descr * | descr, | ||
char * | data | ||
) |
References NPY_BEGIN_THREADS.
NPY_NO_EXPORT PyObject* PyArray_FromFile | ( | FILE * | fp, |
PyArray_Descr * | dtype, | ||
npy_intp | num, | ||
char * | sep | ||
) |
FILE *
pointer fp
, and a PyArray_Descr
, return an array corresponding to the data encoded in that file.
If the dtype is NULL, the default array type is used (double). If non-null, the reference is stolen.
The number of elements to read is given as num
; if it is < 0, then then as many as possible are read.
If sep
is NULL or empty, then binary data is assumed, else text data, with sep
as the separator between elements. Whitespace in the separator matches any length of whitespace in the text, and a match for whitespace around the separator is added.
For memory-mapped files, use the buffer interface. No more data than necessary is read by this routine.
NPY_NO_EXPORT PyObject* PyArray_FromInterface | ( | PyObject * | input | ) |
References PyArray_Check, PyArray_CheckExact, PyArray_FromAny(), PyArray_FromScalar(), PyArray_IsScalar, and PyArray_View().
NPY_NO_EXPORT PyObject* PyArray_FromIter | ( | PyObject * | obj, |
PyArray_Descr * | dtype, | ||
npy_intp | count | ||
) |
Referenced by array_zeros().
NPY_NO_EXPORT PyObject* PyArray_FromString | ( | char * | data, |
npy_intp | slen, | ||
PyArray_Descr * | dtype, | ||
npy_intp | num, | ||
char * | sep | ||
) |
data
, a string length slen
, and a PyArray_Descr
, return an array corresponding to the data encoded in that string.
If the dtype is NULL, the default array type is used (double). If non-null, the reference is stolen.
If slen
is < 0, then the end of string is used for text data. It is an error for slen
to be < 0 for binary data (since embedded NULLs would be the norm).
The number of elements to read is given as num
; if it is < 0, then then as many as possible are read.
If sep
is NULL or empty, then binary data is assumed, else text data, with sep
as the separator between elements. Whitespace in the separator matches any length of whitespace in the text, and a match for whitespace around the separator is added.
NPY_NO_EXPORT PyObject* PyArray_FromStructInterface | ( | PyObject * | input | ) |
References _PyArray_Descr::byteorder, CARRAY, DEPRECATE, MAX_DIMS, PyArray_ISNBO, and PyArray_NewFromDescr().
NPY_NO_EXPORT int PyArray_GetArrayParamsFromObject | ( | PyObject * | op, |
PyArray_Descr * | requested_dtype, | ||
npy_bool | writeable, | ||
PyArray_Descr ** | out_dtype, | ||
int * | out_ndim, | ||
npy_intp * | out_dims, | ||
PyArrayObject ** | out_arr, | ||
PyObject * | context | ||
) |
PyObject* to a NumPy array. This allows the "innate type and shape" of Python list-of-lists to be discovered without actually converting to an array.
In some cases, such as structured arrays and the __array__ interface, a data type needs to be used to make sense of the object. When this is needed, provide a Descr for 'requested_dtype', otherwise provide NULL. This reference is not stolen. Also, if the requested dtype doesn't modify the interpretation of the input, out_dtype will still get the "innate" dtype of the object, not the dtype passed in 'requested_dtype'.
If writing to the value in 'op' is desired, set the boolean 'writeable' to 1. This raises an error when 'op' is a scalar, list of lists, or other non-writeable 'op'.
Typical usage: <blockquote class="last">
PyArrayObject *arr = NULL; PyArray_Descr *dtype = NULL; int ndim = 0; npy_intp dims[NPY_MAXDIMS];
<blockquote class="first"> &ndim, &dims, &arr, NULL) < 0) {</blockquote>
return NULL;
} if (arr == NULL) {
<blockquote>
... validate/change dtype, validate flags, ndim, etc ... Could make custom strides here too arr = PyArray_NewFromDescr(&PyArray_Type, dtype, ndim,
<blockquote> dims, NULL, fortran ? NPY_F_CONTIGUOUS : 0, NULL);</blockquote>
} if (PyArray_CopyObject(arr, op) < 0) {
<blockquote> Py_DECREF(arr); return NULL;</blockquote>
} </blockquote>
} else {
<blockquote>
</blockquote>
} ... use arr ... </blockquote>
<blockquote> usage requires this behave differently, this should be changed!</blockquote>
References _PyArray_Descr::elsize, NPY_F_CONTIGUOUS, NPY_MAXDIMS, NPY_STRING, NPY_UNICODE, NPY_UPDATEIFCOPY, NPY_VOID, PyArray_AssignFromSequence(), PyArray_DATA, PyArray_DESCR, PyArray_DESCR_REPLACE, PyArray_FromArray(), PyArray_FromScalar(), PyArray_IsScalar, PyArray_NDIM, PyArray_NewFromDescr(), and _PyArray_Descr::type_num.
NPY_NO_EXPORT int PyArray_MoveInto | ( | PyArrayObject * | dst, |
PyArrayObject * | src | ||
) |
NPY_NO_EXPORT PyObject* PyArray_New | ( | PyTypeObject * | subtype, |
int | nd, | ||
npy_intp * | dims, | ||
int | type_num, | ||
npy_intp * | strides, | ||
void * | data, | ||
int | itemsize, | ||
int | flags, | ||
PyObject * | obj | ||
) |
References PyArray_Check, and PyArray_ISWRITEABLE.
Referenced by array_subscript_nice(), and PyArray_SearchSorted().
NPY_NO_EXPORT PyObject* PyArray_NewFromDescr | ( | PyTypeObject * | subtype, |
PyArray_Descr * | descr, | ||
int | nd, | ||
npy_intp * | dims, | ||
npy_intp * | strides, | ||
void * | data, | ||
int | flags, | ||
PyObject * | obj | ||
) |
<
Referenced by array_astype(), array_real_get(), array_subscript(), array_swapaxes(), array_wraparray(), PyArray_CastScalarDirect(), PyArray_CastToType(), PyArray_FromAny(), PyArray_FromStructInterface(), PyArray_GetArrayParamsFromObject(), and PyArray_TakeFrom().
NPY_NO_EXPORT PyObject* PyArray_NewLikeArray | ( | PyArrayObject * | prototype, |
NPY_ORDER | order, | ||
PyArray_Descr * | dtype, | ||
int | subok | ||
) |
with possible memory layout order and data type changes.
prototype - The array the new one should be like. order - NPY_CORDER - C-contiguous result.
<blockquote> NPY_FORTRANORDER - Fortran-contiguous result. NPY_ANYORDER - Fortran if prototype is Fortran, C otherwise. NPY_KEEPORDER - Keeps the axis ordering of prototype.</blockquote>
dtype - If not NULL, overrides the data type of the result. subok - If 1, use the prototype's array subtype, otherwise
<blockquote> always create a base-class array.</blockquote>
NOTE: If dtype is not NULL, steals the dtype reference.
NPY_NO_EXPORT PyObject* PyArray_Zeros | ( | int | nd, |
npy_intp * | dims, | ||
PyArray_Descr * | type, | ||
int | fortran | ||
) |
static int setArrayFromSequence | ( | PyArrayObject * | a, |
PyObject * | s, | ||
int | dim, | ||
npy_intp | offset | ||
) | [static] |
static char* swab_separator | ( | char * | sep | ) | [static] |