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