numpy 2.0.0
src/multiarray/ctors.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 "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"

Defines

#define PY_SSIZE_T_CLEAN
#define NPY_NO_DEPRECATED_API
#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)
NPY_NO_EXPORT int PyArray_MaskedMoveInto (PyArrayObject *dst, PyArrayObject *src, PyArrayObject *mask, NPY_CASTING casting)
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_DescrPyArray_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 int PyArray_MaskedCopyInto (PyArrayObject *dst, PyArrayObject *src, PyArrayObject *mask, NPY_CASTING casting)
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 PyArrayObjectarray_fromfile_binary (FILE *fp, PyArray_Descr *dtype, npy_intp num, size_t *nread)
static PyArrayObjectarray_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 Documentation

#define _COPY_N_SIZE (   size)
Value:
for(i=0; i<N; i++) {                       \
        memcpy(tout, tin, size);                \
        tin += instrides;                       \
        tout += outstrides;                     \
    }                                           \
    return
#define _MULTIARRAYMODULE
#define FROM_BUFFER_SIZE   4096
Create an array by reading from the given stream, using the passed next_element and skip_separator functions.

Referenced by PyArray_Zeros().

#define NPY_NO_DEPRECATED_API
#define NPY_NO_PREFIX
#define PY_SSIZE_T_CLEAN
#define PyIntOrLong_Check (   obj)    (PyInt_Check(obj) || PyLong_Check(obj))

Typedef Documentation

typedef int(* next_element)(void **, void *, PyArray_Descr *, void *)
Reading from a file or a string.
As much as possible, we try to use the same code for both files and strings, so the semantics for fromstring and fromfile are the same, especially with regards to the handling of text representations.
typedef int(* skip_separator)(void **, const char *, void *)

Function Documentation

NPY_NO_EXPORT size_t _array_fill_strides ( npy_intp strides,
npy_intp dims,
int  nd,
size_t  itemsize,
int  inflag,
int *  objflags 
)
This is the main array creation routine.
Flags argument has multiple related meanings depending on data and strides:
If data is given, then flags is flags associated with data. If strides is not given, then a contiguous strides array will be created and the NPY_ARRAY_C_CONTIGUOUS bit will be set. If the flags argument has the NPY_ARRAY_F_CONTIGUOUS bit set, then a FORTRAN-style strides array will be created (and of course the NPY_ARRAY_F_CONTIGUOUS flag bit will be set).
If data is not given but created here, then flags will be NPY_ARRAY_DEFAULT and a non-zero flags argument can be used to indicate a FORTRAN style array is desired.

Only make Fortran strides if not contiguous as well

NPY_NO_EXPORT int _array_from_buffer_3118 ( PyObject *  obj,
PyObject **  out 
)
NPY_NO_EXPORT int _arrays_overlap ( PyArrayObject arr1,
PyArrayObject arr2 
)
Returns 1 if the arrays have overlapping data, 0 otherwise
static npy_intp _calc_length ( PyObject *  start,
PyObject *  stop,
PyObject *  step,
PyObject **  next,
int  cmplx 
) [static]
the formula is len = (intp) ceil((start - stop) / step);

References _PyArray_Descr::elsize, INTP_FMT, Py_TYPE, and PyDataType_REFCHK.

NPY_NO_EXPORT void _get_array_memory_extents ( PyArrayObject arr,
npy_uintp out_start,
npy_uintp out_end 
)
Gets a half-open range [start, end) which contains the array data

Calculate with a closed range [start, end]
If the array size is zero, return an empty range
Expand either upwards or downwards depending on stride
Return a half-open range

static int _safe_ceil_to_intp ( double  value,
npy_intp ret 
) [static]
Like ceil(value), but check for overflow.
Return 0 on success, -1 on failure. In case of failure, set a PyExc_Overflow exception

Referenced by PyArray_MaskedCopyInto().

NPY_NO_EXPORT void _strided_byte_swap ( void *  p,
npy_intp  stride,
npy_intp  n,
int  size 
)

<

no byteswap necessary

References c.

Referenced by _new_sort().

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 _copyswap(), and _new_sort().

