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 "npy_pycompat.h"
#include "multiarraymodule.h"
#include "common.h"
#include "ctors.h"
#include "convert_datatype.h"
#include "shape.h"
#include "buffer.h"
#include "numpymemoryview.h"
#include "lowlevel_strided_loops.h"
#include "methods.h"
#include "_datetime.h"
#include "datetime_strings.h"
#include "array_assign.h"
#include "mapping.h"
#include "templ_common.h"
#include "alloc.h"
#include <assert.h>
Defines | |
#define | PY_SSIZE_T_CLEAN |
#define | NPY_NO_DEPRECATED_API NPY_API_VERSION |
#define | _MULTIARRAYMODULE |
#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 (const 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) |
static int | setArrayFromSequence (PyArrayObject *a, PyObject *s, int dim, PyArrayObject *dst) |
NPY_NO_EXPORT int | PyArray_AssignFromSequence (PyArrayObject *self, PyObject *v) |
static int | discover_itemsize (PyObject *s, int nd, int *itemsize, int string_type) |
static int | discover_dimensions (PyObject *obj, 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_int (PyTypeObject *subtype, PyArray_Descr *descr, int nd, npy_intp *dims, npy_intp *strides, void *data, int flags, PyObject *obj, int zeroed, int allow_emptystring) |
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 int | _is_default_descr (PyObject *descr, PyObject *typestr) |
NPY_NO_EXPORT PyObject * | PyArray_FromInterface (PyObject *origin) |
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_CopyAsFlat (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 int | PyArray_MoveInto (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 is_f_order) |
NPY_NO_EXPORT PyObject * | PyArray_Empty (int nd, npy_intp *dims, PyArray_Descr *type, int is_f_order) |
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 void | _array_fill_strides (npy_intp *strides, npy_intp *dims, int nd, size_t itemsize, int inflag, int *objflags) |
NPY_NO_EXPORT PyArrayObject * | PyArray_SubclassWrap (PyArrayObject *arr_of_subclass, PyArrayObject *towrap) |
#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 |
Referenced by PyArray_Zeros().
#define NPY_NO_DEPRECATED_API NPY_API_VERSION |
#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 void _array_fill_strides | ( | npy_intp * | strides, |
npy_intp * | dims, | ||
int | nd, | ||
size_t | itemsize, | ||
int | inflag, | ||
int * | objflags | ||
) |
<
<
<
Referenced by array_setstate().
NPY_NO_EXPORT int _array_from_buffer_3118 | ( | PyObject * | obj, |
PyObject ** | out | ||
) |
static npy_intp _calc_length | ( | PyObject * | start, |
PyObject * | stop, | ||
PyObject * | step, | ||
PyObject ** | next, | ||
int | cmplx | ||
) | [static] |
NPY_NO_EXPORT int _is_default_descr | ( | PyObject * | descr, |
PyObject * | typestr | ||
) |
References NPY_DEFAULT_TYPE, PyArray_DescrFromType(), and PyArray_DTypeFromObject().
static int _safe_ceil_to_intp | ( | double | value, |
npy_intp * | ret | ||
) | [static] |
NPY_NO_EXPORT void _strided_byte_swap | ( | void * | p, |
npy_intp | stride, | ||
npy_intp | n, | ||
int | size | ||
) |
NPY_NO_EXPORT void _unaligned_strided_byte_copy | ( | char * | dst, |
npy_intp | outstrides, | ||
char * | src, | ||
npy_intp | instrides, | ||
npy_intp | N, | ||
int | elsize | ||
) |
References npy_bswap4().
Referenced by _scan(), LONGDOUBLE_scan(), and to().
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] |
Referenced by PyArray_Arange().
static PyArrayObject* array_fromfile_binary | ( | FILE * | fp, |
PyArray_Descr * | dtype, | ||
npy_intp | num, | ||
size_t * | nread | ||
) | [static] |
Referenced by PyArray_Arange().
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 | ||
) |
static int discover_dimensions | ( | PyObject * | obj, |
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, | ||
int | string_type | ||
) | [static] |
References PyArray_Check, PyArray_DIM, PyArray_IsScalar, and PyArray_NDIM.
static int fromfile_next_element | ( | FILE ** | fp, |
void * | dptr, | ||
PyArray_Descr * | dtype, | ||
void * | NPY_UNUSEDstream_data | ||
) | [static] |
Referenced by PyArray_Arange().
static int fromfile_skip_separator | ( | FILE ** | fp, |
const char * | sep, | ||
void * | NPY_UNUSEDstream_data | ||
) | [static] |
Referenced by PyArray_Arange().
static int fromstr_next_element | ( | char ** | s, |
void * | dptr, | ||
PyArray_Descr * | dtype, | ||
const char * | end | ||
) | [static] |
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 | ||
) |
References array_from_text(), array_fromfile_binary(), _PyArray_Descr::elsize, _PyArray_Descr::f, fromfile_next_element(), fromfile_skip_separator(), PyArray_DATA, PyArray_DESCR, PyArray_DIMS, PyArray_MAX, PyArray_NewFromDescr_int(), PyDataMem_RENEW(), PyDataType_REFCHK, and PyArray_ArrFuncs::scanfunc.
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 | ||
) |
NPY_NO_EXPORT PyObject* PyArray_CheckAxis | ( | PyArrayObject * | arr, |
int * | axis, | ||
int | flags | ||
) |
Referenced by PyArray_ArgMax(), PyArray_Max(), PyArray_Mean(), PyArray_Min(), PyArray_Prod(), PyArray_Sum(), and PyArray_TakeFrom().
NPY_NO_EXPORT PyObject* PyArray_CheckFromAny | ( | PyObject * | op, |
PyArray_Descr * | descr, | ||
int | min_depth, | ||
int | max_depth, | ||
int | requires, | ||
PyObject * | context | ||
) |
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.
NPY_NO_EXPORT int PyArray_CopyAsFlat | ( | PyArrayObject * | dst, |
PyArrayObject * | src, | ||
NPY_ORDER | order | ||
) |
NPY_NO_EXPORT int PyArray_CopyInto | ( | PyArrayObject * | dst, |
PyArrayObject * | src | ||
) |
Broadcast to the destination shape if necessary.
Returns 0 on success, -1 on failure.
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 | is_f_order | ||
) |
References PyArray_DATA, and PyDataMem_RENEW().
NPY_NO_EXPORT PyObject* PyArray_EnsureAnyArray | ( | PyObject * | op | ) |
NPY_NO_EXPORT PyObject* PyArray_EnsureArray | ( | PyObject * | op | ) |
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;
Referenced by _array_fromobject(), _pyarray_correlate(), array_imag_get(), PyArray_CanCoerceScalar(), PyArray_CompareLists(), and PyUFunc_FromFuncAndDataAndSignature().
NPY_NO_EXPORT PyObject* PyArray_FromArray | ( | PyArrayObject * | arr, |
PyArray_Descr * | newtype, | ||
int | flags | ||
) |
Referenced by PyArray_ArgMax(), 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 | ||
) |
References PyArray_FillObjectArray().
NPY_NO_EXPORT PyObject* PyArray_FromDimsAndDataAndDescr | ( | int | nd, |
int * | d, | ||
PyArray_Descr * | descr, | ||
char * | data | ||
) |
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 and if dtype->subarray is true dtype will be decrefed even on success.
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.
References PyArray_Check.
NPY_NO_EXPORT PyObject* PyArray_FromInterface | ( | PyObject * | origin | ) |
NPY_NO_EXPORT PyObject* PyArray_FromIter | ( | PyObject * | obj, |
PyArray_Descr * | dtype, | ||
npy_intp | count | ||
) |
Referenced by array_scalar().
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 | ) |
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, is_f_order ? NPY_ARRAY_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>
NPY_NO_EXPORT int PyArray_MoveInto | ( | PyArrayObject * | dst, |
PyArrayObject * | src | ||
) |
References PyArray_Byteswap(), and PyArray_DESCR.
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 PyObject* PyArray_NewFromDescr | ( | PyTypeObject * | subtype, |
PyArray_Descr * | descr, | ||
int | nd, | ||
npy_intp * | dims, | ||
npy_intp * | strides, | ||
void * | data, | ||
int | flags, | ||
PyObject * | obj | ||
) |
Referenced by allocate_reduce_result(), NpyIter_GetShape(), prepare_ufunc_output(), PyArray_CastScalarDirect(), PyArray_TakeFrom(), and PyArray_Zeros().
NPY_NO_EXPORT PyObject* PyArray_NewFromDescr_int | ( | PyTypeObject * | subtype, |
PyArray_Descr * | descr, | ||
int | nd, | ||
npy_intp * | dims, | ||
npy_intp * | strides, | ||
void * | data, | ||
int | flags, | ||
PyObject * | obj, | ||
int | zeroed, | ||
int | allow_emptystring | ||
) |
<
Referenced by PyArray_Arange().
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. On failure or when dtype->subarray is true, dtype will be decrefed.
References d().
NPY_NO_EXPORT PyArrayObject* PyArray_SubclassWrap | ( | PyArrayObject * | arr_of_subclass, |
PyArrayObject * | towrap | ||
) |
NPY_NO_EXPORT PyObject* PyArray_Zeros | ( | int | nd, |
npy_intp * | dims, | ||
PyArray_Descr * | type, | ||
int | is_f_order | ||
) |
References _PyArray_Descr::elsize, FROM_BUFFER_SIZE, NPY_BEGIN_ALLOW_THREADS, PyArray_DATA, PyArray_NewFromDescr(), and swab_separator().
static int setArrayFromSequence | ( | PyArrayObject * | a, |
PyObject * | s, | ||
int | dim, | ||
PyArrayObject * | dst | ||
) | [static] |
References PyArray_DIMS.
static char* swab_separator | ( | const char * | sep | ) | [static] |
Referenced by PyArray_Zeros().