1 #ifndef NDARRAYTYPES_H
2 #define NDARRAYTYPES_H
3
4 #include "npy_common.h"
5 #include "npy_endian.h"
6 #include "npy_cpu.h"
7 #include "utils.h"
8
9 #define NPY_NO_EXPORT NPY_VISIBILITY_HIDDEN
10
11 /* Only use thread if configured in config and python supports it */
12 #if defined WITH_THREAD && !NPY_NO_SMP
13 #define NPY_ALLOW_THREADS 1
14 #else
15 #define NPY_ALLOW_THREADS 0
16 #endif
17
18 #ifndef __has_extension
19 #define __has_extension(x) 0
20 #endif
21
22 #if !defined(_NPY_NO_DEPRECATIONS) && \
23 ((defined(__GNUC__)&& __GNUC__ >= 6) || \
24 __has_extension(attribute_deprecated_with_message))
25 #define NPY_ATTR_DEPRECATE(text) __attribute__ ((deprecated (text)))
26 #else
27 #define NPY_ATTR_DEPRECATE(text)
28 #endif
29
30 /*
31 * There are several places in the code where an array of dimensions
32 * is allocated statically. This is the size of that static
33 * allocation.
34 *
35 * The array creation itself could have arbitrary dimensions but all
36 * the places where static allocation is used would need to be changed
37 * to dynamic (including inside of several structures)
38 */
39
40 #define NPY_MAXDIMS 32
41 #define NPY_MAXARGS 32
42
43 /* Used for Converter Functions "O&" code in ParseTuple */
44 #define NPY_FAIL 0
45 #define NPY_SUCCEED 1
46
47 /*
48 * Binary compatibility version number. This number is increased
49 * whenever the C-API is changed such that binary compatibility is
50 * broken, i.e. whenever a recompile of extension modules is needed.
51 */
52 #define NPY_VERSION NPY_ABI_VERSION
53
54 /*
55 * Minor API version. This number is increased whenever a change is
56 * made to the C-API -- whether it breaks binary compatibility or not.
57 * Some changes, such as adding a function pointer to the end of the
58 * function table, can be made without breaking binary compatibility.
59 * In this case, only the NPY_FEATURE_VERSION (*not* NPY_VERSION)
60 * would be increased. Whenever binary compatibility is broken, both
61 * NPY_VERSION and NPY_FEATURE_VERSION should be increased.
62 */
63 #define NPY_FEATURE_VERSION NPY_API_VERSION
64
65 enum NPY_TYPES { NPY_BOOL=0,
66 NPY_BYTE, NPY_UBYTE,
67 NPY_SHORT, NPY_USHORT,
68 NPY_INT, NPY_UINT,
69 NPY_LONG, NPY_ULONG,
70 NPY_LONGLONG, NPY_ULONGLONG,
71 NPY_FLOAT, NPY_DOUBLE, NPY_LONGDOUBLE,
72 NPY_CFLOAT, NPY_CDOUBLE, NPY_CLONGDOUBLE,
73 NPY_OBJECT=17,
74 NPY_STRING, NPY_UNICODE,
75 NPY_VOID,
76 /*
77 * New 1.6 types appended, may be integrated
78 * into the above in 2.0.
79 */
80 NPY_DATETIME, NPY_TIMEDELTA, NPY_HALF,
81
82 NPY_NTYPES,
83 NPY_NOTYPE,
84 NPY_CHAR NPY_ATTR_DEPRECATE("Use NPY_STRING"),
85 NPY_USERDEF=256, /* leave room for characters */
86
87 /* The number of types not including the new 1.6 types */
88 NPY_NTYPES_ABI_COMPATIBLE=21
89 };
90 #ifdef _MSC_VER
91 #pragma deprecated(NPY_CHAR)
92 #endif
93
94 /* basetype array priority */
95 #define NPY_PRIORITY 0.0
96
97 /* default subtype priority */
98 #define NPY_SUBTYPE_PRIORITY 1.0
99
100 /* default scalar priority */
101 #define NPY_SCALAR_PRIORITY -1000000.0
102
103 /* How many floating point types are there (excluding half) */
104 #define NPY_NUM_FLOATTYPE 3
105
106 /*
107 * These characters correspond to the array type and the struct
108 * module
109 */
110
111 enum NPY_TYPECHAR {
112 NPY_BOOLLTR = '?',
113 NPY_BYTELTR = 'b',
114 NPY_UBYTELTR = 'B',
115 NPY_SHORTLTR = 'h',
116 NPY_USHORTLTR = 'H',
117 NPY_INTLTR = 'i',
118 NPY_UINTLTR = 'I',
119 NPY_LONGLTR = 'l',
120 NPY_ULONGLTR = 'L',
121 NPY_LONGLONGLTR = 'q',
122 NPY_ULONGLONGLTR = 'Q',
123 NPY_HALFLTR = 'e',
124 NPY_FLOATLTR = 'f',
125 NPY_DOUBLELTR = 'd',
126 NPY_LONGDOUBLELTR = 'g',
127 NPY_CFLOATLTR = 'F',
128 NPY_CDOUBLELTR = 'D',
129 NPY_CLONGDOUBLELTR = 'G',
130 NPY_OBJECTLTR = 'O',
131 NPY_STRINGLTR = 'S',
132 NPY_STRINGLTR2 = 'a',
133 NPY_UNICODELTR = 'U',
134 NPY_VOIDLTR = 'V',
135 NPY_DATETIMELTR = 'M',
136 NPY_TIMEDELTALTR = 'm',
137 NPY_CHARLTR = 'c',
138
139 /*
140 * No Descriptor, just a define -- this let's
141 * Python users specify an array of integers
142 * large enough to hold a pointer on the
143 * platform
144 */
145 NPY_INTPLTR = 'p',
146 NPY_UINTPLTR = 'P',
147
148 /*
149 * These are for dtype 'kinds', not dtype 'typecodes'
150 * as the above are for.
151 */
152 NPY_GENBOOLLTR ='b',
153 NPY_SIGNEDLTR = 'i',
154 NPY_UNSIGNEDLTR = 'u',
155 NPY_FLOATINGLTR = 'f',
156 NPY_COMPLEXLTR = 'c'
157 };
158
159 /*
160 * Changing this may break Numpy API compatibility
161 * due to changing offsets in PyArray_ArrFuncs, so be
162 * careful. Here we have reused the mergesort slot for
163 * any kind of stable sort, the actual implementation will
164 * depend on the data type.
165 */
166 typedef enum {
167 NPY_QUICKSORT=0,
168 NPY_HEAPSORT=1,
169 NPY_MERGESORT=2,
170 NPY_STABLESORT=2,
171 } NPY_SORTKIND;
172 #define NPY_NSORTS (NPY_STABLESORT + 1)
173
174
175 typedef enum {
176 NPY_INTROSELECT=0
177 } NPY_SELECTKIND;
178 #define NPY_NSELECTS (NPY_INTROSELECT + 1)
179
180
181 typedef enum {
182 NPY_SEARCHLEFT=0,
183 NPY_SEARCHRIGHT=1
184 } NPY_SEARCHSIDE;
185 #define NPY_NSEARCHSIDES (NPY_SEARCHRIGHT + 1)
186
187
188 typedef enum {
189 NPY_NOSCALAR=-1,
190 NPY_BOOL_SCALAR,
191 NPY_INTPOS_SCALAR,
192 NPY_INTNEG_SCALAR,
193 NPY_FLOAT_SCALAR,
194 NPY_COMPLEX_SCALAR,
195 NPY_OBJECT_SCALAR
196 } NPY_SCALARKIND;
197 #define NPY_NSCALARKINDS (NPY_OBJECT_SCALAR + 1)
198
199 /* For specifying array memory layout or iteration order */
200 typedef enum {
201 /* Fortran order if inputs are all Fortran, C otherwise */
202 NPY_ANYORDER=-1,
203 /* C order */
204 NPY_CORDER=0,
205 /* Fortran order */
206 NPY_FORTRANORDER=1,
207 /* An order as close to the inputs as possible */
208 NPY_KEEPORDER=2
209 } NPY_ORDER;
210
211 /* For specifying allowed casting in operations which support it */
212 typedef enum {
213 _NPY_ERROR_OCCURRED_IN_CAST = -1,
214 /* Only allow identical types */
215 NPY_NO_CASTING=0,
216 /* Allow identical and byte swapped types */
217 NPY_EQUIV_CASTING=1,
218 /* Only allow safe casts */
219 NPY_SAFE_CASTING=2,
220 /* Allow safe casts or casts within the same kind */
221 NPY_SAME_KIND_CASTING=3,
222 /* Allow any casts */
223 NPY_UNSAFE_CASTING=4,
224 /*
225 * Flag to allow signalling that a cast is a view, this flag is not
226 * valid when requesting a cast of specific safety.
227 * _NPY_CAST_IS_VIEW|NPY_EQUIV_CASTING means the same as NPY_NO_CASTING.
228 */
229 // TODO-DTYPES: Needs to be documented.
230 _NPY_CAST_IS_VIEW = 1 << 16,
231 } NPY_CASTING;
232
233 typedef enum {
234 NPY_CLIP=0,
235 NPY_WRAP=1,
236 NPY_RAISE=2
237 } NPY_CLIPMODE;
238
239 /* The special not-a-time (NaT) value */
240 #define NPY_DATETIME_NAT NPY_MIN_INT64
241
242 /*
243 * Upper bound on the length of a DATETIME ISO 8601 string
244 * YEAR: 21 (64-bit year)
245 * MONTH: 3
246 * DAY: 3
247 * HOURS: 3
248 * MINUTES: 3
249 * SECONDS: 3
250 * ATTOSECONDS: 1 + 3*6
251 * TIMEZONE: 5
252 * NULL TERMINATOR: 1
253 */
254 #define NPY_DATETIME_MAX_ISO8601_STRLEN (21 + 3*5 + 1 + 3*6 + 6 + 1)
255
256 /* The FR in the unit names stands for frequency */
257 typedef enum {
258 /* Force signed enum type, must be -1 for code compatibility */
259 NPY_FR_ERROR = -1, /* error or undetermined */
260
261 /* Start of valid units */
262 NPY_FR_Y = 0, /* Years */
263 NPY_FR_M = 1, /* Months */
264 NPY_FR_W = 2, /* Weeks */
265 /* Gap where 1.6 NPY_FR_B (value 3) was */
266 NPY_FR_D = 4, /* Days */
267 NPY_FR_h = 5, /* hours */
268 NPY_FR_m = 6, /* minutes */
269 NPY_FR_s = 7, /* seconds */
270 NPY_FR_ms = 8, /* milliseconds */
271 NPY_FR_us = 9, /* microseconds */
272 NPY_FR_ns = 10, /* nanoseconds */
273 NPY_FR_ps = 11, /* picoseconds */
274 NPY_FR_fs = 12, /* femtoseconds */
275 NPY_FR_as = 13, /* attoseconds */
276 NPY_FR_GENERIC = 14 /* unbound units, can convert to anything */
277 } NPY_DATETIMEUNIT;
278
279 /*
280 * NOTE: With the NPY_FR_B gap for 1.6 ABI compatibility, NPY_DATETIME_NUMUNITS
281 * is technically one more than the actual number of units.
282 */
283 #define NPY_DATETIME_NUMUNITS (NPY_FR_GENERIC + 1)
284 #define NPY_DATETIME_DEFAULTUNIT NPY_FR_GENERIC
285
286 /*
287 * Business day conventions for mapping invalid business
288 * days to valid business days.
289 */
290 typedef enum {
291 /* Go forward in time to the following business day. */
292 NPY_BUSDAY_FORWARD,
293 NPY_BUSDAY_FOLLOWING = NPY_BUSDAY_FORWARD,
294 /* Go backward in time to the preceding business day. */
295 NPY_BUSDAY_BACKWARD,
296 NPY_BUSDAY_PRECEDING = NPY_BUSDAY_BACKWARD,
297 /*
298 * Go forward in time to the following business day, unless it
299 * crosses a month boundary, in which case go backward
300 */
301 NPY_BUSDAY_MODIFIEDFOLLOWING,
302 /*
303 * Go backward in time to the preceding business day, unless it
304 * crosses a month boundary, in which case go forward.
305 */
306 NPY_BUSDAY_MODIFIEDPRECEDING,
307 /* Produce a NaT for non-business days. */
308 NPY_BUSDAY_NAT,
309 /* Raise an exception for non-business days. */
310 NPY_BUSDAY_RAISE
311 } NPY_BUSDAY_ROLL;
312
313 /************************************************************
314 * NumPy Auxiliary Data for inner loops, sort functions, etc.
315 ************************************************************/
316
317 /*
318 * When creating an auxiliary data struct, this should always appear
319 * as the first member, like this:
320 *
321 * typedef struct {
322 * NpyAuxData base;
323 * double constant;
324 * } constant_multiplier_aux_data;
325 */
326 typedef struct NpyAuxData_tag NpyAuxData;
327
328 /* Function pointers for freeing or cloning auxiliary data */
329 typedef void (NpyAuxData_FreeFunc) (NpyAuxData *);
330 typedef NpyAuxData *(NpyAuxData_CloneFunc) (NpyAuxData *);
331
332 struct NpyAuxData_tag {
333 NpyAuxData_FreeFunc *free;
334 NpyAuxData_CloneFunc *clone;
335 /* To allow for a bit of expansion without breaking the ABI */
336 void *reserved[2];
337 };
338
339 /* Macros to use for freeing and cloning auxiliary data */
340 #define NPY_AUXDATA_FREE(auxdata) \
341 do { \
342 if ((auxdata) != NULL) { \
343 (auxdata)->free(auxdata); \
344 } \
345 } while(0)
346 #define NPY_AUXDATA_CLONE(auxdata) \
347 ((auxdata)->clone(auxdata))
348
349 #define NPY_ERR(str) fprintf(stderr, #str); fflush(stderr);
350 #define NPY_ERR2(str) fprintf(stderr, str); fflush(stderr);
351
352 /*
353 * Macros to define how array, and dimension/strides data is
354 * allocated.
355 */
356
357 /* Data buffer - PyDataMem_NEW/FREE/RENEW are in multiarraymodule.c */
358
359 #define NPY_USE_PYMEM 1
360
361
362 #if NPY_USE_PYMEM == 1
363 /* use the Raw versions which are safe to call with the GIL released */
364 #define PyArray_malloc PyMem_RawMalloc
365 #define PyArray_free PyMem_RawFree
366 #define PyArray_realloc PyMem_RawRealloc
367 #else
368 #define PyArray_malloc malloc
369 #define PyArray_free free
370 #define PyArray_realloc realloc
371 #endif
372
373 /* Dimensions and strides */
374 #define PyDimMem_NEW(size) \
375 ((npy_intp *)PyArray_malloc(size*sizeof(npy_intp)))
376
377 #define PyDimMem_FREE(ptr) PyArray_free(ptr)
378
379 #define PyDimMem_RENEW(ptr,size) \
380 ((npy_intp *)PyArray_realloc(ptr,size*sizeof(npy_intp)))
381
382 /* forward declaration */
383 struct _PyArray_Descr;
384
385 /* These must deal with unaligned and swapped data if necessary */
386 typedef PyObject * (PyArray_GetItemFunc) (void *, void *);
387 typedef int (PyArray_SetItemFunc)(PyObject *, void *, void *);
388
389 typedef void (PyArray_CopySwapNFunc)(void *, npy_intp, void *, npy_intp,
390 npy_intp, int, void *);
391
392 typedef void (PyArray_CopySwapFunc)(void *, void *, int, void *);
393 typedef npy_bool (PyArray_NonzeroFunc)(void *, void *);
394
395
396 /*
397 * These assume aligned and notswapped data -- a buffer will be used
398 * before or contiguous data will be obtained
399 */
400
401 typedef int (PyArray_CompareFunc)(const void *, const void *, void *);
402 typedef int (PyArray_ArgFunc)(void*, npy_intp, npy_intp*, void *);
403
404 typedef void (PyArray_DotFunc)(void *, npy_intp, void *, npy_intp, void *,
405 npy_intp, void *);
406
407 typedef void (PyArray_VectorUnaryFunc)(void *, void *, npy_intp, void *,
408 void *);
409
410 /*
411 * XXX the ignore argument should be removed next time the API version
412 * is bumped. It used to be the separator.
413 */
414 typedef int (PyArray_ScanFunc)(FILE *fp, void *dptr,
415 char *ignore, struct _PyArray_Descr *);
416 typedef int (PyArray_FromStrFunc)(char *s, void *dptr, char **endptr,
417 struct _PyArray_Descr *);
418
419 typedef int (PyArray_FillFunc)(void *, npy_intp, void *);
420
421 typedef int (PyArray_SortFunc)(void *, npy_intp, void *);
422 typedef int (PyArray_ArgSortFunc)(void *, npy_intp *, npy_intp, void *);
423 typedef int (PyArray_PartitionFunc)(void *, npy_intp, npy_intp,
424 npy_intp *, npy_intp *,
425 void *);
426 typedef int (PyArray_ArgPartitionFunc)(void *, npy_intp *, npy_intp, npy_intp,
427 npy_intp *, npy_intp *,
428 void *);
429
430 typedef int (PyArray_FillWithScalarFunc)(void *, npy_intp, void *, void *);
431
432 typedef int (PyArray_ScalarKindFunc)(void *);
433
434 typedef void (PyArray_FastClipFunc)(void *in, npy_intp n_in, void *min,
435 void *max, void *out);
436 typedef void (PyArray_FastPutmaskFunc)(void *in, void *mask, npy_intp n_in,
437 void *values, npy_intp nv);
438 typedef int (PyArray_FastTakeFunc)(void *dest, void *src, npy_intp *indarray,
439 npy_intp nindarray, npy_intp n_outer,
440 npy_intp m_middle, npy_intp nelem,
441 NPY_CLIPMODE clipmode);
442
443 typedef struct {
444 npy_intp *ptr;
445 int len;
446 } PyArray_Dims;
447
448 typedef struct {
449 /*
450 * Functions to cast to most other standard types
451 * Can have some NULL entries. The types
452 * DATETIME, TIMEDELTA, and HALF go into the castdict
453 * even though they are built-in.
454 */
455 PyArray_VectorUnaryFunc *cast[NPY_NTYPES_ABI_COMPATIBLE];
456
457 /* The next four functions *cannot* be NULL */
458
459 /*
460 * Functions to get and set items with standard Python types
461 * -- not array scalars
462 */
463 PyArray_GetItemFunc *getitem;
464 PyArray_SetItemFunc *setitem;
465
466 /*
467 * Copy and/or swap data. Memory areas may not overlap
468 * Use memmove first if they might
469 */
470 PyArray_CopySwapNFunc *copyswapn;
471 PyArray_CopySwapFunc *copyswap;
472
473 /*
474 * Function to compare items
475 * Can be NULL
476 */
477 PyArray_CompareFunc *compare;
478
479 /*
480 * Function to select largest
481 * Can be NULL
482 */
483 PyArray_ArgFunc *argmax;
484
485 /*
486 * Function to compute dot product
487 * Can be NULL
488 */
489 PyArray_DotFunc *dotfunc;
490
491 /*
492 * Function to scan an ASCII file and
493 * place a single value plus possible separator
494 * Can be NULL
495 */
496 PyArray_ScanFunc *scanfunc;
497
498 /*
499 * Function to read a single value from a string
500 * and adjust the pointer; Can be NULL
501 */
502 PyArray_FromStrFunc *fromstr;
503
504 /*
505 * Function to determine if data is zero or not
506 * If NULL a default version is
507 * used at Registration time.
508 */
509 PyArray_NonzeroFunc *nonzero;
510
511 /*
512 * Used for arange. Should return 0 on success
513 * and -1 on failure.
514 * Can be NULL.
515 */
516 PyArray_FillFunc *fill;
517
518 /*
519 * Function to fill arrays with scalar values
520 * Can be NULL
521 */
522 PyArray_FillWithScalarFunc *fillwithscalar;
523
524 /*
525 * Sorting functions
526 * Can be NULL
527 */
528 PyArray_SortFunc *sort[NPY_NSORTS];
529 PyArray_ArgSortFunc *argsort[NPY_NSORTS];
530
531 /*
532 * Dictionary of additional casting functions
533 * PyArray_VectorUnaryFuncs
534 * which can be populated to support casting
535 * to other registered types. Can be NULL
536 */
537 PyObject *castdict;
538
539 /*
540 * Functions useful for generalizing
541 * the casting rules.
542 * Can be NULL;
543 */
544 PyArray_ScalarKindFunc *scalarkind;
545 int **cancastscalarkindto;
546 int *cancastto;
547
548 PyArray_FastClipFunc *fastclip;
549 PyArray_FastPutmaskFunc *fastputmask;
550 PyArray_FastTakeFunc *fasttake;
551
552 /*
553 * Function to select smallest
554 * Can be NULL
555 */
556 PyArray_ArgFunc *argmin;
557
558 } PyArray_ArrFuncs;
559
560 /* The item must be reference counted when it is inserted or extracted. */
561 #define NPY_ITEM_REFCOUNT 0x01
562 /* Same as needing REFCOUNT */
563 #define NPY_ITEM_HASOBJECT 0x01
564 /* Convert to list for pickling */
565 #define NPY_LIST_PICKLE 0x02
566 /* The item is a POINTER */
567 #define NPY_ITEM_IS_POINTER 0x04
568 /* memory needs to be initialized for this data-type */
569 #define NPY_NEEDS_INIT 0x08
570 /* operations need Python C-API so don't give-up thread. */
571 #define NPY_NEEDS_PYAPI 0x10
572 /* Use f.getitem when extracting elements of this data-type */
573 #define NPY_USE_GETITEM 0x20
574 /* Use f.setitem when setting creating 0-d array from this data-type.*/
575 #define NPY_USE_SETITEM 0x40
576 /* A sticky flag specifically for structured arrays */
577 #define NPY_ALIGNED_STRUCT 0x80
578
579 /*
580 *These are inherited for global data-type if any data-types in the
581 * field have them
582 */
583 #define NPY_FROM_FIELDS (NPY_NEEDS_INIT | NPY_LIST_PICKLE | \
584 NPY_ITEM_REFCOUNT | NPY_NEEDS_PYAPI)
585
586 #define NPY_OBJECT_DTYPE_FLAGS (NPY_LIST_PICKLE | NPY_USE_GETITEM | \
587 NPY_ITEM_IS_POINTER | NPY_ITEM_REFCOUNT | \
588 NPY_NEEDS_INIT | NPY_NEEDS_PYAPI)
589
590 #define PyDataType_FLAGCHK(dtype, flag) \
591 (((dtype)->flags & (flag)) == (flag))
592
593 #define PyDataType_REFCHK(dtype) \
594 PyDataType_FLAGCHK(dtype, NPY_ITEM_REFCOUNT)
595
596 typedef struct _PyArray_Descr {
597 PyObject_HEAD
598 /*
599 * the type object representing an
600 * instance of this type -- should not
601 * be two type_numbers with the same type
602 * object.
603 */
604 PyTypeObject *typeobj;
605 /* kind for this type */
606 char kind;
607 /* unique-character representing this type */
608 char type;
609 /*
610 * '>' (big), '<' (little), '|'
611 * (not-applicable), or '=' (native).
612 */
613 char byteorder;
614 /* flags describing data type */
615 char flags;
616 /* number representing this type */
617 int type_num;
618 /* element size (itemsize) for this type */
619 int elsize;
620 /* alignment needed for this type */
621 int alignment;
622 /*
623 * Non-NULL if this type is
624 * is an array (C-contiguous)
625 * of some other type
626 */
627 struct _arr_descr *subarray;
628 /*
629 * The fields dictionary for this type
630 * For statically defined descr this
631 * is always Py_None
632 */
633 PyObject *fields;
634 /*
635 * An ordered tuple of field names or NULL
636 * if no fields are defined
637 */
638 PyObject *names;
639 /*
640 * a table of functions specific for each
641 * basic data descriptor
642 */
643 PyArray_ArrFuncs *f;
644 /* Metadata about this dtype */
645 PyObject *metadata;
646 /*
647 * Metadata specific to the C implementation
648 * of the particular dtype. This was added
649 * for NumPy 1.7.0.
650 */
651 NpyAuxData *c_metadata;
652 /* Cached hash value (-1 if not yet computed).
653 * This was added for NumPy 2.0.0.
654 */
655 npy_hash_t hash;
656 } PyArray_Descr;
657
658 typedef struct _arr_descr {
659 PyArray_Descr *base;
660 PyObject *shape; /* a tuple */
661 } PyArray_ArrayDescr;
662
663 /*
664 * The main array object structure.
665 *
666 * It has been recommended to use the inline functions defined below
667 * (PyArray_DATA and friends) to access fields here for a number of
668 * releases. Direct access to the members themselves is deprecated.
669 * To ensure that your code does not use deprecated access,
670 * #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
671 * (or NPY_1_8_API_VERSION or higher as required).
672 */
673 /* This struct will be moved to a private header in a future release */
674 typedef struct tagPyArrayObject_fields {
675 PyObject_HEAD
676 /* Pointer to the raw data buffer */
677 char *data;
678 /* The number of dimensions, also called 'ndim' */
679 int nd;
680 /* The size in each dimension, also called 'shape' */
681 npy_intp *dimensions;
682 /*
683 * Number of bytes to jump to get to the
684 * next element in each dimension
685 */
686 npy_intp *strides;
687 /*
688 * This object is decref'd upon
689 * deletion of array. Except in the
690 * case of WRITEBACKIFCOPY which has
691 * special handling.
692 *
693 * For views it points to the original
694 * array, collapsed so no chains of
695 * views occur.
696 *
697 * For creation from buffer object it
698 * points to an object that should be
699 * decref'd on deletion
700 *
701 * For WRITEBACKIFCOPY flag this is an
702 * array to-be-updated upon calling
703 * PyArray_ResolveWritebackIfCopy
704 */
705 PyObject *base;
706 /* Pointer to type structure */
707 PyArray_Descr *descr;
708 /* Flags describing array -- see below */
709 int flags;
710 /* For weak references */
711 PyObject *weakreflist;
712 void *_buffer_info; /* private buffer info, tagged to allow warning */
713 } PyArrayObject_fields;
714
715 /*
716 * To hide the implementation details, we only expose
717 * the Python struct HEAD.
718 */
719 #if !defined(NPY_NO_DEPRECATED_API) || \
720 (NPY_NO_DEPRECATED_API < NPY_1_7_API_VERSION)
721 /*
722 * Can't put this in npy_deprecated_api.h like the others.
723 * PyArrayObject field access is deprecated as of NumPy 1.7.
724 */
725 typedef PyArrayObject_fields PyArrayObject;
726 #else
727 typedef struct tagPyArrayObject {
728 PyObject_HEAD
729 } PyArrayObject;
730 #endif
731
732 /*
733 * Removed 2020-Nov-25, NumPy 1.20
734 * #define NPY_SIZEOF_PYARRAYOBJECT (sizeof(PyArrayObject_fields))
735 *
736 * The above macro was removed as it gave a false sense of a stable ABI
737 * with respect to the structures size. If you require a runtime constant,
738 * you can use `PyArray_Type.tp_basicsize` instead. Otherwise, please
739 * see the PyArrayObject documentation or ask the NumPy developers for
740 * information on how to correctly replace the macro in a way that is
741 * compatible with multiple NumPy versions.
742 */
743
744
745 /* Array Flags Object */
746 typedef struct PyArrayFlagsObject {
747 PyObject_HEAD
748 PyObject *arr;
749 int flags;
750 } PyArrayFlagsObject;
751
752 /* Mirrors buffer object to ptr */
753
754 typedef struct {
755 PyObject_HEAD
756 PyObject *base;
757 void *ptr;
758 npy_intp len;
759 int flags;
760 } PyArray_Chunk;
761
762 typedef struct {
763 NPY_DATETIMEUNIT base;
764 int num;
765 } PyArray_DatetimeMetaData;
766
767 typedef struct {
768 NpyAuxData base;
769 PyArray_DatetimeMetaData meta;
770 } PyArray_DatetimeDTypeMetaData;
771
772 /*
773 * This structure contains an exploded view of a date-time value.
774 * NaT is represented by year == NPY_DATETIME_NAT.
775 */
776 typedef struct {
777 npy_int64 year;
778 npy_int32 month, day, hour, min, sec, us, ps, as;
779 } npy_datetimestruct;
780
781 /* This is not used internally. */
782 typedef struct {
783 npy_int64 day;
784 npy_int32 sec, us, ps, as;
785 } npy_timedeltastruct;
786
787 typedef int (PyArray_FinalizeFunc)(PyArrayObject *, PyObject *);
788
789 /*
790 * Means c-style contiguous (last index varies the fastest). The data
791 * elements right after each other.
792 *
793 * This flag may be requested in constructor functions.
794 * This flag may be tested for in PyArray_FLAGS(arr).
795 */
796 #define NPY_ARRAY_C_CONTIGUOUS 0x0001
797
798 /*
799 * Set if array is a contiguous Fortran array: the first index varies
800 * the fastest in memory (strides array is reverse of C-contiguous
801 * array)
802 *
803 * This flag may be requested in constructor functions.
804 * This flag may be tested for in PyArray_FLAGS(arr).
805 */
806 #define NPY_ARRAY_F_CONTIGUOUS 0x0002
807
808 /*
809 * Note: all 0-d arrays are C_CONTIGUOUS and F_CONTIGUOUS. If a
810 * 1-d array is C_CONTIGUOUS it is also F_CONTIGUOUS. Arrays with
811 * more then one dimension can be C_CONTIGUOUS and F_CONTIGUOUS
812 * at the same time if they have either zero or one element.
813 * If NPY_RELAXED_STRIDES_CHECKING is set, a higher dimensional
814 * array is always C_CONTIGUOUS and F_CONTIGUOUS if it has zero elements
815 * and the array is contiguous if ndarray.squeeze() is contiguous.
816 * I.e. dimensions for which `ndarray.shape[dimension] == 1` are
817 * ignored.
818 */
819
820 /*
821 * If set, the array owns the data: it will be free'd when the array
822 * is deleted.
823 *
824 * This flag may be tested for in PyArray_FLAGS(arr).
825 */
826 #define NPY_ARRAY_OWNDATA 0x0004
827
828 /*
829 * An array never has the next four set; they're only used as parameter
830 * flags to the various FromAny functions
831 *
832 * This flag may be requested in constructor functions.
833 */
834
835 /* Cause a cast to occur regardless of whether or not it is safe. */
836 #define NPY_ARRAY_FORCECAST 0x0010
837
838 /*
839 * Always copy the array. Returned arrays are always CONTIGUOUS,
840 * ALIGNED, and WRITEABLE.
841 *
842 * This flag may be requested in constructor functions.
843 */
844 #define NPY_ARRAY_ENSURECOPY 0x0020
845
846 /*
847 * Make sure the returned array is a base-class ndarray
848 *
849 * This flag may be requested in constructor functions.
850 */
851 #define NPY_ARRAY_ENSUREARRAY 0x0040
852
853 /*
854 * Make sure that the strides are in units of the element size Needed
855 * for some operations with record-arrays.
856 *
857 * This flag may be requested in constructor functions.
858 */
859 #define NPY_ARRAY_ELEMENTSTRIDES 0x0080
860
861 /*
862 * Array data is aligned on the appropriate memory address for the type
863 * stored according to how the compiler would align things (e.g., an
864 * array of integers (4 bytes each) starts on a memory address that's
865 * a multiple of 4)
866 *
867 * This flag may be requested in constructor functions.
868 * This flag may be tested for in PyArray_FLAGS(arr).
869 */
870 #define NPY_ARRAY_ALIGNED 0x0100
871
872 /*
873 * Array data has the native endianness
874 *
875 * This flag may be requested in constructor functions.
876 */
877 #define NPY_ARRAY_NOTSWAPPED 0x0200
878
879 /*
880 * Array data is writeable
881 *
882 * This flag may be requested in constructor functions.
883 * This flag may be tested for in PyArray_FLAGS(arr).
884 */
885 #define NPY_ARRAY_WRITEABLE 0x0400
886
887 /*
888 * If this flag is set, then base contains a pointer to an array of
889 * the same size that should be updated with the current contents of
890 * this array when PyArray_ResolveWritebackIfCopy is called.
891 *
892 * This flag may be requested in constructor functions.
893 * This flag may be tested for in PyArray_FLAGS(arr).
894 */
895 #define NPY_ARRAY_UPDATEIFCOPY 0x1000 /* Deprecated in 1.14 */
896 #define NPY_ARRAY_WRITEBACKIFCOPY 0x2000
897
898 /*
899 * NOTE: there are also internal flags defined in multiarray/arrayobject.h,
900 * which start at bit 31 and work down.
901 */
902
903 #define NPY_ARRAY_BEHAVED (NPY_ARRAY_ALIGNED | \
904 NPY_ARRAY_WRITEABLE)
905 #define NPY_ARRAY_BEHAVED_NS (NPY_ARRAY_ALIGNED | \
906 NPY_ARRAY_WRITEABLE | \
907 NPY_ARRAY_NOTSWAPPED)
908 #define NPY_ARRAY_CARRAY (NPY_ARRAY_C_CONTIGUOUS | \
909 NPY_ARRAY_BEHAVED)
910 #define NPY_ARRAY_CARRAY_RO (NPY_ARRAY_C_CONTIGUOUS | \
911 NPY_ARRAY_ALIGNED)
912 #define NPY_ARRAY_FARRAY (NPY_ARRAY_F_CONTIGUOUS | \
913 NPY_ARRAY_BEHAVED)
914 #define NPY_ARRAY_FARRAY_RO (NPY_ARRAY_F_CONTIGUOUS | \
915 NPY_ARRAY_ALIGNED)
916 #define NPY_ARRAY_DEFAULT (NPY_ARRAY_CARRAY)
917 #define NPY_ARRAY_IN_ARRAY (NPY_ARRAY_CARRAY_RO)
918 #define NPY_ARRAY_OUT_ARRAY (NPY_ARRAY_CARRAY)
919 #define NPY_ARRAY_INOUT_ARRAY (NPY_ARRAY_CARRAY | \
920 NPY_ARRAY_UPDATEIFCOPY)
921 #define NPY_ARRAY_INOUT_ARRAY2 (NPY_ARRAY_CARRAY | \
922 NPY_ARRAY_WRITEBACKIFCOPY)
923 #define NPY_ARRAY_IN_FARRAY (NPY_ARRAY_FARRAY_RO)
924 #define NPY_ARRAY_OUT_FARRAY (NPY_ARRAY_FARRAY)
925 #define NPY_ARRAY_INOUT_FARRAY (NPY_ARRAY_FARRAY | \
926 NPY_ARRAY_UPDATEIFCOPY)
927 #define NPY_ARRAY_INOUT_FARRAY2 (NPY_ARRAY_FARRAY | \
928 NPY_ARRAY_WRITEBACKIFCOPY)
929
930 #define NPY_ARRAY_UPDATE_ALL (NPY_ARRAY_C_CONTIGUOUS | \
931 NPY_ARRAY_F_CONTIGUOUS | \
932 NPY_ARRAY_ALIGNED)
933
934 /* This flag is for the array interface, not PyArrayObject */
935 #define NPY_ARR_HAS_DESCR 0x0800
936
937
938
939
940 /*
941 * Size of internal buffers used for alignment Make BUFSIZE a multiple
942 * of sizeof(npy_cdouble) -- usually 16 so that ufunc buffers are aligned
943 */
944 #define NPY_MIN_BUFSIZE ((int)sizeof(npy_cdouble))
945 #define NPY_MAX_BUFSIZE (((int)sizeof(npy_cdouble))*1000000)
946 #define NPY_BUFSIZE 8192
947 /* buffer stress test size: */
948 /*#define NPY_BUFSIZE 17*/
949
950 #define PyArray_MAX(a,b) (((a)>(b))?(a):(b))
951 #define PyArray_MIN(a,b) (((a)<(b))?(a):(b))
952 #define PyArray_CLT(p,q) ((((p).real==(q).real) ? ((p).imag < (q).imag) : \
953 ((p).real < (q).real)))
954 #define PyArray_CGT(p,q) ((((p).real==(q).real) ? ((p).imag > (q).imag) : \
955 ((p).real > (q).real)))
956 #define PyArray_CLE(p,q) ((((p).real==(q).real) ? ((p).imag <= (q).imag) : \
957 ((p).real <= (q).real)))
958 #define PyArray_CGE(p,q) ((((p).real==(q).real) ? ((p).imag >= (q).imag) : \
959 ((p).real >= (q).real)))
960 #define PyArray_CEQ(p,q) (((p).real==(q).real) && ((p).imag == (q).imag))
961 #define PyArray_CNE(p,q) (((p).real!=(q).real) || ((p).imag != (q).imag))
962
963 /*
964 * C API: consists of Macros and functions. The MACROS are defined
965 * here.
966 */
967
968
969 #define PyArray_ISCONTIGUOUS(m) PyArray_CHKFLAGS((m), NPY_ARRAY_C_CONTIGUOUS)
970 #define PyArray_ISWRITEABLE(m) PyArray_CHKFLAGS((m), NPY_ARRAY_WRITEABLE)
971 #define PyArray_ISALIGNED(m) PyArray_CHKFLAGS((m), NPY_ARRAY_ALIGNED)
972
973 #define PyArray_IS_C_CONTIGUOUS(m) PyArray_CHKFLAGS((m), NPY_ARRAY_C_CONTIGUOUS)
974 #define PyArray_IS_F_CONTIGUOUS(m) PyArray_CHKFLAGS((m), NPY_ARRAY_F_CONTIGUOUS)
975
976 /* the variable is used in some places, so always define it */
977 #define NPY_BEGIN_THREADS_DEF PyThreadState *_save=NULL;
978 #if NPY_ALLOW_THREADS
979 #define NPY_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
980 #define NPY_END_ALLOW_THREADS Py_END_ALLOW_THREADS
981 #define NPY_BEGIN_THREADS do {_save = PyEval_SaveThread();} while (0);
982 #define NPY_END_THREADS do { if (_save) \
983 { PyEval_RestoreThread(_save); _save = NULL;} } while (0);
984 #define NPY_BEGIN_THREADS_THRESHOLDED(loop_size) do { if ((loop_size) > 500) \
985 { _save = PyEval_SaveThread();} } while (0);
986
987 #define NPY_BEGIN_THREADS_DESCR(dtype) \
988 do {if (!(PyDataType_FLAGCHK((dtype), NPY_NEEDS_PYAPI))) \
989 NPY_BEGIN_THREADS;} while (0);
990
991 #define NPY_END_THREADS_DESCR(dtype) \
992 do {if (!(PyDataType_FLAGCHK((dtype), NPY_NEEDS_PYAPI))) \
993 NPY_END_THREADS; } while (0);
994
995 #define NPY_ALLOW_C_API_DEF PyGILState_STATE __save__;
996 #define NPY_ALLOW_C_API do {__save__ = PyGILState_Ensure();} while (0);
997 #define NPY_DISABLE_C_API do {PyGILState_Release(__save__);} while (0);
998 #else
999 #define NPY_BEGIN_ALLOW_THREADS
1000 #define NPY_END_ALLOW_THREADS
1001 #define NPY_BEGIN_THREADS
1002 #define NPY_END_THREADS
1003 #define NPY_BEGIN_THREADS_THRESHOLDED(loop_size)
1004 #define NPY_BEGIN_THREADS_DESCR(dtype)
1005 #define NPY_END_THREADS_DESCR(dtype)
1006 #define NPY_ALLOW_C_API_DEF
1007 #define NPY_ALLOW_C_API
1008 #define NPY_DISABLE_C_API
1009 #endif
1010
1011 /**********************************
1012 * The nditer object, added in 1.6
1013 **********************************/
1014
1015 /* The actual structure of the iterator is an internal detail */
1016 typedef struct NpyIter_InternalOnly NpyIter;
1017
1018 /* Iterator function pointers that may be specialized */
1019 typedef int (NpyIter_IterNextFunc)(NpyIter *iter);
1020 typedef void (NpyIter_GetMultiIndexFunc)(NpyIter *iter,
1021 npy_intp *outcoords);
1022
1023 /*** Global flags that may be passed to the iterator constructors ***/
1024
1025 /* Track an index representing C order */
1026 #define NPY_ITER_C_INDEX 0x00000001
1027 /* Track an index representing Fortran order */
1028 #define NPY_ITER_F_INDEX 0x00000002
1029 /* Track a multi-index */
1030 #define NPY_ITER_MULTI_INDEX 0x00000004
1031 /* User code external to the iterator does the 1-dimensional innermost loop */
1032 #define NPY_ITER_EXTERNAL_LOOP 0x00000008
1033 /* Convert all the operands to a common data type */
1034 #define NPY_ITER_COMMON_DTYPE 0x00000010
1035 /* Operands may hold references, requiring API access during iteration */
1036 #define NPY_ITER_REFS_OK 0x00000020
1037 /* Zero-sized operands should be permitted, iteration checks IterSize for 0 */
1038 #define NPY_ITER_ZEROSIZE_OK 0x00000040
1039 /* Permits reductions (size-0 stride with dimension size > 1) */
1040 #define NPY_ITER_REDUCE_OK 0x00000080
1041 /* Enables sub-range iteration */
1042 #define NPY_ITER_RANGED 0x00000100
1043 /* Enables buffering */
1044 #define NPY_ITER_BUFFERED 0x00000200
1045 /* When buffering is enabled, grows the inner loop if possible */
1046 #define NPY_ITER_GROWINNER 0x00000400
1047 /* Delay allocation of buffers until first Reset* call */
1048 #define NPY_ITER_DELAY_BUFALLOC 0x00000800
1049 /* When NPY_KEEPORDER is specified, disable reversing negative-stride axes */
1050 #define NPY_ITER_DONT_NEGATE_STRIDES 0x00001000
1051 /*
1052 * If output operands overlap with other operands (based on heuristics that
1053 * has false positives but no false negatives), make temporary copies to
1054 * eliminate overlap.
1055 */
1056 #define NPY_ITER_COPY_IF_OVERLAP 0x00002000
1057
1058 /*** Per-operand flags that may be passed to the iterator constructors ***/
1059
1060 /* The operand will be read from and written to */
1061 #define NPY_ITER_READWRITE 0x00010000
1062 /* The operand will only be read from */
1063 #define NPY_ITER_READONLY 0x00020000
1064 /* The operand will only be written to */
1065 #define NPY_ITER_WRITEONLY 0x00040000
1066 /* The operand's data must be in native byte order */
1067 #define NPY_ITER_NBO 0x00080000
1068 /* The operand's data must be aligned */
1069 #define NPY_ITER_ALIGNED 0x00100000
1070 /* The operand's data must be contiguous (within the inner loop) */
1071 #define NPY_ITER_CONTIG 0x00200000
1072 /* The operand may be copied to satisfy requirements */
1073 #define NPY_ITER_COPY 0x00400000
1074 /* The operand may be copied with WRITEBACKIFCOPY to satisfy requirements */
1075 #define NPY_ITER_UPDATEIFCOPY 0x00800000
1076 /* Allocate the operand if it is NULL */
1077 #define NPY_ITER_ALLOCATE 0x01000000
1078 /* If an operand is allocated, don't use any subtype */
1079 #define NPY_ITER_NO_SUBTYPE 0x02000000
1080 /* This is a virtual array slot, operand is NULL but temporary data is there */
1081 #define NPY_ITER_VIRTUAL 0x04000000
1082 /* Require that the dimension match the iterator dimensions exactly */
1083 #define NPY_ITER_NO_BROADCAST 0x08000000
1084 /* A mask is being used on this array, affects buffer -> array copy */
1085 #define NPY_ITER_WRITEMASKED 0x10000000
1086 /* This array is the mask for all WRITEMASKED operands */
1087 #define NPY_ITER_ARRAYMASK 0x20000000
1088 /* Assume iterator order data access for COPY_IF_OVERLAP */
1089 #define NPY_ITER_OVERLAP_ASSUME_ELEMENTWISE 0x40000000
1090
1091 #define NPY_ITER_GLOBAL_FLAGS 0x0000ffff
1092 #define NPY_ITER_PER_OP_FLAGS 0xffff0000
1093
1094
1095 /*****************************
1096 * Basic iterator object
1097 *****************************/
1098
1099 /* FWD declaration */
1100 typedef struct PyArrayIterObject_tag PyArrayIterObject;
1101
1102 /*
1103 * type of the function which translates a set of coordinates to a
1104 * pointer to the data
1105 */
1106 typedef char* (*npy_iter_get_dataptr_t)(
1107 PyArrayIterObject* iter, const npy_intp*);
1108
1109 struct PyArrayIterObject_tag {
1110 PyObject_HEAD
1111 int nd_m1; /* number of dimensions - 1 */
1112 npy_intp index, size;
1113 npy_intp coordinates[NPY_MAXDIMS];/* N-dimensional loop */
1114 npy_intp dims_m1[NPY_MAXDIMS]; /* ao->dimensions - 1 */
1115 npy_intp strides[NPY_MAXDIMS]; /* ao->strides or fake */
1116 npy_intp backstrides[NPY_MAXDIMS];/* how far to jump back */
1117 npy_intp factors[NPY_MAXDIMS]; /* shape factors */
1118 PyArrayObject *ao;
1119 char *dataptr; /* pointer to current item*/
1120 npy_bool contiguous;
1121
1122 npy_intp bounds[NPY_MAXDIMS][2];
1123 npy_intp limits[NPY_MAXDIMS][2];
1124 npy_intp limits_sizes[NPY_MAXDIMS];
1125 npy_iter_get_dataptr_t translate;
1126 } ;
1127
1128
1129 /* Iterator API */
1130 #define PyArrayIter_Check(op) PyObject_TypeCheck((op), &PyArrayIter_Type)
1131
1132 #define _PyAIT(it) ((PyArrayIterObject *)(it))
1133 #define PyArray_ITER_RESET(it) do { \
1134 _PyAIT(it)->index = 0; \
1135 _PyAIT(it)->dataptr = PyArray_BYTES(_PyAIT(it)->ao); \
1136 memset(_PyAIT(it)->coordinates, 0, \
1137 (_PyAIT(it)->nd_m1+1)*sizeof(npy_intp)); \
1138 } while (0)
1139
1140 #define _PyArray_ITER_NEXT1(it) do { \
1141 (it)->dataptr += _PyAIT(it)->strides[0]; \
1142 (it)->coordinates[0]++; \
1143 } while (0)
1144
1145 #define _PyArray_ITER_NEXT2(it) do { \
1146 if ((it)->coordinates[1] < (it)->dims_m1[1]) { \
1147 (it)->coordinates[1]++; \
1148 (it)->dataptr += (it)->strides[1]; \
1149 } \
1150 else { \
1151 (it)->coordinates[1] = 0; \
1152 (it)->coordinates[0]++; \
1153 (it)->dataptr += (it)->strides[0] - \
1154 (it)->backstrides[1]; \
1155 } \
1156 } while (0)
1157
1158 #define PyArray_ITER_NEXT(it) do { \
1159 _PyAIT(it)->index++; \
1160 if (_PyAIT(it)->nd_m1 == 0) { \
1161 _PyArray_ITER_NEXT1(_PyAIT(it)); \
1162 } \
1163 else if (_PyAIT(it)->contiguous) \
1164 _PyAIT(it)->dataptr += PyArray_DESCR(_PyAIT(it)->ao)->elsize; \
1165 else if (_PyAIT(it)->nd_m1 == 1) { \
1166 _PyArray_ITER_NEXT2(_PyAIT(it)); \
1167 } \
1168 else { \
1169 int __npy_i; \
1170 for (__npy_i=_PyAIT(it)->nd_m1; __npy_i >= 0; __npy_i--) { \
1171 if (_PyAIT(it)->coordinates[__npy_i] < \
1172 _PyAIT(it)->dims_m1[__npy_i]) { \
1173 _PyAIT(it)->coordinates[__npy_i]++; \
1174 _PyAIT(it)->dataptr += \
1175 _PyAIT(it)->strides[__npy_i]; \
1176 break; \
1177 } \
1178 else { \
1179 _PyAIT(it)->coordinates[__npy_i] = 0; \
1180 _PyAIT(it)->dataptr -= \
1181 _PyAIT(it)->backstrides[__npy_i]; \
1182 } \
1183 } \
1184 } \
1185 } while (0)
1186
1187 #define PyArray_ITER_GOTO(it, destination) do { \
1188 int __npy_i; \
1189 _PyAIT(it)->index = 0; \
1190 _PyAIT(it)->dataptr = PyArray_BYTES(_PyAIT(it)->ao); \
1191 for (__npy_i = _PyAIT(it)->nd_m1; __npy_i>=0; __npy_i--) { \
1192 if (destination[__npy_i] < 0) { \
1193 destination[__npy_i] += \
1194 _PyAIT(it)->dims_m1[__npy_i]+1; \
1195 } \
1196 _PyAIT(it)->dataptr += destination[__npy_i] * \
1197 _PyAIT(it)->strides[__npy_i]; \
1198 _PyAIT(it)->coordinates[__npy_i] = \
1199 destination[__npy_i]; \
1200 _PyAIT(it)->index += destination[__npy_i] * \
1201 ( __npy_i==_PyAIT(it)->nd_m1 ? 1 : \
1202 _PyAIT(it)->dims_m1[__npy_i+1]+1) ; \
1203 } \
1204 } while (0)
1205
1206 #define PyArray_ITER_GOTO1D(it, ind) do { \
1207 int __npy_i; \
1208 npy_intp __npy_ind = (npy_intp)(ind); \
1209 if (__npy_ind < 0) __npy_ind += _PyAIT(it)->size; \
1210 _PyAIT(it)->index = __npy_ind; \
1211 if (_PyAIT(it)->nd_m1 == 0) { \
1212 _PyAIT(it)->dataptr = PyArray_BYTES(_PyAIT(it)->ao) + \
1213 __npy_ind * _PyAIT(it)->strides[0]; \
1214 } \
1215 else if (_PyAIT(it)->contiguous) \
1216 _PyAIT(it)->dataptr = PyArray_BYTES(_PyAIT(it)->ao) + \
1217 __npy_ind * PyArray_DESCR(_PyAIT(it)->ao)->elsize; \
1218 else { \
1219 _PyAIT(it)->dataptr = PyArray_BYTES(_PyAIT(it)->ao); \
1220 for (__npy_i = 0; __npy_i<=_PyAIT(it)->nd_m1; \
1221 __npy_i++) { \
1222 _PyAIT(it)->dataptr += \
1223 (__npy_ind / _PyAIT(it)->factors[__npy_i]) \
1224 * _PyAIT(it)->strides[__npy_i]; \
1225 __npy_ind %= _PyAIT(it)->factors[__npy_i]; \
1226 } \
1227 } \
1228 } while (0)
1229
1230 #define PyArray_ITER_DATA(it) ((void *)(_PyAIT(it)->dataptr))
1231
1232 #define PyArray_ITER_NOTDONE(it) (_PyAIT(it)->index < _PyAIT(it)->size)
1233
1234
1235 /*
1236 * Any object passed to PyArray_Broadcast must be binary compatible
1237 * with this structure.
1238 */
1239
1240 typedef struct {
1241 PyObject_HEAD
1242 int numiter; /* number of iters */
1243 npy_intp size; /* broadcasted size */
1244 npy_intp index; /* current index */
1245 int nd; /* number of dims */
1246 npy_intp dimensions[NPY_MAXDIMS]; /* dimensions */
1247 PyArrayIterObject *iters[NPY_MAXARGS]; /* iterators */
1248 } PyArrayMultiIterObject;
1249
1250 #define _PyMIT(m) ((PyArrayMultiIterObject *)(m))
1251 #define PyArray_MultiIter_RESET(multi) do { \
1252 int __npy_mi; \
1253 _PyMIT(multi)->index = 0; \
1254 for (__npy_mi=0; __npy_mi < _PyMIT(multi)->numiter; __npy_mi++) { \
1255 PyArray_ITER_RESET(_PyMIT(multi)->iters[__npy_mi]); \
1256 } \
1257 } while (0)
1258
1259 #define PyArray_MultiIter_NEXT(multi) do { \
1260 int __npy_mi; \
1261 _PyMIT(multi)->index++; \
1262 for (__npy_mi=0; __npy_mi < _PyMIT(multi)->numiter; __npy_mi++) { \
1263 PyArray_ITER_NEXT(_PyMIT(multi)->iters[__npy_mi]); \
1264 } \
1265 } while (0)
1266
1267 #define PyArray_MultiIter_GOTO(multi, dest) do { \
1268 int __npy_mi; \
1269 for (__npy_mi=0; __npy_mi < _PyMIT(multi)->numiter; __npy_mi++) { \
1270 PyArray_ITER_GOTO(_PyMIT(multi)->iters[__npy_mi], dest); \
1271 } \
1272 _PyMIT(multi)->index = _PyMIT(multi)->iters[0]->index; \
1273 } while (0)
1274
1275 #define PyArray_MultiIter_GOTO1D(multi, ind) do { \
1276 int __npy_mi; \
1277 for (__npy_mi=0; __npy_mi < _PyMIT(multi)->numiter; __npy_mi++) { \
1278 PyArray_ITER_GOTO1D(_PyMIT(multi)->iters[__npy_mi], ind); \
1279 } \
1280 _PyMIT(multi)->index = _PyMIT(multi)->iters[0]->index; \
1281 } while (0)
1282
1283 #define PyArray_MultiIter_DATA(multi, i) \
1284 ((void *)(_PyMIT(multi)->iters[i]->dataptr))
1285
1286 #define PyArray_MultiIter_NEXTi(multi, i) \
1287 PyArray_ITER_NEXT(_PyMIT(multi)->iters[i])
1288
1289 #define PyArray_MultiIter_NOTDONE(multi) \
1290 (_PyMIT(multi)->index < _PyMIT(multi)->size)
1291
1292
1293 /*
1294 * Store the information needed for fancy-indexing over an array. The
1295 * fields are slightly unordered to keep consec, dataptr and subspace
1296 * where they were originally.
1297 */
1298 typedef struct {
1299 PyObject_HEAD
1300 /*
1301 * Multi-iterator portion --- needs to be present in this
1302 * order to work with PyArray_Broadcast
1303 */
1304
1305 int numiter; /* number of index-array
1306 iterators */
1307 npy_intp size; /* size of broadcasted
1308 result */
1309 npy_intp index; /* current index */
1310 int nd; /* number of dims */
1311 npy_intp dimensions[NPY_MAXDIMS]; /* dimensions */
1312 NpyIter *outer; /* index objects
1313 iterator */
1314 void *unused[NPY_MAXDIMS - 2];
1315 PyArrayObject *array;
1316 /* Flat iterator for the indexed array. For compatibility solely. */
1317 PyArrayIterObject *ait;
1318
1319 /*
1320 * Subspace array. For binary compatibility (was an iterator,
1321 * but only the check for NULL should be used).
1322 */
1323 PyArrayObject *subspace;
1324
1325 /*
1326 * if subspace iteration, then this is the array of axes in
1327 * the underlying array represented by the index objects
1328 */
1329 int iteraxes[NPY_MAXDIMS];
1330 npy_intp fancy_strides[NPY_MAXDIMS];
1331
1332 /* pointer when all fancy indices are 0 */
1333 char *baseoffset;
1334
1335 /*
1336 * after binding consec denotes at which axis the fancy axes
1337 * are inserted.
1338 */
1339 int consec;
1340 char *dataptr;
1341
1342 int nd_fancy;
1343 npy_intp fancy_dims[NPY_MAXDIMS];
1344
1345 /* Whether the iterator (any of the iterators) requires API */
1346 int needs_api;
1347
1348 /*
1349 * Extra op information.
1350 */
1351 PyArrayObject *extra_op;
1352 PyArray_Descr *extra_op_dtype; /* desired dtype */
1353 npy_uint32 *extra_op_flags; /* Iterator flags */
1354
1355 NpyIter *extra_op_iter;
1356 NpyIter_IterNextFunc *extra_op_next;
1357 char **extra_op_ptrs;
1358
1359 /*
1360 * Information about the iteration state.
1361 */
1362 NpyIter_IterNextFunc *outer_next;
1363 char **outer_ptrs;
1364 npy_intp *outer_strides;
1365
1366 /*
1367 * Information about the subspace iterator.
1368 */
1369 NpyIter *subspace_iter;
1370 NpyIter_IterNextFunc *subspace_next;
1371 char **subspace_ptrs;
1372 npy_intp *subspace_strides;
1373
1374 /* Count for the external loop (which ever it is) for API iteration */
1375 npy_intp iter_count;
1376
1377 } PyArrayMapIterObject;
1378
1379 enum {
1380 NPY_NEIGHBORHOOD_ITER_ZERO_PADDING,
1381 NPY_NEIGHBORHOOD_ITER_ONE_PADDING,
1382 NPY_NEIGHBORHOOD_ITER_CONSTANT_PADDING,
1383 NPY_NEIGHBORHOOD_ITER_CIRCULAR_PADDING,
1384 NPY_NEIGHBORHOOD_ITER_MIRROR_PADDING
1385 };
1386
1387 typedef struct {
1388 PyObject_HEAD
1389
1390 /*
1391 * PyArrayIterObject part: keep this in this exact order
1392 */
1393 int nd_m1; /* number of dimensions - 1 */
1394 npy_intp index, size;
1395 npy_intp coordinates[NPY_MAXDIMS];/* N-dimensional loop */
1396 npy_intp dims_m1[NPY_MAXDIMS]; /* ao->dimensions - 1 */
1397 npy_intp strides[NPY_MAXDIMS]; /* ao->strides or fake */
1398 npy_intp backstrides[NPY_MAXDIMS];/* how far to jump back */
1399 npy_intp factors[NPY_MAXDIMS]; /* shape factors */
1400 PyArrayObject *ao;
1401 char *dataptr; /* pointer to current item*/
1402 npy_bool contiguous;
1403
1404 npy_intp bounds[NPY_MAXDIMS][2];
1405 npy_intp limits[NPY_MAXDIMS][2];
1406 npy_intp limits_sizes[NPY_MAXDIMS];
1407 npy_iter_get_dataptr_t translate;
1408
1409 /*
1410 * New members
1411 */
1412 npy_intp nd;
1413
1414 /* Dimensions is the dimension of the array */
1415 npy_intp dimensions[NPY_MAXDIMS];
1416
1417 /*
1418 * Neighborhood points coordinates are computed relatively to the
1419 * point pointed by _internal_iter
1420 */
1421 PyArrayIterObject* _internal_iter;
1422 /*
1423 * To keep a reference to the representation of the constant value
1424 * for constant padding
1425 */
1426 char* constant;
1427
1428 int mode;
1429 } PyArrayNeighborhoodIterObject;
1430
1431 /*
1432 * Neighborhood iterator API
1433 */
1434
1435 /* General: those work for any mode */
1436 static NPY_INLINE int
1437 PyArrayNeighborhoodIter_Reset(PyArrayNeighborhoodIterObject* iter);
1438 static NPY_INLINE int
1439 PyArrayNeighborhoodIter_Next(PyArrayNeighborhoodIterObject* iter);
1440 #if 0
1441 static NPY_INLINE int
1442 PyArrayNeighborhoodIter_Next2D(PyArrayNeighborhoodIterObject* iter);
1443 #endif
1444
1445 /*
1446 * Include inline implementations - functions defined there are not
1447 * considered public API
1448 */
1449 #define _NPY_INCLUDE_NEIGHBORHOOD_IMP
1450 #include "_neighborhood_iterator_imp.h"
1451 #undef _NPY_INCLUDE_NEIGHBORHOOD_IMP
1452
1453 /* The default array type */
1454 #define NPY_DEFAULT_TYPE NPY_DOUBLE
1455
1456 /*
1457 * All sorts of useful ways to look into a PyArrayObject. It is recommended
1458 * to use PyArrayObject * objects instead of always casting from PyObject *,
1459 * for improved type checking.
1460 *
1461 * In many cases here the macro versions of the accessors are deprecated,
1462 * but can't be immediately changed to inline functions because the
1463 * preexisting macros accept PyObject * and do automatic casts. Inline
1464 * functions accepting PyArrayObject * provides for some compile-time
1465 * checking of correctness when working with these objects in C.
1466 */
1467
1468 #define PyArray_ISONESEGMENT(m) (PyArray_CHKFLAGS(m, NPY_ARRAY_C_CONTIGUOUS) || \
1469 PyArray_CHKFLAGS(m, NPY_ARRAY_F_CONTIGUOUS))
1470
1471 #define PyArray_ISFORTRAN(m) (PyArray_CHKFLAGS(m, NPY_ARRAY_F_CONTIGUOUS) && \
1472 (!PyArray_CHKFLAGS(m, NPY_ARRAY_C_CONTIGUOUS)))
1473
1474 #define PyArray_FORTRAN_IF(m) ((PyArray_CHKFLAGS(m, NPY_ARRAY_F_CONTIGUOUS) ? \
1475 NPY_ARRAY_F_CONTIGUOUS : 0))
1476
1477 #if (defined(NPY_NO_DEPRECATED_API) && (NPY_1_7_API_VERSION <= NPY_NO_DEPRECATED_API))
1478 /*
1479 * Changing access macros into functions, to allow for future hiding
1480 * of the internal memory layout. This later hiding will allow the 2.x series
1481 * to change the internal representation of arrays without affecting
1482 * ABI compatibility.
1483 */
1484
1485 static NPY_INLINE int
PyArray_NDIM(const PyArrayObject * arr)1486 PyArray_NDIM(const PyArrayObject *arr)
1487 {
1488 return ((PyArrayObject_fields *)arr)->nd;
1489 }
1490
1491 static NPY_INLINE void *
PyArray_DATA(PyArrayObject * arr)1492 PyArray_DATA(PyArrayObject *arr)
1493 {
1494 return ((PyArrayObject_fields *)arr)->data;
1495 }
1496
1497 static NPY_INLINE char *
PyArray_BYTES(PyArrayObject * arr)1498 PyArray_BYTES(PyArrayObject *arr)
1499 {
1500 return ((PyArrayObject_fields *)arr)->data;
1501 }
1502
1503 static NPY_INLINE npy_intp *
PyArray_DIMS(PyArrayObject * arr)1504 PyArray_DIMS(PyArrayObject *arr)
1505 {
1506 return ((PyArrayObject_fields *)arr)->dimensions;
1507 }
1508
1509 static NPY_INLINE npy_intp *
PyArray_STRIDES(PyArrayObject * arr)1510 PyArray_STRIDES(PyArrayObject *arr)
1511 {
1512 return ((PyArrayObject_fields *)arr)->strides;
1513 }
1514
1515 static NPY_INLINE npy_intp
PyArray_DIM(const PyArrayObject * arr,int idim)1516 PyArray_DIM(const PyArrayObject *arr, int idim)
1517 {
1518 return ((PyArrayObject_fields *)arr)->dimensions[idim];
1519 }
1520
1521 static NPY_INLINE npy_intp
PyArray_STRIDE(const PyArrayObject * arr,int istride)1522 PyArray_STRIDE(const PyArrayObject *arr, int istride)
1523 {
1524 return ((PyArrayObject_fields *)arr)->strides[istride];
1525 }
1526
1527 static NPY_INLINE NPY_RETURNS_BORROWED_REF PyObject *
PyArray_BASE(PyArrayObject * arr)1528 PyArray_BASE(PyArrayObject *arr)
1529 {
1530 return ((PyArrayObject_fields *)arr)->base;
1531 }
1532
1533 static NPY_INLINE NPY_RETURNS_BORROWED_REF PyArray_Descr *
PyArray_DESCR(PyArrayObject * arr)1534 PyArray_DESCR(PyArrayObject *arr)
1535 {
1536 return ((PyArrayObject_fields *)arr)->descr;
1537 }
1538
1539 static NPY_INLINE int
PyArray_FLAGS(const PyArrayObject * arr)1540 PyArray_FLAGS(const PyArrayObject *arr)
1541 {
1542 return ((PyArrayObject_fields *)arr)->flags;
1543 }
1544
1545 static NPY_INLINE npy_intp
PyArray_ITEMSIZE(const PyArrayObject * arr)1546 PyArray_ITEMSIZE(const PyArrayObject *arr)
1547 {
1548 return ((PyArrayObject_fields *)arr)->descr->elsize;
1549 }
1550
1551 static NPY_INLINE int
PyArray_TYPE(const PyArrayObject * arr)1552 PyArray_TYPE(const PyArrayObject *arr)
1553 {
1554 return ((PyArrayObject_fields *)arr)->descr->type_num;
1555 }
1556
1557 static NPY_INLINE int
PyArray_CHKFLAGS(const PyArrayObject * arr,int flags)1558 PyArray_CHKFLAGS(const PyArrayObject *arr, int flags)
1559 {
1560 return (PyArray_FLAGS(arr) & flags) == flags;
1561 }
1562
1563 static NPY_INLINE PyObject *
PyArray_GETITEM(const PyArrayObject * arr,const char * itemptr)1564 PyArray_GETITEM(const PyArrayObject *arr, const char *itemptr)
1565 {
1566 return ((PyArrayObject_fields *)arr)->descr->f->getitem(
1567 (void *)itemptr, (PyArrayObject *)arr);
1568 }
1569
1570 /*
1571 * SETITEM should only be used if it is known that the value is a scalar
1572 * and of a type understood by the arrays dtype.
1573 * Use `PyArray_Pack` if the value may be of a different dtype.
1574 */
1575 static NPY_INLINE int
PyArray_SETITEM(PyArrayObject * arr,char * itemptr,PyObject * v)1576 PyArray_SETITEM(PyArrayObject *arr, char *itemptr, PyObject *v)
1577 {
1578 return ((PyArrayObject_fields *)arr)->descr->f->setitem(v, itemptr, arr);
1579 }
1580
1581 #else
1582
1583 /* These macros are deprecated as of NumPy 1.7. */
1584 #define PyArray_NDIM(obj) (((PyArrayObject_fields *)(obj))->nd)
1585 #define PyArray_BYTES(obj) (((PyArrayObject_fields *)(obj))->data)
1586 #define PyArray_DATA(obj) ((void *)((PyArrayObject_fields *)(obj))->data)
1587 #define PyArray_DIMS(obj) (((PyArrayObject_fields *)(obj))->dimensions)
1588 #define PyArray_STRIDES(obj) (((PyArrayObject_fields *)(obj))->strides)
1589 #define PyArray_DIM(obj,n) (PyArray_DIMS(obj)[n])
1590 #define PyArray_STRIDE(obj,n) (PyArray_STRIDES(obj)[n])
1591 #define PyArray_BASE(obj) (((PyArrayObject_fields *)(obj))->base)
1592 #define PyArray_DESCR(obj) (((PyArrayObject_fields *)(obj))->descr)
1593 #define PyArray_FLAGS(obj) (((PyArrayObject_fields *)(obj))->flags)
1594 #define PyArray_CHKFLAGS(m, FLAGS) \
1595 ((((PyArrayObject_fields *)(m))->flags & (FLAGS)) == (FLAGS))
1596 #define PyArray_ITEMSIZE(obj) \
1597 (((PyArrayObject_fields *)(obj))->descr->elsize)
1598 #define PyArray_TYPE(obj) \
1599 (((PyArrayObject_fields *)(obj))->descr->type_num)
1600 #define PyArray_GETITEM(obj,itemptr) \
1601 PyArray_DESCR(obj)->f->getitem((char *)(itemptr), \
1602 (PyArrayObject *)(obj))
1603
1604 #define PyArray_SETITEM(obj,itemptr,v) \
1605 PyArray_DESCR(obj)->f->setitem((PyObject *)(v), \
1606 (char *)(itemptr), \
1607 (PyArrayObject *)(obj))
1608 #endif
1609
1610 static NPY_INLINE PyArray_Descr *
PyArray_DTYPE(PyArrayObject * arr)1611 PyArray_DTYPE(PyArrayObject *arr)
1612 {
1613 return ((PyArrayObject_fields *)arr)->descr;
1614 }
1615
1616 static NPY_INLINE npy_intp *
PyArray_SHAPE(PyArrayObject * arr)1617 PyArray_SHAPE(PyArrayObject *arr)
1618 {
1619 return ((PyArrayObject_fields *)arr)->dimensions;
1620 }
1621
1622 /*
1623 * Enables the specified array flags. Does no checking,
1624 * assumes you know what you're doing.
1625 */
1626 static NPY_INLINE void
PyArray_ENABLEFLAGS(PyArrayObject * arr,int flags)1627 PyArray_ENABLEFLAGS(PyArrayObject *arr, int flags)
1628 {
1629 ((PyArrayObject_fields *)arr)->flags |= flags;
1630 }
1631
1632 /*
1633 * Clears the specified array flags. Does no checking,
1634 * assumes you know what you're doing.
1635 */
1636 static NPY_INLINE void
PyArray_CLEARFLAGS(PyArrayObject * arr,int flags)1637 PyArray_CLEARFLAGS(PyArrayObject *arr, int flags)
1638 {
1639 ((PyArrayObject_fields *)arr)->flags &= ~flags;
1640 }
1641
1642 #define PyTypeNum_ISBOOL(type) ((type) == NPY_BOOL)
1643
1644 #define PyTypeNum_ISUNSIGNED(type) (((type) == NPY_UBYTE) || \
1645 ((type) == NPY_USHORT) || \
1646 ((type) == NPY_UINT) || \
1647 ((type) == NPY_ULONG) || \
1648 ((type) == NPY_ULONGLONG))
1649
1650 #define PyTypeNum_ISSIGNED(type) (((type) == NPY_BYTE) || \
1651 ((type) == NPY_SHORT) || \
1652 ((type) == NPY_INT) || \
1653 ((type) == NPY_LONG) || \
1654 ((type) == NPY_LONGLONG))
1655
1656 #define PyTypeNum_ISINTEGER(type) (((type) >= NPY_BYTE) && \
1657 ((type) <= NPY_ULONGLONG))
1658
1659 #define PyTypeNum_ISFLOAT(type) ((((type) >= NPY_FLOAT) && \
1660 ((type) <= NPY_LONGDOUBLE)) || \
1661 ((type) == NPY_HALF))
1662
1663 #define PyTypeNum_ISNUMBER(type) (((type) <= NPY_CLONGDOUBLE) || \
1664 ((type) == NPY_HALF))
1665
1666 #define PyTypeNum_ISSTRING(type) (((type) == NPY_STRING) || \
1667 ((type) == NPY_UNICODE))
1668
1669 #define PyTypeNum_ISCOMPLEX(type) (((type) >= NPY_CFLOAT) && \
1670 ((type) <= NPY_CLONGDOUBLE))
1671
1672 #define PyTypeNum_ISPYTHON(type) (((type) == NPY_LONG) || \
1673 ((type) == NPY_DOUBLE) || \
1674 ((type) == NPY_CDOUBLE) || \
1675 ((type) == NPY_BOOL) || \
1676 ((type) == NPY_OBJECT ))
1677
1678 #define PyTypeNum_ISFLEXIBLE(type) (((type) >=NPY_STRING) && \
1679 ((type) <=NPY_VOID))
1680
1681 #define PyTypeNum_ISDATETIME(type) (((type) >=NPY_DATETIME) && \
1682 ((type) <=NPY_TIMEDELTA))
1683
1684 #define PyTypeNum_ISUSERDEF(type) (((type) >= NPY_USERDEF) && \
1685 ((type) < NPY_USERDEF+ \
1686 NPY_NUMUSERTYPES))
1687
1688 #define PyTypeNum_ISEXTENDED(type) (PyTypeNum_ISFLEXIBLE(type) || \
1689 PyTypeNum_ISUSERDEF(type))
1690
1691 #define PyTypeNum_ISOBJECT(type) ((type) == NPY_OBJECT)
1692
1693
1694 #define PyDataType_ISBOOL(obj) PyTypeNum_ISBOOL(((PyArray_Descr*)(obj))->type_num)
1695 #define PyDataType_ISUNSIGNED(obj) PyTypeNum_ISUNSIGNED(((PyArray_Descr*)(obj))->type_num)
1696 #define PyDataType_ISSIGNED(obj) PyTypeNum_ISSIGNED(((PyArray_Descr*)(obj))->type_num)
1697 #define PyDataType_ISINTEGER(obj) PyTypeNum_ISINTEGER(((PyArray_Descr*)(obj))->type_num )
1698 #define PyDataType_ISFLOAT(obj) PyTypeNum_ISFLOAT(((PyArray_Descr*)(obj))->type_num)
1699 #define PyDataType_ISNUMBER(obj) PyTypeNum_ISNUMBER(((PyArray_Descr*)(obj))->type_num)
1700 #define PyDataType_ISSTRING(obj) PyTypeNum_ISSTRING(((PyArray_Descr*)(obj))->type_num)
1701 #define PyDataType_ISCOMPLEX(obj) PyTypeNum_ISCOMPLEX(((PyArray_Descr*)(obj))->type_num)
1702 #define PyDataType_ISPYTHON(obj) PyTypeNum_ISPYTHON(((PyArray_Descr*)(obj))->type_num)
1703 #define PyDataType_ISFLEXIBLE(obj) PyTypeNum_ISFLEXIBLE(((PyArray_Descr*)(obj))->type_num)
1704 #define PyDataType_ISDATETIME(obj) PyTypeNum_ISDATETIME(((PyArray_Descr*)(obj))->type_num)
1705 #define PyDataType_ISUSERDEF(obj) PyTypeNum_ISUSERDEF(((PyArray_Descr*)(obj))->type_num)
1706 #define PyDataType_ISEXTENDED(obj) PyTypeNum_ISEXTENDED(((PyArray_Descr*)(obj))->type_num)
1707 #define PyDataType_ISOBJECT(obj) PyTypeNum_ISOBJECT(((PyArray_Descr*)(obj))->type_num)
1708 #define PyDataType_HASFIELDS(obj) (((PyArray_Descr *)(obj))->names != NULL)
1709 #define PyDataType_HASSUBARRAY(dtype) ((dtype)->subarray != NULL)
1710 #define PyDataType_ISUNSIZED(dtype) ((dtype)->elsize == 0 && \
1711 !PyDataType_HASFIELDS(dtype))
1712 #define PyDataType_MAKEUNSIZED(dtype) ((dtype)->elsize = 0)
1713
1714 #define PyArray_ISBOOL(obj) PyTypeNum_ISBOOL(PyArray_TYPE(obj))
1715 #define PyArray_ISUNSIGNED(obj) PyTypeNum_ISUNSIGNED(PyArray_TYPE(obj))
1716 #define PyArray_ISSIGNED(obj) PyTypeNum_ISSIGNED(PyArray_TYPE(obj))
1717 #define PyArray_ISINTEGER(obj) PyTypeNum_ISINTEGER(PyArray_TYPE(obj))
1718 #define PyArray_ISFLOAT(obj) PyTypeNum_ISFLOAT(PyArray_TYPE(obj))
1719 #define PyArray_ISNUMBER(obj) PyTypeNum_ISNUMBER(PyArray_TYPE(obj))
1720 #define PyArray_ISSTRING(obj) PyTypeNum_ISSTRING(PyArray_TYPE(obj))
1721 #define PyArray_ISCOMPLEX(obj) PyTypeNum_ISCOMPLEX(PyArray_TYPE(obj))
1722 #define PyArray_ISPYTHON(obj) PyTypeNum_ISPYTHON(PyArray_TYPE(obj))
1723 #define PyArray_ISFLEXIBLE(obj) PyTypeNum_ISFLEXIBLE(PyArray_TYPE(obj))
1724 #define PyArray_ISDATETIME(obj) PyTypeNum_ISDATETIME(PyArray_TYPE(obj))
1725 #define PyArray_ISUSERDEF(obj) PyTypeNum_ISUSERDEF(PyArray_TYPE(obj))
1726 #define PyArray_ISEXTENDED(obj) PyTypeNum_ISEXTENDED(PyArray_TYPE(obj))
1727 #define PyArray_ISOBJECT(obj) PyTypeNum_ISOBJECT(PyArray_TYPE(obj))
1728 #define PyArray_HASFIELDS(obj) PyDataType_HASFIELDS(PyArray_DESCR(obj))
1729
1730 /*
1731 * FIXME: This should check for a flag on the data-type that
1732 * states whether or not it is variable length. Because the
1733 * ISFLEXIBLE check is hard-coded to the built-in data-types.
1734 */
1735 #define PyArray_ISVARIABLE(obj) PyTypeNum_ISFLEXIBLE(PyArray_TYPE(obj))
1736
1737 #define PyArray_SAFEALIGNEDCOPY(obj) (PyArray_ISALIGNED(obj) && !PyArray_ISVARIABLE(obj))
1738
1739
1740 #define NPY_LITTLE '<'
1741 #define NPY_BIG '>'
1742 #define NPY_NATIVE '='
1743 #define NPY_SWAP 's'
1744 #define NPY_IGNORE '|'
1745
1746 #if NPY_BYTE_ORDER == NPY_BIG_ENDIAN
1747 #define NPY_NATBYTE NPY_BIG
1748 #define NPY_OPPBYTE NPY_LITTLE
1749 #else
1750 #define NPY_NATBYTE NPY_LITTLE
1751 #define NPY_OPPBYTE NPY_BIG
1752 #endif
1753
1754 #define PyArray_ISNBO(arg) ((arg) != NPY_OPPBYTE)
1755 #define PyArray_IsNativeByteOrder PyArray_ISNBO
1756 #define PyArray_ISNOTSWAPPED(m) PyArray_ISNBO(PyArray_DESCR(m)->byteorder)
1757 #define PyArray_ISBYTESWAPPED(m) (!PyArray_ISNOTSWAPPED(m))
1758
1759 #define PyArray_FLAGSWAP(m, flags) (PyArray_CHKFLAGS(m, flags) && \
1760 PyArray_ISNOTSWAPPED(m))
1761
1762 #define PyArray_ISCARRAY(m) PyArray_FLAGSWAP(m, NPY_ARRAY_CARRAY)
1763 #define PyArray_ISCARRAY_RO(m) PyArray_FLAGSWAP(m, NPY_ARRAY_CARRAY_RO)
1764 #define PyArray_ISFARRAY(m) PyArray_FLAGSWAP(m, NPY_ARRAY_FARRAY)
1765 #define PyArray_ISFARRAY_RO(m) PyArray_FLAGSWAP(m, NPY_ARRAY_FARRAY_RO)
1766 #define PyArray_ISBEHAVED(m) PyArray_FLAGSWAP(m, NPY_ARRAY_BEHAVED)
1767 #define PyArray_ISBEHAVED_RO(m) PyArray_FLAGSWAP(m, NPY_ARRAY_ALIGNED)
1768
1769
1770 #define PyDataType_ISNOTSWAPPED(d) PyArray_ISNBO(((PyArray_Descr *)(d))->byteorder)
1771 #define PyDataType_ISBYTESWAPPED(d) (!PyDataType_ISNOTSWAPPED(d))
1772
1773 /************************************************************
1774 * A struct used by PyArray_CreateSortedStridePerm, new in 1.7.
1775 ************************************************************/
1776
1777 typedef struct {
1778 npy_intp perm, stride;
1779 } npy_stride_sort_item;
1780
1781 /************************************************************
1782 * This is the form of the struct that's stored in the
1783 * PyCapsule returned by an array's __array_struct__ attribute. See
1784 * https://docs.scipy.org/doc/numpy/reference/arrays.interface.html for the full
1785 * documentation.
1786 ************************************************************/
1787 typedef struct {
1788 int two; /*
1789 * contains the integer 2 as a sanity
1790 * check
1791 */
1792
1793 int nd; /* number of dimensions */
1794
1795 char typekind; /*
1796 * kind in array --- character code of
1797 * typestr
1798 */
1799
1800 int itemsize; /* size of each element */
1801
1802 int flags; /*
1803 * how should be data interpreted. Valid
1804 * flags are CONTIGUOUS (1), F_CONTIGUOUS (2),
1805 * ALIGNED (0x100), NOTSWAPPED (0x200), and
1806 * WRITEABLE (0x400). ARR_HAS_DESCR (0x800)
1807 * states that arrdescr field is present in
1808 * structure
1809 */
1810
1811 npy_intp *shape; /*
1812 * A length-nd array of shape
1813 * information
1814 */
1815
1816 npy_intp *strides; /* A length-nd array of stride information */
1817
1818 void *data; /* A pointer to the first element of the array */
1819
1820 PyObject *descr; /*
1821 * A list of fields or NULL (ignored if flags
1822 * does not have ARR_HAS_DESCR flag set)
1823 */
1824 } PyArrayInterface;
1825
1826 /*
1827 * This is a function for hooking into the PyDataMem_NEW/FREE/RENEW functions.
1828 * See the documentation for PyDataMem_SetEventHook.
1829 */
1830 typedef void (PyDataMem_EventHookFunc)(void *inp, void *outp, size_t size,
1831 void *user_data);
1832
1833
1834 /*
1835 * PyArray_DTypeMeta related definitions.
1836 *
1837 * As of now, this API is preliminary and will be extended as necessary.
1838 */
1839 #if defined(NPY_INTERNAL_BUILD) && NPY_INTERNAL_BUILD
1840 /*
1841 * The Structures defined in this block are considered private API and
1842 * may change without warning!
1843 */
1844 /* TODO: Make this definition public in the API, as soon as its settled */
1845 NPY_NO_EXPORT extern PyTypeObject PyArrayDTypeMeta_Type;
1846
1847 typedef struct PyArray_DTypeMeta_tag PyArray_DTypeMeta;
1848
1849 typedef PyArray_Descr *(discover_descr_from_pyobject_function)(
1850 PyArray_DTypeMeta *cls, PyObject *obj);
1851
1852 /*
1853 * Before making this public, we should decide whether it should pass
1854 * the type, or allow looking at the object. A possible use-case:
1855 * `np.array(np.array([0]), dtype=np.ndarray)`
1856 * Could consider arrays that are not `dtype=ndarray` "scalars".
1857 */
1858 typedef int (is_known_scalar_type_function)(
1859 PyArray_DTypeMeta *cls, PyTypeObject *obj);
1860
1861 typedef PyArray_Descr *(default_descr_function)(PyArray_DTypeMeta *cls);
1862 typedef PyArray_DTypeMeta *(common_dtype_function)(
1863 PyArray_DTypeMeta *dtype1, PyArray_DTypeMeta *dtyep2);
1864 typedef PyArray_Descr *(common_instance_function)(
1865 PyArray_Descr *dtype1, PyArray_Descr *dtyep2);
1866
1867 /*
1868 * While NumPy DTypes would not need to be heap types the plan is to
1869 * make DTypes available in Python at which point they will be heap types.
1870 * Since we also wish to add fields to the DType class, this looks like
1871 * a typical instance definition, but with PyHeapTypeObject instead of
1872 * only the PyObject_HEAD.
1873 * This must only be exposed very extremely careful consideration, since
1874 * it is a fairly complex construct which may be better to allow
1875 * refactoring of.
1876 */
1877 struct PyArray_DTypeMeta_tag {
1878 PyHeapTypeObject super;
1879
1880 /*
1881 * Most DTypes will have a singleton default instance, for the
1882 * parametric legacy DTypes (bytes, string, void, datetime) this
1883 * may be a pointer to the *prototype* instance?
1884 */
1885 PyArray_Descr *singleton;
1886 /*
1887 * Is this DType created using the old API? This exists mainly to
1888 * allow for assertions in paths specific to wrapping legacy types.
1889 */
1890 npy_bool legacy;
1891 /* The values stored by a parametric datatype depend on its instance */
1892 npy_bool parametric;
1893 /* whether the DType can be instantiated (i.e. np.dtype cannot) */
1894 npy_bool abstract;
1895
1896 /*
1897 * The following fields replicate the most important dtype information.
1898 * In the legacy implementation most of these are stored in the
1899 * PyArray_Descr struct.
1900 */
1901 /* The type object of the scalar instances (may be NULL?) */
1902 PyTypeObject *scalar_type;
1903 /* kind for this type */
1904 char kind;
1905 /* unique-character representing this type */
1906 char type;
1907 /* flags describing data type */
1908 char flags;
1909 /* number representing this type */
1910 int type_num;
1911 /*
1912 * Point to the original ArrFuncs.
1913 * NOTE: We could make a copy to detect changes to `f`.
1914 */
1915 PyArray_ArrFuncs *f;
1916
1917 /* DType methods, these could be moved into its own struct */
1918 discover_descr_from_pyobject_function *discover_descr_from_pyobject;
1919 is_known_scalar_type_function *is_known_scalar_type;
1920 default_descr_function *default_descr;
1921 common_dtype_function *common_dtype;
1922 common_instance_function *common_instance;
1923 /*
1924 * Dictionary of ArrayMethods representing most possible casts
1925 * (structured and object are exceptions).
1926 * This should potentially become a weak mapping in the future.
1927 */
1928 PyObject *castingimpls;
1929 };
1930
1931 #endif /* NPY_INTERNAL_BUILD */
1932
1933
1934 /*
1935 * Use the keyword NPY_DEPRECATED_INCLUDES to ensure that the header files
1936 * npy_*_*_deprecated_api.h are only included from here and nowhere else.
1937 */
1938 #ifdef NPY_DEPRECATED_INCLUDES
1939 #error "Do not use the reserved keyword NPY_DEPRECATED_INCLUDES."
1940 #endif
1941 #define NPY_DEPRECATED_INCLUDES
1942 #if !defined(NPY_NO_DEPRECATED_API) || \
1943 (NPY_NO_DEPRECATED_API < NPY_1_7_API_VERSION)
1944 #include "npy_1_7_deprecated_api.h"
1945 #endif
1946 /*
1947 * There is no file npy_1_8_deprecated_api.h since there are no additional
1948 * deprecated API features in NumPy 1.8.
1949 *
1950 * Note to maintainers: insert code like the following in future NumPy
1951 * versions.
1952 *
1953 * #if !defined(NPY_NO_DEPRECATED_API) || \
1954 * (NPY_NO_DEPRECATED_API < NPY_1_9_API_VERSION)
1955 * #include "npy_1_9_deprecated_api.h"
1956 * #endif
1957 */
1958 #undef NPY_DEPRECATED_INCLUDES
1959
1960 #endif /* NPY_ARRAYTYPES_H */
1961