static int _update_descr_and_dimensions ( PyArray_Descr **  des,
npy_intp newdims,
npy_intp newstrides,
int  oldnd 
) [static]
Change a sub-array field to the base descriptor and update the dimensions and strides appropriately. Dimensions and strides are added to the end.
Strides are only added if given (because data is given).

Make new strides -- alwasy C-contiguous

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 
)
If numitems > 1, then dst must be contiguous

References PyArray_DATA.

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]
Take an arbitrary object and discover how many dimensions it has, filling in the dimensions as we go.

s is an Array
s is a Scalar
s is not a Sequence
s is a String
s is a Tuple, but tuples aren't expanded
s is a PEP 3118 buffer
s has the __array_struct__ interface
s has the __array_interface__ interface
1-dimensional sequence
PySequence_Check detects whether an old type object is a sequence by the presence of the __getitem__ attribute, and for new type objects that aren't dictionaries by the presence of the __len__ attribute as well. In either case it is possible to have an object that tests as a sequence but doesn't behave as a sequence and consequently, the PySequence_GetItem call can fail. When that happens and the object looks like a dictionary, we truncate the dimensions and set the object creation flag, otherwise we pass the error back up the call chain.
For the dimension truncation check below
Get the dimensions of the first item
see comment above
Reduce max_ndim_m1 to just items which match
If the dimensions are truncated, need to produce an object array.

static int discover_itemsize ( PyObject *  s,
int  nd,
int *  itemsize 
) [static]
The rest of this code is to build the right kind of array from a python object.

If an object has no length, leave it be

static int fromfile_next_element ( FILE **  fp,
void *  dptr,
PyArray_Descr dtype,
void *  NPY_UNUSEDstream_data 
) [static]

the NULL argument is for backwards-compatibility

References _PyArray_Descr::f, and PyArray_ArrFuncs::scanfunc.

Referenced by PyArray_Arange().

static int fromfile_skip_separator ( FILE **  fp,
const char *  sep,
void *  NPY_UNUSEDstream_data 
) [static]

matched separator
separator was whitespace wildcard that didn't match
whitespace wildcard

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]
Assuming that the separator is the next bit in the string (file), skip it.
Single spaces in the separator are matched to arbitrary-long sequences of whitespace in the input. If the separator consists only of spaces, it matches one or more whitespace characters.
If we can't match the separator, return -2. If we hit the end of the string (file), return -1. Otherwise, return 0.

matched separator
separator was whitespace wildcard that didn't match
whitespace wildcard

NPY_NO_EXPORT PyObject* PyArray_Arange ( double  start,
double  stop,
double  step,
int  type_num 
)
Arange,

place start in the buffer and the next value in the second position if length > 2, then call the inner loop, otherwise stop

References array_from_text(), array_fromfile_binary(), PyArrayObject::data, PyArrayObject::descr, _PyArray_Descr::elsize, _PyArray_Descr::f, fromfile_next_element(), fromfile_skip_separator(), NPY_MAX, PyArray_DIM, PyDataMem_RENEW, PyDataType_REFCHK, and PyArray_ArrFuncs::scanfunc.

NPY_NO_EXPORT PyObject* PyArray_ArangeObj ( PyObject *  start,
PyObject *  stop,
PyObject *  step,
PyArray_Descr dtype 
)
ArangeObj, <blockquote> this doesn't change the references</blockquote>

Datetime arange is handled specially
intentionally made to be PyArray_LONG default
calculate the length and next = start + step
If dtype is not in native byte-order then get native-byte order version. And then swap on the way out.
place start in the buffer and the next value in the second position if length > 2, then call the inner loop, otherwise stop

<

steals the reference

NPY_NO_EXPORT int PyArray_AssignFromSequence ( PyArrayObject self,
PyObject *  v 
)
NPY_NO_EXPORT PyObject* PyArray_CheckAxis ( PyArrayObject arr,
int *  axis,
int  flags 
)
PyArray_CheckAxis <blockquote> check that axis is valid convert 0-d arrays to 1-d arrays</blockquote>

References _PyArray_Descr::elsize.

Referenced by PyArray_ArgMax(), and PyArray_Prod().

NPY_NO_EXPORT PyObject* PyArray_CheckFromAny ( PyObject *  op,
PyArray_Descr descr,
int  min_depth,
int  max_depth,
int  requires,
PyObject *  context 
)
flags is any of NPY_ARRAY_C_CONTIGUOUS (formerly CONTIGUOUS), NPY_ARRAY_F_CONTIGUOUS (formerly FORTRAN), NPY_ARRAY_ALIGNED, NPY_ARRAY_WRITEABLE, NPY_ARRAY_NOTSWAPPED, NPY_ARRAY_ENSURECOPY, NPY_ARRAY_UPDATEIFCOPY, NPY_ARRAY_FORCECAST, NPY_ARRAY_ENSUREARRAY, NPY_ARRAY_ELEMENTSTRIDES
or'd (|) together
Any of these flags present means that the returned array should guarantee that aspect of the array. Otherwise the returned array won't guarantee it -- it will depend on the object as to whether or not it has such features.
Note that NPY_ARRAY_ENSURECOPY is enough to guarantee NPY_ARRAY_C_CONTIGUOUS, NPY_ARRAY_ALIGNED and NPY_ARRAY_WRITEABLE and therefore it is redundant to include those as well.
NPY_ARRAY_BEHAVED == NPY_ARRAY_ALIGNED | NPY_ARRAY_WRITEABLE NPY_ARRAY_CARRAY = NPY_ARRAY_C_CONTIGUOUS | NPY_ARRAY_BEHAVED NPY_ARRAY_FARRAY = NPY_ARRAY_F_CONTIGUOUS | NPY_ARRAY_BEHAVED
NPY_ARRAY_F_CONTIGUOUS can be set in the FLAGS to request a FORTRAN array. Fortran arrays are always behaved (aligned, notswapped, and writeable) and not (C) CONTIGUOUS (if > 1d).
NPY_ARRAY_UPDATEIFCOPY flag sets this flag in the returned array if a copy is made and the base argument points to the (possibly) misbehaved array. When the new array is deallocated, the original array held in base is updated with the contents of the new array.
NPY_ARRAY_FORCECAST will cause a cast to occur regardless of whether or not it is safe.
steals a reference to descr -- accepts NULL
NPY_NO_EXPORT int PyArray_CopyAnyInto ( PyArrayObject dst,
PyArrayObject src 
)
Copy an Array into another array -- memory must not overlap

Does not require src and dest to have "broadcastable" shapes (only the same number of elements).

TODO: For NumPy 2.0, this could accept an order parameter which
only allows NPY_CORDER and NPY_FORDER. Could also rename this to CopyAsFlat to make the name more intuitive.

Returns 0 on success, -1 on error.

References MAX_DIMS, PyArray_NDIM, and PyArray_Ravel().

Referenced by PyArray_TypeNumFromName().

NPY_NO_EXPORT int PyArray_CopyAnyIntoOrdered ( PyArrayObject dst,
PyArrayObject src,
NPY_ORDER  order 
)
TODO: Put the order parameter in PyArray_CopyAnyInto and remove this

If the shapes match and a particular order is forced for both, use the more efficient CopyInto
Zero-sized arrays require nothing be done
This copy is based on matching C-order traversals of src and dst. By using two iterators, we can find maximal sub-chunks that can be processed at once.
Get all the values needed for the inner loop
Since buffering is disabled, we can cache the stride
Since buffering is disabled, we can cache the stride
Because buffering is disabled in the iterator, the inner loop strides will be the same throughout the iteration loop. Thus, we can pass them to this function to take advantage of contiguous strides, etc.
The tests did not trigger this code, so added a new function ndarray.setasflat to the Python exposure in order to test it.
Transfer the biggest amount that fits both
If we exhausted the dst block, refresh it
If we exhausted the src block, refresh it

Referenced by PyArray_FromArrayAttr().

NPY_NO_EXPORT int PyArray_CopyInto ( PyArrayObject dst,
PyArrayObject src 
)
Copy an Array into another array -- memory must not overlap.

Broadcast to the destination shape if necessary.

Returns 0 on success, -1 on failure.

Check for overlap with positive strides, and if found, possibly reverse the order
TODO: In NumPy 2.0, reenable NPY_ITER_NO_BROADCAST. This
was removed during NumPy 1.6 testing for compatibility with NumPy 1.5, as per Travis's -10 veto power.
op_flags[0] = NPY_ITER_WRITEONLY|NPY_ITER_NO_BROADCAST;
If 'src' is being broadcast to 'dst', and it is smaller than the default NumPy buffer size, allow the iterator to make a copy of 'src' with the 'dst' dtype if necessary.
This is a performance operation, to allow fewer casts followed by more plain copies.
Because buffering is disabled in the iterator, the inner loop strides will be the same throughout the iteration loop. Thus, we can pass them to this function to take advantage of contiguous strides, etc.

Referenced by PyArray_CastToType().

NPY_NO_EXPORT PyArray_Descr* PyArray_DescrFromObject ( PyObject *  op,
PyArray_Descr mintype 
)
new reference -- accepts NULL for mintype
NPY_NO_EXPORT PyObject* PyArray_Empty ( int  nd,
npy_intp dims,
PyArray_Descr type,
int  is_f_order 
)
Empty <blockquote> accepts NULL type steals referenct to type</blockquote>

References PyArrayObject::data, _PyArray_Descr::elsize, and PyDataMem_RENEW.

NPY_NO_EXPORT PyObject* PyArray_EnsureAnyArray ( PyObject *  op)

Referenced by _strings_richcompare().

NPY_NO_EXPORT PyObject* PyArray_EnsureArray ( PyObject *  op)
end old calls
This is a quick wrapper around PyArray_FromAny(op, NULL, 0, 0, ENSUREARRAY)
that special cases Arrays and PyArray_Scalars up front It steals a reference to the object It also guarantees that the result is PyArray_Type Because it decrefs op if any conversion needs to take place so it can be used like PyArray_EnsureArray(some_function(...))

References NpyIter_Deallocate().

NPY_NO_EXPORT PyObject* PyArray_FromAny ( PyObject *  op,
PyArray_Descr newtype,
int  min_depth,
int  max_depth,
int  flags,
PyObject *  context 
)
Does not check for NPY_ARRAY_ENSURECOPY and NPY_ARRAY_NOTSWAPPED in flags
Steals a reference to newtype --- which can be NULL

This is the main code to make a NumPy array from a Python Object. It is called from many different places.
Get either the array or its parameters if it isn't an array
If the requested dtype is flexible, adapt it
If we got dimensions and dtype instead of an array
TODO: would be nice to do this too, but it's
a behavior change. It's also a bit tricky for downcasting to small integer and float types, and might be better to modify PyArray_AssignFromSequence and descr->f->setitem to have a 'casting' parameter and to check each value with scalar rules like in PyArray_MinScalarType.
if (!(flags&NPY_ARRAY_FORCECAST) && ndim > 0 &&

<blockquote class="first"> !PyArray_CanCastTo(dtype, newtype)) {</blockquote>

System Message: WARNING/2 (<string>, line 4) Block quote ends without a blank line; unexpected unindent.

Py_DECREF(dtype); Py_XDECREF(newtype); PyErr_SetString(PyExc_TypeError,

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

<blockquote> "object cannot be safely cast to array " "of required type");</blockquote>

System Message: WARNING/2 (<string>, line 9) Block quote ends without a blank line; unexpected unindent.

return NULL;

System Message: WARNING/2 (<string>, line 10) Definition list ends without a blank line; unexpected unindent.
}
Create an array and copy the data

Referenced by array_subscript_nice(), einsum_sub_op_from_str(), new_array_for_sum(), and PyArray_CompareLists().

NPY_NO_EXPORT PyObject* PyArray_FromArray ( PyArrayObject arr,
PyArray_Descr newtype,
int  flags 
)
steals reference to newtype --- acc. NULL

Can't cast unless ndim-0 array, NPY_ARRAY_FORCECAST is specified or the cast is safe.
Don't copy if sizes are compatible
If no copy then just increase the reference count and return the input
The desired output type is different than the input array type and copy was not specified

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 
)

Store a reference for decref on deallocation

NPY_NO_EXPORT PyObject* PyArray_FromDims ( int  nd,
int *  d,
int  type 
)
Construct an empty array from dimensions and typenum

Old FromDims set memory to zero --- some algorithms relied on that. Better keep it the same. If Object type, then it's already been set to zero, though.

NPY_NO_EXPORT PyObject* PyArray_FromDimsAndDataAndDescr ( int  nd,
int *  d,
PyArray_Descr descr,
char *  data 
)
These are also old calls (should use PyArray_NewFromDescr)
They all zero-out the memory as previously done
steals reference to descr -- and enforces native byteorder on it.
Like FromDimsAndData but uses the Descr structure instead of typecode
as input.
NPY_NO_EXPORT PyObject* PyArray_FromFile ( FILE *  fp,
PyArray_Descr dtype,
npy_intp  num,
char *  sep 
)
Given a 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.

Realloc memory for smaller number of elements

NPY_NO_EXPORT PyObject* PyArray_FromInterface ( PyObject *  input)

Get the memory from __array_data__ and __array_offset__
Get the shape
Get the typestring -- ignore array_descr
Get the strides

NPY_NO_EXPORT PyObject* PyArray_FromIter ( PyObject *  obj,
PyArray_Descr dtype,
npy_intp  count 
)
steals a reference to dtype (which cannot be NULL)

We would need to alter the memory RENEW code to decrement any reference counts before throwing away any memory.
Grow ret->data: this is similar for the strategy for PyListObject, but we use 50% overallocation => 0, 4, 8, 14, 23, 36, 56, 86 ...
Realloc the data so that don't keep extra memory tied up (assuming realloc is reasonably good about reusing space...)

NPY_NO_EXPORT PyObject* PyArray_FromString ( char *  data,
npy_intp  slen,
PyArray_Descr dtype,
npy_intp  num,
char *  sep 
)
Given a pointer to a string 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.

read from character-based string

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 
)
Retrieves the array parameters for viewing/converting an arbitrary

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'.

Result: When success (0 return value) is returned, either out_arr
is filled with a non-NULL PyArrayObject and the rest of the parameters are untouched, or out_arr is filled with NULL, and the rest of the parameters are filled.

Typical usage: <blockquote class="last">

PyArrayObject *arr = NULL; PyArray_Descr *dtype = NULL; int ndim = 0; npy_intp dims[NPY_MAXDIMS];

System Message: WARNING/2 (<string>, line 26); backlink Inline emphasis start-string without end-string.
System Message: WARNING/2 (<string>, line 26); backlink Inline emphasis start-string without end-string.
if (PyArray_GetArrayParamsFromObject(op, NULL, 1, &dtype,

<blockquote class="first"> &ndim, &dims, &arr, NULL) < 0) {</blockquote>

System Message: WARNING/2 (<string>, line 33) Block quote ends without a blank line; unexpected unindent.

return NULL;

System Message: WARNING/2 (<string>, line 34) Definition list ends without a blank line; unexpected unindent.

} if (arr == NULL) {

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

<blockquote>

... validate/change dtype, validate flags, ndim, etc ... Could make custom strides here too arr = PyArray_NewFromDescr(&PyArray_Type, dtype, ndim,

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

<blockquote> dims, NULL, is_f_order ? NPY_ARRAY_F_CONTIGUOUS : 0, NULL);</blockquote>

System Message: WARNING/2 (<string>, line 42) Block quote ends without a blank line; unexpected unindent.
if (arr == NULL) {
return NULL;
System Message: WARNING/2 (<string>, line 44) Definition list ends without a blank line; unexpected unindent.

} if (PyArray_CopyObject(arr, op) < 0) {

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

<blockquote> Py_DECREF(arr); return NULL;</blockquote>

System Message: WARNING/2 (<string>, line 48) Block quote ends without a blank line; unexpected unindent.

} </blockquote>

System Message: WARNING/2 (<string>, line 49) Block quote ends without a blank line; unexpected unindent.

} else {

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

<blockquote>

... in this case the other parameters weren't filled, just
validate and possibly copy arr itself ...

</blockquote>

System Message: WARNING/2 (<string>, line 53) Block quote ends without a blank line; unexpected unindent.

} ... use arr ... </blockquote>

If op is an array
If op is a NumPy scalar
If op is a Python scalar
If op supports the PEP 3118 buffer interface
If op supports the __array_struct__ or __array_interface__ interface
If op supplies the __array__ function. The documentation says this should produce a copy, so we skip this method if writeable is true, because the intent of writeable is to modify the operand. XXX: If the implementation is wrong, and/or if actual

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

<blockquote> usage requires this behave differently, this should be changed!</blockquote>

Try to treat op as a list of lists
Determine the type, using the requested data type if it will affect how the array is retrieved
Say it's an OBJECT array if there's an error
If object arrays are forced
If the type is flexible, determine its size
Say it's an OBJECT scalar if there's an error
Anything can be viewed as an object, unless it needs to be writeable

NPY_NO_EXPORT int PyArray_MaskedCopyInto ( PyArrayObject dst,
PyArrayObject src,
PyArrayObject mask,
NPY_CASTING  casting 
)
Copy an Array into another array, wherever the mask specifies.

The memory of src and dst must not overlap.

Broadcast to the destination shape if necessary.

Returns 0 on success, -1 on failure.

Check for overlap with positive strides, and if found, possibly reverse the order
TODO: In NumPy 2.0, renable NPY_ITER_NO_BROADCAST. This
was removed during NumPy 1.6 testing for compatibility with NumPy 1.5, as per Travis's -10 veto power.
op_flags[0] = NPY_ITER_WRITEONLY|NPY_ITER_NO_BROADCAST;
If 'src' is being broadcast to 'dst', and it is smaller than the default NumPy buffer size, allow the iterator to make a copy of 'src' with the 'dst' dtype if necessary.
This is a performance operation, to allow fewer casts followed by more plain copies.
Because buffering is disabled in the iterator, the inner loop strides will be the same throughout the iteration loop. Thus, we can pass them to this function to take advantage of contiguous strides, etc.

References _safe_ceil_to_intp(), error_converting, and MIN.

NPY_NO_EXPORT int PyArray_MaskedMoveInto ( PyArrayObject dst,
PyArrayObject src,
PyArrayObject mask,
NPY_CASTING  casting 
)
Copy the memory of one array into another, allowing for overlapping data

and selecting which elements to move based on a mask.

Precisely handling the overlapping data is in general a difficult problem to solve efficiently, because strides can be negative. Consider "a = np.arange(3); a[::-1] = a", which previously produced the incorrect [0, 1, 0].

Instead of trying to be fancy, we simply check for overlap and make a temporary copy when one exists.

Returns 0 on success, negative on failure.

Performance fix for expresions like "a[1000:6000] += x". In this case, first an in-place add is done, followed by an assignment, equivalently expressed like this: <blockquote> tmp = a[1000:6000] # Calls array_subscript_nice in mapping.c np.add(tmp, x, tmp) a[1000:6000] = tmp # Calls array_ass_sub in mapping.c</blockquote>
In the assignment the underlying data type, shape, strides, and data pointers are identical, but src != dst because they are separately generated slices. By detecting this and skipping the redundant copy of values to themselves, we potentially give a big speed boost.
Note that we don't call EquivTypes, because usually the exact same dtype object will appear, and we don't want to slow things down with a complicated comparison. The comparisons are ordered to try and reject this with as little work as possible.
printf("Redundant copy operation detectedn");
A special case is when there is just one dimension with positive strides, and we pass that to CopyInto, which correctly handles it for most cases. It may still incorrectly handle copying of partially-overlapping data elements, where the data pointer was offset by a fraction of the element size.
Allocate a temporary copy array.

NPY_NO_EXPORT int PyArray_MoveInto ( PyArrayObject dst,
PyArrayObject src 
)
Move the memory of one array into another, allowing for overlapping data. <blockquote>
This is in general a difficult problem to solve efficiently, because strides can be negative. Consider "a = np.arange(3); a[::-1] = a", which previously produced the incorrect [0, 1, 0].
Instead of trying to be fancy, we simply check for overlap and make a temporary copy when one exists.
Returns 0 on success, negative on failure. </blockquote>

Performance fix for expresions like "a[1000:6000] += x". In this case, first an in-place add is done, followed by an assignment, equivalently expressed like this: <blockquote> tmp = a[1000:6000] # Calls array_subscript_nice in mapping.c np.add(tmp, x, tmp) a[1000:6000] = tmp # Calls array_ass_sub in mapping.c</blockquote>
In the assignment the underlying data type, shape, strides, and data pointers are identical, but src != dst because they are separately generated slices. By detecting this and skipping the redundant copy of values to themselves, we potentially give a big speed boost.
Note that we don't call EquivTypes, because usually the exact same dtype object will appear, and we don't want to slow things down with a complicated comparison. The comparisons are ordered to try and reject this with as little work as possible.
printf("Redundant copy operation detectedn");
A special case is when there is just one dimension with positive strides, and we pass that to CopyInto, which correctly handles it for most cases. It may still incorrectly handle copying of partially-overlapping data elements, where the data pointer was offset by a fraction of the element size.
Allocate a temporary copy array.

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 
)
Generic new array creation routine. <blockquote> steals a reference to descr (even on failure)</blockquote>

Check dimensions
Compare to PyArray_OverflowMultiplyList that returns 0 in this case.
Care needs to be taken to avoid integer overflow when multiplying the dimensions together to get the total size of the array. Hence before each multiplication we first check that the product will not exceed the maximum allowable size.

<

fill it in
we allow strides even when we create the memory, but be careful with this...
Allocate something even for zero-space arrays e.g. shape=(0,) -- otherwise buffer exposure (a.data) doesn't work as it should.
It is bad to have unitialized OBJECT pointers which could also be sub-fields of a VOID array
If data is passed in, this object won't own it by default. Caller must arrange for this to be reset if truly desired
If the strides were provided to the function, need to update the flags to get the right CONTIGUOUS, ALIGN properties
call the __array_finalize__ method if a subtype. If obj is NULL, then call method with Py_None
A C-function is stored here

Referenced by array_real_get(), array_swapaxes(), npyiter_flip_negative_strides(), PyArray_CastScalarDirect(), PyArray_CastToType(), PyArray_TakeFrom(), and PyArray_Zeros().

NPY_NO_EXPORT PyObject* PyArray_NewLikeArray ( PyArrayObject prototype,
NPY_ORDER  order,
PyArray_Descr dtype,
int  subok 
)
Creates a new array with the same shape as the provided one,

with possible memory layout order and data type changes.

prototype - The array the new one should be like. order - NPY_CORDER - C-contiguous result.

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

<blockquote> NPY_FORTRANORDER - Fortran-contiguous result. NPY_ANYORDER - Fortran if prototype is Fortran, C otherwise. NPY_KEEPORDER - Keeps the axis ordering of prototype.</blockquote>

System Message: WARNING/2 (<string>, line 9) Block quote ends without a blank line; unexpected unindent.

dtype - If not NULL, overrides the data type of the result. subok - If 1, use the prototype's array subtype, otherwise

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

<blockquote> always create a base-class array.</blockquote>

NOTE: If dtype is not NULL, steals the dtype reference.

If no override data type, use the one from the prototype
Handle ANYORDER and simple KEEPORDER cases
If it's not KEEPORDER, this is simple
KEEPORDER needs some analysis of the strides
Build the new strides
Finally, allocate the array

NPY_NO_EXPORT PyObject* PyArray_Zeros ( int  nd,
npy_intp dims,
PyArray_Descr type,
int  is_f_order 
)
Zeros <blockquote> steal a reference accepts NULL type</blockquote>

References FROM_BUFFER_SIZE, NPY_BEGIN_ALLOW_THREADS, PyArray_NewFromDescr(), and swab_separator().

static int setArrayFromSequence ( PyArrayObject a,
PyObject *  s,
int  dim,
npy_intp  offset 
) [static]
adapted from Numarray

This code is to ensure that the sequence access below will return a lower-dimensional sequence.
INCREF on entry DECREF on exit
FIXME: This could probably copy the entire subarray at once here using a faster algorithm. Right now, just make sure a base-class array is used so that the dimensionality reduction assumption is correct.
This will DECREF(s) if replaced
Either the dimensions match, or the sequence has length 1 and can be broadcast to the destination.
Broadcast the one element from the sequence to all the outputs
Copy element by element

static char* swab_separator ( char *  sep) [static]
Remove multiple whitespace from the separator, and add a space to the beginning and end. This simplifies the separator-skipping code below.

add space to front if there isn't one
add space to end if there isn't one

Referenced by PyArray_Zeros().