1# NumPy static imports for Cython 2# 3# NOTE: Do not make incompatible local changes to this file without contacting the NumPy project. 4# This file is maintained by the NumPy project at 5# https://github.com/numpy/numpy/tree/master/numpy 6# 7# If any of the PyArray_* functions are called, import_array must be 8# called first. This is done automatically by Cython 3.0+ if a call 9# is not detected inside of the module. 10# 11# Author: Dag Sverre Seljebotn 12# 13 14from cpython.ref cimport Py_INCREF 15from cpython.object cimport PyObject, PyTypeObject, PyObject_TypeCheck 16cimport libc.stdio as stdio 17 18 19cdef extern from *: 20 # Leave a marker that the NumPy declarations came from Cython and not from NumPy itself. 21 # See https://github.com/cython/cython/issues/3573 22 """ 23 /* Using NumPy API declarations from "Cython/Includes/numpy/" */ 24 """ 25 26 27cdef extern from "Python.h": 28 ctypedef Py_ssize_t Py_intptr_t 29 30cdef extern from "numpy/arrayobject.h": 31 ctypedef Py_intptr_t npy_intp 32 ctypedef size_t npy_uintp 33 34 cdef enum NPY_TYPES: 35 NPY_BOOL 36 NPY_BYTE 37 NPY_UBYTE 38 NPY_SHORT 39 NPY_USHORT 40 NPY_INT 41 NPY_UINT 42 NPY_LONG 43 NPY_ULONG 44 NPY_LONGLONG 45 NPY_ULONGLONG 46 NPY_FLOAT 47 NPY_DOUBLE 48 NPY_LONGDOUBLE 49 NPY_CFLOAT 50 NPY_CDOUBLE 51 NPY_CLONGDOUBLE 52 NPY_OBJECT 53 NPY_STRING 54 NPY_UNICODE 55 NPY_VOID 56 NPY_DATETIME 57 NPY_TIMEDELTA 58 NPY_NTYPES 59 NPY_NOTYPE 60 61 NPY_INT8 62 NPY_INT16 63 NPY_INT32 64 NPY_INT64 65 NPY_INT128 66 NPY_INT256 67 NPY_UINT8 68 NPY_UINT16 69 NPY_UINT32 70 NPY_UINT64 71 NPY_UINT128 72 NPY_UINT256 73 NPY_FLOAT16 74 NPY_FLOAT32 75 NPY_FLOAT64 76 NPY_FLOAT80 77 NPY_FLOAT96 78 NPY_FLOAT128 79 NPY_FLOAT256 80 NPY_COMPLEX32 81 NPY_COMPLEX64 82 NPY_COMPLEX128 83 NPY_COMPLEX160 84 NPY_COMPLEX192 85 NPY_COMPLEX256 86 NPY_COMPLEX512 87 88 NPY_INTP 89 90 ctypedef enum NPY_ORDER: 91 NPY_ANYORDER 92 NPY_CORDER 93 NPY_FORTRANORDER 94 NPY_KEEPORDER 95 96 ctypedef enum NPY_CASTING: 97 NPY_NO_CASTING 98 NPY_EQUIV_CASTING 99 NPY_SAFE_CASTING 100 NPY_SAME_KIND_CASTING 101 NPY_UNSAFE_CASTING 102 103 ctypedef enum NPY_CLIPMODE: 104 NPY_CLIP 105 NPY_WRAP 106 NPY_RAISE 107 108 ctypedef enum NPY_SCALARKIND: 109 NPY_NOSCALAR, 110 NPY_BOOL_SCALAR, 111 NPY_INTPOS_SCALAR, 112 NPY_INTNEG_SCALAR, 113 NPY_FLOAT_SCALAR, 114 NPY_COMPLEX_SCALAR, 115 NPY_OBJECT_SCALAR 116 117 ctypedef enum NPY_SORTKIND: 118 NPY_QUICKSORT 119 NPY_HEAPSORT 120 NPY_MERGESORT 121 122 ctypedef enum NPY_SEARCHSIDE: 123 NPY_SEARCHLEFT 124 NPY_SEARCHRIGHT 125 126 enum: 127 # DEPRECATED since NumPy 1.7 ! Do not use in new code! 128 NPY_C_CONTIGUOUS 129 NPY_F_CONTIGUOUS 130 NPY_CONTIGUOUS 131 NPY_FORTRAN 132 NPY_OWNDATA 133 NPY_FORCECAST 134 NPY_ENSURECOPY 135 NPY_ENSUREARRAY 136 NPY_ELEMENTSTRIDES 137 NPY_ALIGNED 138 NPY_NOTSWAPPED 139 NPY_WRITEABLE 140 NPY_UPDATEIFCOPY 141 NPY_ARR_HAS_DESCR 142 143 NPY_BEHAVED 144 NPY_BEHAVED_NS 145 NPY_CARRAY 146 NPY_CARRAY_RO 147 NPY_FARRAY 148 NPY_FARRAY_RO 149 NPY_DEFAULT 150 151 NPY_IN_ARRAY 152 NPY_OUT_ARRAY 153 NPY_INOUT_ARRAY 154 NPY_IN_FARRAY 155 NPY_OUT_FARRAY 156 NPY_INOUT_FARRAY 157 158 NPY_UPDATE_ALL 159 160 enum: 161 # Added in NumPy 1.7 to replace the deprecated enums above. 162 NPY_ARRAY_C_CONTIGUOUS 163 NPY_ARRAY_F_CONTIGUOUS 164 NPY_ARRAY_OWNDATA 165 NPY_ARRAY_FORCECAST 166 NPY_ARRAY_ENSURECOPY 167 NPY_ARRAY_ENSUREARRAY 168 NPY_ARRAY_ELEMENTSTRIDES 169 NPY_ARRAY_ALIGNED 170 NPY_ARRAY_NOTSWAPPED 171 NPY_ARRAY_WRITEABLE 172 NPY_ARRAY_UPDATEIFCOPY 173 174 NPY_ARRAY_BEHAVED 175 NPY_ARRAY_BEHAVED_NS 176 NPY_ARRAY_CARRAY 177 NPY_ARRAY_CARRAY_RO 178 NPY_ARRAY_FARRAY 179 NPY_ARRAY_FARRAY_RO 180 NPY_ARRAY_DEFAULT 181 182 NPY_ARRAY_IN_ARRAY 183 NPY_ARRAY_OUT_ARRAY 184 NPY_ARRAY_INOUT_ARRAY 185 NPY_ARRAY_IN_FARRAY 186 NPY_ARRAY_OUT_FARRAY 187 NPY_ARRAY_INOUT_FARRAY 188 189 NPY_ARRAY_UPDATE_ALL 190 191 cdef enum: 192 NPY_MAXDIMS 193 194 npy_intp NPY_MAX_ELSIZE 195 196 ctypedef void (*PyArray_VectorUnaryFunc)(void *, void *, npy_intp, void *, void *) 197 198 ctypedef struct PyArray_ArrayDescr: 199 # shape is a tuple, but Cython doesn't support "tuple shape" 200 # inside a non-PyObject declaration, so we have to declare it 201 # as just a PyObject*. 202 PyObject* shape 203 204 ctypedef struct PyArray_Descr: 205 pass 206 207 ctypedef class numpy.dtype [object PyArray_Descr, check_size ignore]: 208 # Use PyDataType_* macros when possible, however there are no macros 209 # for accessing some of the fields, so some are defined. 210 cdef PyTypeObject* typeobj 211 cdef char kind 212 cdef char type 213 # Numpy sometimes mutates this without warning (e.g. it'll 214 # sometimes change "|" to "<" in shared dtype objects on 215 # little-endian machines). If this matters to you, use 216 # PyArray_IsNativeByteOrder(dtype.byteorder) instead of 217 # directly accessing this field. 218 cdef char byteorder 219 cdef char flags 220 cdef int type_num 221 cdef int itemsize "elsize" 222 cdef int alignment 223 cdef object fields 224 cdef tuple names 225 # Use PyDataType_HASSUBARRAY to test whether this field is 226 # valid (the pointer can be NULL). Most users should access 227 # this field via the inline helper method PyDataType_SHAPE. 228 cdef PyArray_ArrayDescr* subarray 229 230 ctypedef class numpy.flatiter [object PyArrayIterObject, check_size ignore]: 231 # Use through macros 232 pass 233 234 ctypedef class numpy.broadcast [object PyArrayMultiIterObject, check_size ignore]: 235 # Use through macros 236 pass 237 238 ctypedef struct PyArrayObject: 239 # For use in situations where ndarray can't replace PyArrayObject*, 240 # like PyArrayObject**. 241 pass 242 243 ctypedef class numpy.ndarray [object PyArrayObject, check_size ignore]: 244 cdef __cythonbufferdefaults__ = {"mode": "strided"} 245 246 # NOTE: no field declarations since direct access is deprecated since NumPy 1.7 247 # Instead, we use properties that map to the corresponding C-API functions. 248 249 @property 250 cdef inline PyObject* base(self) nogil: 251 """Returns a borrowed reference to the object owning the data/memory. 252 """ 253 return PyArray_BASE(self) 254 255 @property 256 cdef inline dtype descr(self): 257 """Returns an owned reference to the dtype of the array. 258 """ 259 return <dtype>PyArray_DESCR(self) 260 261 @property 262 cdef inline int ndim(self) nogil: 263 """Returns the number of dimensions in the array. 264 """ 265 return PyArray_NDIM(self) 266 267 @property 268 cdef inline npy_intp *shape(self) nogil: 269 """Returns a pointer to the dimensions/shape of the array. 270 The number of elements matches the number of dimensions of the array (ndim). 271 Can return NULL for 0-dimensional arrays. 272 """ 273 return PyArray_DIMS(self) 274 275 @property 276 cdef inline npy_intp *strides(self) nogil: 277 """Returns a pointer to the strides of the array. 278 The number of elements matches the number of dimensions of the array (ndim). 279 """ 280 return PyArray_STRIDES(self) 281 282 @property 283 cdef inline npy_intp size(self) nogil: 284 """Returns the total size (in number of elements) of the array. 285 """ 286 return PyArray_SIZE(self) 287 288 @property 289 cdef inline char* data(self) nogil: 290 """The pointer to the data buffer as a char*. 291 This is provided for legacy reasons to avoid direct struct field access. 292 For new code that needs this access, you probably want to cast the result 293 of `PyArray_DATA()` instead, which returns a 'void*'. 294 """ 295 return PyArray_BYTES(self) 296 297 ctypedef unsigned char npy_bool 298 299 ctypedef signed char npy_byte 300 ctypedef signed short npy_short 301 ctypedef signed int npy_int 302 ctypedef signed long npy_long 303 ctypedef signed long long npy_longlong 304 305 ctypedef unsigned char npy_ubyte 306 ctypedef unsigned short npy_ushort 307 ctypedef unsigned int npy_uint 308 ctypedef unsigned long npy_ulong 309 ctypedef unsigned long long npy_ulonglong 310 311 ctypedef float npy_float 312 ctypedef double npy_double 313 ctypedef long double npy_longdouble 314 315 ctypedef signed char npy_int8 316 ctypedef signed short npy_int16 317 ctypedef signed int npy_int32 318 ctypedef signed long long npy_int64 319 ctypedef signed long long npy_int96 320 ctypedef signed long long npy_int128 321 322 ctypedef unsigned char npy_uint8 323 ctypedef unsigned short npy_uint16 324 ctypedef unsigned int npy_uint32 325 ctypedef unsigned long long npy_uint64 326 ctypedef unsigned long long npy_uint96 327 ctypedef unsigned long long npy_uint128 328 329 ctypedef float npy_float32 330 ctypedef double npy_float64 331 ctypedef long double npy_float80 332 ctypedef long double npy_float96 333 ctypedef long double npy_float128 334 335 ctypedef struct npy_cfloat: 336 double real 337 double imag 338 339 ctypedef struct npy_cdouble: 340 double real 341 double imag 342 343 ctypedef struct npy_clongdouble: 344 long double real 345 long double imag 346 347 ctypedef struct npy_complex64: 348 float real 349 float imag 350 351 ctypedef struct npy_complex128: 352 double real 353 double imag 354 355 ctypedef struct npy_complex160: 356 long double real 357 long double imag 358 359 ctypedef struct npy_complex192: 360 long double real 361 long double imag 362 363 ctypedef struct npy_complex256: 364 long double real 365 long double imag 366 367 ctypedef struct PyArray_Dims: 368 npy_intp *ptr 369 int len 370 371 int _import_array() except -1 372 # A second definition so _import_array isn't marked as used when we use it here. 373 # Do not use - subject to change any time. 374 int __pyx_import_array "_import_array"() except -1 375 376 # 377 # Macros from ndarrayobject.h 378 # 379 bint PyArray_CHKFLAGS(ndarray m, int flags) nogil 380 bint PyArray_IS_C_CONTIGUOUS(ndarray arr) nogil 381 bint PyArray_IS_F_CONTIGUOUS(ndarray arr) nogil 382 bint PyArray_ISCONTIGUOUS(ndarray m) nogil 383 bint PyArray_ISWRITEABLE(ndarray m) nogil 384 bint PyArray_ISALIGNED(ndarray m) nogil 385 386 int PyArray_NDIM(ndarray) nogil 387 bint PyArray_ISONESEGMENT(ndarray) nogil 388 bint PyArray_ISFORTRAN(ndarray) nogil 389 int PyArray_FORTRANIF(ndarray) nogil 390 391 void* PyArray_DATA(ndarray) nogil 392 char* PyArray_BYTES(ndarray) nogil 393 394 npy_intp* PyArray_DIMS(ndarray) nogil 395 npy_intp* PyArray_STRIDES(ndarray) nogil 396 npy_intp PyArray_DIM(ndarray, size_t) nogil 397 npy_intp PyArray_STRIDE(ndarray, size_t) nogil 398 399 PyObject *PyArray_BASE(ndarray) nogil # returns borrowed reference! 400 PyArray_Descr *PyArray_DESCR(ndarray) nogil # returns borrowed reference to dtype! 401 PyArray_Descr *PyArray_DTYPE(ndarray) nogil # returns borrowed reference to dtype! NP 1.7+ alias for descr. 402 int PyArray_FLAGS(ndarray) nogil 403 void PyArray_CLEARFLAGS(ndarray, int flags) nogil # Added in NumPy 1.7 404 void PyArray_ENABLEFLAGS(ndarray, int flags) nogil # Added in NumPy 1.7 405 npy_intp PyArray_ITEMSIZE(ndarray) nogil 406 int PyArray_TYPE(ndarray arr) nogil 407 408 object PyArray_GETITEM(ndarray arr, void *itemptr) 409 int PyArray_SETITEM(ndarray arr, void *itemptr, object obj) 410 411 bint PyTypeNum_ISBOOL(int) nogil 412 bint PyTypeNum_ISUNSIGNED(int) nogil 413 bint PyTypeNum_ISSIGNED(int) nogil 414 bint PyTypeNum_ISINTEGER(int) nogil 415 bint PyTypeNum_ISFLOAT(int) nogil 416 bint PyTypeNum_ISNUMBER(int) nogil 417 bint PyTypeNum_ISSTRING(int) nogil 418 bint PyTypeNum_ISCOMPLEX(int) nogil 419 bint PyTypeNum_ISPYTHON(int) nogil 420 bint PyTypeNum_ISFLEXIBLE(int) nogil 421 bint PyTypeNum_ISUSERDEF(int) nogil 422 bint PyTypeNum_ISEXTENDED(int) nogil 423 bint PyTypeNum_ISOBJECT(int) nogil 424 425 bint PyDataType_ISBOOL(dtype) nogil 426 bint PyDataType_ISUNSIGNED(dtype) nogil 427 bint PyDataType_ISSIGNED(dtype) nogil 428 bint PyDataType_ISINTEGER(dtype) nogil 429 bint PyDataType_ISFLOAT(dtype) nogil 430 bint PyDataType_ISNUMBER(dtype) nogil 431 bint PyDataType_ISSTRING(dtype) nogil 432 bint PyDataType_ISCOMPLEX(dtype) nogil 433 bint PyDataType_ISPYTHON(dtype) nogil 434 bint PyDataType_ISFLEXIBLE(dtype) nogil 435 bint PyDataType_ISUSERDEF(dtype) nogil 436 bint PyDataType_ISEXTENDED(dtype) nogil 437 bint PyDataType_ISOBJECT(dtype) nogil 438 bint PyDataType_HASFIELDS(dtype) nogil 439 bint PyDataType_HASSUBARRAY(dtype) nogil 440 441 bint PyArray_ISBOOL(ndarray) nogil 442 bint PyArray_ISUNSIGNED(ndarray) nogil 443 bint PyArray_ISSIGNED(ndarray) nogil 444 bint PyArray_ISINTEGER(ndarray) nogil 445 bint PyArray_ISFLOAT(ndarray) nogil 446 bint PyArray_ISNUMBER(ndarray) nogil 447 bint PyArray_ISSTRING(ndarray) nogil 448 bint PyArray_ISCOMPLEX(ndarray) nogil 449 bint PyArray_ISPYTHON(ndarray) nogil 450 bint PyArray_ISFLEXIBLE(ndarray) nogil 451 bint PyArray_ISUSERDEF(ndarray) nogil 452 bint PyArray_ISEXTENDED(ndarray) nogil 453 bint PyArray_ISOBJECT(ndarray) nogil 454 bint PyArray_HASFIELDS(ndarray) nogil 455 456 bint PyArray_ISVARIABLE(ndarray) nogil 457 458 bint PyArray_SAFEALIGNEDCOPY(ndarray) nogil 459 bint PyArray_ISNBO(char) nogil # works on ndarray.byteorder 460 bint PyArray_IsNativeByteOrder(char) nogil # works on ndarray.byteorder 461 bint PyArray_ISNOTSWAPPED(ndarray) nogil 462 bint PyArray_ISBYTESWAPPED(ndarray) nogil 463 464 bint PyArray_FLAGSWAP(ndarray, int) nogil 465 466 bint PyArray_ISCARRAY(ndarray) nogil 467 bint PyArray_ISCARRAY_RO(ndarray) nogil 468 bint PyArray_ISFARRAY(ndarray) nogil 469 bint PyArray_ISFARRAY_RO(ndarray) nogil 470 bint PyArray_ISBEHAVED(ndarray) nogil 471 bint PyArray_ISBEHAVED_RO(ndarray) nogil 472 473 474 bint PyDataType_ISNOTSWAPPED(dtype) nogil 475 bint PyDataType_ISBYTESWAPPED(dtype) nogil 476 477 bint PyArray_DescrCheck(object) 478 479 bint PyArray_Check(object) 480 bint PyArray_CheckExact(object) 481 482 # Cannot be supported due to out arg: 483 # bint PyArray_HasArrayInterfaceType(object, dtype, object, object&) 484 # bint PyArray_HasArrayInterface(op, out) 485 486 487 bint PyArray_IsZeroDim(object) 488 # Cannot be supported due to ## ## in macro: 489 # bint PyArray_IsScalar(object, verbatim work) 490 bint PyArray_CheckScalar(object) 491 bint PyArray_IsPythonNumber(object) 492 bint PyArray_IsPythonScalar(object) 493 bint PyArray_IsAnyScalar(object) 494 bint PyArray_CheckAnyScalar(object) 495 496 ndarray PyArray_GETCONTIGUOUS(ndarray) 497 bint PyArray_SAMESHAPE(ndarray, ndarray) nogil 498 npy_intp PyArray_SIZE(ndarray) nogil 499 npy_intp PyArray_NBYTES(ndarray) nogil 500 501 object PyArray_FROM_O(object) 502 object PyArray_FROM_OF(object m, int flags) 503 object PyArray_FROM_OT(object m, int type) 504 object PyArray_FROM_OTF(object m, int type, int flags) 505 object PyArray_FROMANY(object m, int type, int min, int max, int flags) 506 object PyArray_ZEROS(int nd, npy_intp* dims, int type, int fortran) 507 object PyArray_EMPTY(int nd, npy_intp* dims, int type, int fortran) 508 void PyArray_FILLWBYTE(object, int val) 509 npy_intp PyArray_REFCOUNT(object) 510 object PyArray_ContiguousFromAny(op, int, int min_depth, int max_depth) 511 unsigned char PyArray_EquivArrTypes(ndarray a1, ndarray a2) 512 bint PyArray_EquivByteorders(int b1, int b2) nogil 513 object PyArray_SimpleNew(int nd, npy_intp* dims, int typenum) 514 object PyArray_SimpleNewFromData(int nd, npy_intp* dims, int typenum, void* data) 515 #object PyArray_SimpleNewFromDescr(int nd, npy_intp* dims, dtype descr) 516 object PyArray_ToScalar(void* data, ndarray arr) 517 518 void* PyArray_GETPTR1(ndarray m, npy_intp i) nogil 519 void* PyArray_GETPTR2(ndarray m, npy_intp i, npy_intp j) nogil 520 void* PyArray_GETPTR3(ndarray m, npy_intp i, npy_intp j, npy_intp k) nogil 521 void* PyArray_GETPTR4(ndarray m, npy_intp i, npy_intp j, npy_intp k, npy_intp l) nogil 522 523 void PyArray_XDECREF_ERR(ndarray) 524 # Cannot be supported due to out arg 525 # void PyArray_DESCR_REPLACE(descr) 526 527 528 object PyArray_Copy(ndarray) 529 object PyArray_FromObject(object op, int type, int min_depth, int max_depth) 530 object PyArray_ContiguousFromObject(object op, int type, int min_depth, int max_depth) 531 object PyArray_CopyFromObject(object op, int type, int min_depth, int max_depth) 532 533 object PyArray_Cast(ndarray mp, int type_num) 534 object PyArray_Take(ndarray ap, object items, int axis) 535 object PyArray_Put(ndarray ap, object items, object values) 536 537 void PyArray_ITER_RESET(flatiter it) nogil 538 void PyArray_ITER_NEXT(flatiter it) nogil 539 void PyArray_ITER_GOTO(flatiter it, npy_intp* destination) nogil 540 void PyArray_ITER_GOTO1D(flatiter it, npy_intp ind) nogil 541 void* PyArray_ITER_DATA(flatiter it) nogil 542 bint PyArray_ITER_NOTDONE(flatiter it) nogil 543 544 void PyArray_MultiIter_RESET(broadcast multi) nogil 545 void PyArray_MultiIter_NEXT(broadcast multi) nogil 546 void PyArray_MultiIter_GOTO(broadcast multi, npy_intp dest) nogil 547 void PyArray_MultiIter_GOTO1D(broadcast multi, npy_intp ind) nogil 548 void* PyArray_MultiIter_DATA(broadcast multi, npy_intp i) nogil 549 void PyArray_MultiIter_NEXTi(broadcast multi, npy_intp i) nogil 550 bint PyArray_MultiIter_NOTDONE(broadcast multi) nogil 551 552 # Functions from __multiarray_api.h 553 554 # Functions taking dtype and returning object/ndarray are disabled 555 # for now as they steal dtype references. I'm conservative and disable 556 # more than is probably needed until it can be checked further. 557 int PyArray_SetNumericOps (object) 558 object PyArray_GetNumericOps () 559 int PyArray_INCREF (ndarray) 560 int PyArray_XDECREF (ndarray) 561 void PyArray_SetStringFunction (object, int) 562 dtype PyArray_DescrFromType (int) 563 object PyArray_TypeObjectFromType (int) 564 char * PyArray_Zero (ndarray) 565 char * PyArray_One (ndarray) 566 #object PyArray_CastToType (ndarray, dtype, int) 567 int PyArray_CastTo (ndarray, ndarray) 568 int PyArray_CastAnyTo (ndarray, ndarray) 569 int PyArray_CanCastSafely (int, int) 570 npy_bool PyArray_CanCastTo (dtype, dtype) 571 int PyArray_ObjectType (object, int) 572 dtype PyArray_DescrFromObject (object, dtype) 573 #ndarray* PyArray_ConvertToCommonType (object, int *) 574 dtype PyArray_DescrFromScalar (object) 575 dtype PyArray_DescrFromTypeObject (object) 576 npy_intp PyArray_Size (object) 577 #object PyArray_Scalar (void *, dtype, object) 578 #object PyArray_FromScalar (object, dtype) 579 void PyArray_ScalarAsCtype (object, void *) 580 #int PyArray_CastScalarToCtype (object, void *, dtype) 581 #int PyArray_CastScalarDirect (object, dtype, void *, int) 582 object PyArray_ScalarFromObject (object) 583 #PyArray_VectorUnaryFunc * PyArray_GetCastFunc (dtype, int) 584 object PyArray_FromDims (int, int *, int) 585 #object PyArray_FromDimsAndDataAndDescr (int, int *, dtype, char *) 586 #object PyArray_FromAny (object, dtype, int, int, int, object) 587 object PyArray_EnsureArray (object) 588 object PyArray_EnsureAnyArray (object) 589 #object PyArray_FromFile (stdio.FILE *, dtype, npy_intp, char *) 590 #object PyArray_FromString (char *, npy_intp, dtype, npy_intp, char *) 591 #object PyArray_FromBuffer (object, dtype, npy_intp, npy_intp) 592 #object PyArray_FromIter (object, dtype, npy_intp) 593 object PyArray_Return (ndarray) 594 #object PyArray_GetField (ndarray, dtype, int) 595 #int PyArray_SetField (ndarray, dtype, int, object) 596 object PyArray_Byteswap (ndarray, npy_bool) 597 object PyArray_Resize (ndarray, PyArray_Dims *, int, NPY_ORDER) 598 int PyArray_MoveInto (ndarray, ndarray) 599 int PyArray_CopyInto (ndarray, ndarray) 600 int PyArray_CopyAnyInto (ndarray, ndarray) 601 int PyArray_CopyObject (ndarray, object) 602 object PyArray_NewCopy (ndarray, NPY_ORDER) 603 object PyArray_ToList (ndarray) 604 object PyArray_ToString (ndarray, NPY_ORDER) 605 int PyArray_ToFile (ndarray, stdio.FILE *, char *, char *) 606 int PyArray_Dump (object, object, int) 607 object PyArray_Dumps (object, int) 608 int PyArray_ValidType (int) 609 void PyArray_UpdateFlags (ndarray, int) 610 object PyArray_New (type, int, npy_intp *, int, npy_intp *, void *, int, int, object) 611 #object PyArray_NewFromDescr (type, dtype, int, npy_intp *, npy_intp *, void *, int, object) 612 #dtype PyArray_DescrNew (dtype) 613 dtype PyArray_DescrNewFromType (int) 614 double PyArray_GetPriority (object, double) 615 object PyArray_IterNew (object) 616 object PyArray_MultiIterNew (int, ...) 617 618 int PyArray_PyIntAsInt (object) 619 npy_intp PyArray_PyIntAsIntp (object) 620 int PyArray_Broadcast (broadcast) 621 void PyArray_FillObjectArray (ndarray, object) 622 int PyArray_FillWithScalar (ndarray, object) 623 npy_bool PyArray_CheckStrides (int, int, npy_intp, npy_intp, npy_intp *, npy_intp *) 624 dtype PyArray_DescrNewByteorder (dtype, char) 625 object PyArray_IterAllButAxis (object, int *) 626 #object PyArray_CheckFromAny (object, dtype, int, int, int, object) 627 #object PyArray_FromArray (ndarray, dtype, int) 628 object PyArray_FromInterface (object) 629 object PyArray_FromStructInterface (object) 630 #object PyArray_FromArrayAttr (object, dtype, object) 631 #NPY_SCALARKIND PyArray_ScalarKind (int, ndarray*) 632 int PyArray_CanCoerceScalar (int, int, NPY_SCALARKIND) 633 object PyArray_NewFlagsObject (object) 634 npy_bool PyArray_CanCastScalar (type, type) 635 #int PyArray_CompareUCS4 (npy_ucs4 *, npy_ucs4 *, register size_t) 636 int PyArray_RemoveSmallest (broadcast) 637 int PyArray_ElementStrides (object) 638 void PyArray_Item_INCREF (char *, dtype) 639 void PyArray_Item_XDECREF (char *, dtype) 640 object PyArray_FieldNames (object) 641 object PyArray_Transpose (ndarray, PyArray_Dims *) 642 object PyArray_TakeFrom (ndarray, object, int, ndarray, NPY_CLIPMODE) 643 object PyArray_PutTo (ndarray, object, object, NPY_CLIPMODE) 644 object PyArray_PutMask (ndarray, object, object) 645 object PyArray_Repeat (ndarray, object, int) 646 object PyArray_Choose (ndarray, object, ndarray, NPY_CLIPMODE) 647 int PyArray_Sort (ndarray, int, NPY_SORTKIND) 648 object PyArray_ArgSort (ndarray, int, NPY_SORTKIND) 649 object PyArray_SearchSorted (ndarray, object, NPY_SEARCHSIDE, PyObject *) 650 object PyArray_ArgMax (ndarray, int, ndarray) 651 object PyArray_ArgMin (ndarray, int, ndarray) 652 object PyArray_Reshape (ndarray, object) 653 object PyArray_Newshape (ndarray, PyArray_Dims *, NPY_ORDER) 654 object PyArray_Squeeze (ndarray) 655 #object PyArray_View (ndarray, dtype, type) 656 object PyArray_SwapAxes (ndarray, int, int) 657 object PyArray_Max (ndarray, int, ndarray) 658 object PyArray_Min (ndarray, int, ndarray) 659 object PyArray_Ptp (ndarray, int, ndarray) 660 object PyArray_Mean (ndarray, int, int, ndarray) 661 object PyArray_Trace (ndarray, int, int, int, int, ndarray) 662 object PyArray_Diagonal (ndarray, int, int, int) 663 object PyArray_Clip (ndarray, object, object, ndarray) 664 object PyArray_Conjugate (ndarray, ndarray) 665 object PyArray_Nonzero (ndarray) 666 object PyArray_Std (ndarray, int, int, ndarray, int) 667 object PyArray_Sum (ndarray, int, int, ndarray) 668 object PyArray_CumSum (ndarray, int, int, ndarray) 669 object PyArray_Prod (ndarray, int, int, ndarray) 670 object PyArray_CumProd (ndarray, int, int, ndarray) 671 object PyArray_All (ndarray, int, ndarray) 672 object PyArray_Any (ndarray, int, ndarray) 673 object PyArray_Compress (ndarray, object, int, ndarray) 674 object PyArray_Flatten (ndarray, NPY_ORDER) 675 object PyArray_Ravel (ndarray, NPY_ORDER) 676 npy_intp PyArray_MultiplyList (npy_intp *, int) 677 int PyArray_MultiplyIntList (int *, int) 678 void * PyArray_GetPtr (ndarray, npy_intp*) 679 int PyArray_CompareLists (npy_intp *, npy_intp *, int) 680 #int PyArray_AsCArray (object*, void *, npy_intp *, int, dtype) 681 #int PyArray_As1D (object*, char **, int *, int) 682 #int PyArray_As2D (object*, char ***, int *, int *, int) 683 int PyArray_Free (object, void *) 684 #int PyArray_Converter (object, object*) 685 int PyArray_IntpFromSequence (object, npy_intp *, int) 686 object PyArray_Concatenate (object, int) 687 object PyArray_InnerProduct (object, object) 688 object PyArray_MatrixProduct (object, object) 689 object PyArray_CopyAndTranspose (object) 690 object PyArray_Correlate (object, object, int) 691 int PyArray_TypestrConvert (int, int) 692 #int PyArray_DescrConverter (object, dtype*) 693 #int PyArray_DescrConverter2 (object, dtype*) 694 int PyArray_IntpConverter (object, PyArray_Dims *) 695 #int PyArray_BufferConverter (object, chunk) 696 int PyArray_AxisConverter (object, int *) 697 int PyArray_BoolConverter (object, npy_bool *) 698 int PyArray_ByteorderConverter (object, char *) 699 int PyArray_OrderConverter (object, NPY_ORDER *) 700 unsigned char PyArray_EquivTypes (dtype, dtype) 701 #object PyArray_Zeros (int, npy_intp *, dtype, int) 702 #object PyArray_Empty (int, npy_intp *, dtype, int) 703 object PyArray_Where (object, object, object) 704 object PyArray_Arange (double, double, double, int) 705 #object PyArray_ArangeObj (object, object, object, dtype) 706 int PyArray_SortkindConverter (object, NPY_SORTKIND *) 707 object PyArray_LexSort (object, int) 708 object PyArray_Round (ndarray, int, ndarray) 709 unsigned char PyArray_EquivTypenums (int, int) 710 int PyArray_RegisterDataType (dtype) 711 int PyArray_RegisterCastFunc (dtype, int, PyArray_VectorUnaryFunc *) 712 int PyArray_RegisterCanCast (dtype, int, NPY_SCALARKIND) 713 #void PyArray_InitArrFuncs (PyArray_ArrFuncs *) 714 object PyArray_IntTupleFromIntp (int, npy_intp *) 715 int PyArray_TypeNumFromName (char *) 716 int PyArray_ClipmodeConverter (object, NPY_CLIPMODE *) 717 #int PyArray_OutputConverter (object, ndarray*) 718 object PyArray_BroadcastToShape (object, npy_intp *, int) 719 void _PyArray_SigintHandler (int) 720 void* _PyArray_GetSigintBuf () 721 #int PyArray_DescrAlignConverter (object, dtype*) 722 #int PyArray_DescrAlignConverter2 (object, dtype*) 723 int PyArray_SearchsideConverter (object, void *) 724 object PyArray_CheckAxis (ndarray, int *, int) 725 npy_intp PyArray_OverflowMultiplyList (npy_intp *, int) 726 int PyArray_CompareString (char *, char *, size_t) 727 int PyArray_SetBaseObject(ndarray, base) # NOTE: steals a reference to base! Use "set_array_base()" instead. 728 729 730# Typedefs that matches the runtime dtype objects in 731# the numpy module. 732 733# The ones that are commented out needs an IFDEF function 734# in Cython to enable them only on the right systems. 735 736ctypedef npy_int8 int8_t 737ctypedef npy_int16 int16_t 738ctypedef npy_int32 int32_t 739ctypedef npy_int64 int64_t 740#ctypedef npy_int96 int96_t 741#ctypedef npy_int128 int128_t 742 743ctypedef npy_uint8 uint8_t 744ctypedef npy_uint16 uint16_t 745ctypedef npy_uint32 uint32_t 746ctypedef npy_uint64 uint64_t 747#ctypedef npy_uint96 uint96_t 748#ctypedef npy_uint128 uint128_t 749 750ctypedef npy_float32 float32_t 751ctypedef npy_float64 float64_t 752#ctypedef npy_float80 float80_t 753#ctypedef npy_float128 float128_t 754 755ctypedef float complex complex64_t 756ctypedef double complex complex128_t 757 758# The int types are mapped a bit surprising -- 759# numpy.int corresponds to 'l' and numpy.long to 'q' 760ctypedef npy_long int_t 761ctypedef npy_longlong long_t 762ctypedef npy_longlong longlong_t 763 764ctypedef npy_ulong uint_t 765ctypedef npy_ulonglong ulong_t 766ctypedef npy_ulonglong ulonglong_t 767 768ctypedef npy_intp intp_t 769ctypedef npy_uintp uintp_t 770 771ctypedef npy_double float_t 772ctypedef npy_double double_t 773ctypedef npy_longdouble longdouble_t 774 775ctypedef npy_cfloat cfloat_t 776ctypedef npy_cdouble cdouble_t 777ctypedef npy_clongdouble clongdouble_t 778 779ctypedef npy_cdouble complex_t 780 781cdef inline object PyArray_MultiIterNew1(a): 782 return PyArray_MultiIterNew(1, <void*>a) 783 784cdef inline object PyArray_MultiIterNew2(a, b): 785 return PyArray_MultiIterNew(2, <void*>a, <void*>b) 786 787cdef inline object PyArray_MultiIterNew3(a, b, c): 788 return PyArray_MultiIterNew(3, <void*>a, <void*>b, <void*> c) 789 790cdef inline object PyArray_MultiIterNew4(a, b, c, d): 791 return PyArray_MultiIterNew(4, <void*>a, <void*>b, <void*>c, <void*> d) 792 793cdef inline object PyArray_MultiIterNew5(a, b, c, d, e): 794 return PyArray_MultiIterNew(5, <void*>a, <void*>b, <void*>c, <void*> d, <void*> e) 795 796cdef inline tuple PyDataType_SHAPE(dtype d): 797 if PyDataType_HASSUBARRAY(d): 798 return <tuple>d.subarray.shape 799 else: 800 return () 801 802 803cdef extern from "numpy/ndarrayobject.h": 804 PyTypeObject PyTimedeltaArrType_Type 805 PyTypeObject PyDatetimeArrType_Type 806 ctypedef int64_t npy_timedelta 807 ctypedef int64_t npy_datetime 808 809cdef extern from "numpy/ndarraytypes.h": 810 ctypedef struct PyArray_DatetimeMetaData: 811 NPY_DATETIMEUNIT base 812 int64_t num 813 814cdef extern from "numpy/arrayscalars.h": 815 816 # abstract types 817 ctypedef class numpy.generic [object PyObject]: 818 pass 819 ctypedef class numpy.number [object PyObject]: 820 pass 821 ctypedef class numpy.integer [object PyObject]: 822 pass 823 ctypedef class numpy.signedinteger [object PyObject]: 824 pass 825 ctypedef class numpy.unsignedinteger [object PyObject]: 826 pass 827 ctypedef class numpy.inexact [object PyObject]: 828 pass 829 ctypedef class numpy.floating [object PyObject]: 830 pass 831 ctypedef class numpy.complexfloating [object PyObject]: 832 pass 833 ctypedef class numpy.flexible [object PyObject]: 834 pass 835 ctypedef class numpy.character [object PyObject]: 836 pass 837 838 ctypedef struct PyDatetimeScalarObject: 839 # PyObject_HEAD 840 npy_datetime obval 841 PyArray_DatetimeMetaData obmeta 842 843 ctypedef struct PyTimedeltaScalarObject: 844 # PyObject_HEAD 845 npy_timedelta obval 846 PyArray_DatetimeMetaData obmeta 847 848 ctypedef enum NPY_DATETIMEUNIT: 849 NPY_FR_Y 850 NPY_FR_M 851 NPY_FR_W 852 NPY_FR_D 853 NPY_FR_B 854 NPY_FR_h 855 NPY_FR_m 856 NPY_FR_s 857 NPY_FR_ms 858 NPY_FR_us 859 NPY_FR_ns 860 NPY_FR_ps 861 NPY_FR_fs 862 NPY_FR_as 863 864 865# 866# ufunc API 867# 868 869cdef extern from "numpy/ufuncobject.h": 870 871 ctypedef void (*PyUFuncGenericFunction) (char **, npy_intp *, npy_intp *, void *) 872 873 ctypedef class numpy.ufunc [object PyUFuncObject, check_size ignore]: 874 cdef: 875 int nin, nout, nargs 876 int identity 877 PyUFuncGenericFunction *functions 878 void **data 879 int ntypes 880 int check_return 881 char *name 882 char *types 883 char *doc 884 void *ptr 885 PyObject *obj 886 PyObject *userloops 887 888 cdef enum: 889 PyUFunc_Zero 890 PyUFunc_One 891 PyUFunc_None 892 UFUNC_ERR_IGNORE 893 UFUNC_ERR_WARN 894 UFUNC_ERR_RAISE 895 UFUNC_ERR_CALL 896 UFUNC_ERR_PRINT 897 UFUNC_ERR_LOG 898 UFUNC_MASK_DIVIDEBYZERO 899 UFUNC_MASK_OVERFLOW 900 UFUNC_MASK_UNDERFLOW 901 UFUNC_MASK_INVALID 902 UFUNC_SHIFT_DIVIDEBYZERO 903 UFUNC_SHIFT_OVERFLOW 904 UFUNC_SHIFT_UNDERFLOW 905 UFUNC_SHIFT_INVALID 906 UFUNC_FPE_DIVIDEBYZERO 907 UFUNC_FPE_OVERFLOW 908 UFUNC_FPE_UNDERFLOW 909 UFUNC_FPE_INVALID 910 UFUNC_ERR_DEFAULT 911 UFUNC_ERR_DEFAULT2 912 913 object PyUFunc_FromFuncAndData(PyUFuncGenericFunction *, 914 void **, char *, int, int, int, int, char *, char *, int) 915 int PyUFunc_RegisterLoopForType(ufunc, int, 916 PyUFuncGenericFunction, int *, void *) 917 int PyUFunc_GenericFunction \ 918 (ufunc, PyObject *, PyObject *, PyArrayObject **) 919 void PyUFunc_f_f_As_d_d \ 920 (char **, npy_intp *, npy_intp *, void *) 921 void PyUFunc_d_d \ 922 (char **, npy_intp *, npy_intp *, void *) 923 void PyUFunc_f_f \ 924 (char **, npy_intp *, npy_intp *, void *) 925 void PyUFunc_g_g \ 926 (char **, npy_intp *, npy_intp *, void *) 927 void PyUFunc_F_F_As_D_D \ 928 (char **, npy_intp *, npy_intp *, void *) 929 void PyUFunc_F_F \ 930 (char **, npy_intp *, npy_intp *, void *) 931 void PyUFunc_D_D \ 932 (char **, npy_intp *, npy_intp *, void *) 933 void PyUFunc_G_G \ 934 (char **, npy_intp *, npy_intp *, void *) 935 void PyUFunc_O_O \ 936 (char **, npy_intp *, npy_intp *, void *) 937 void PyUFunc_ff_f_As_dd_d \ 938 (char **, npy_intp *, npy_intp *, void *) 939 void PyUFunc_ff_f \ 940 (char **, npy_intp *, npy_intp *, void *) 941 void PyUFunc_dd_d \ 942 (char **, npy_intp *, npy_intp *, void *) 943 void PyUFunc_gg_g \ 944 (char **, npy_intp *, npy_intp *, void *) 945 void PyUFunc_FF_F_As_DD_D \ 946 (char **, npy_intp *, npy_intp *, void *) 947 void PyUFunc_DD_D \ 948 (char **, npy_intp *, npy_intp *, void *) 949 void PyUFunc_FF_F \ 950 (char **, npy_intp *, npy_intp *, void *) 951 void PyUFunc_GG_G \ 952 (char **, npy_intp *, npy_intp *, void *) 953 void PyUFunc_OO_O \ 954 (char **, npy_intp *, npy_intp *, void *) 955 void PyUFunc_O_O_method \ 956 (char **, npy_intp *, npy_intp *, void *) 957 void PyUFunc_OO_O_method \ 958 (char **, npy_intp *, npy_intp *, void *) 959 void PyUFunc_On_Om \ 960 (char **, npy_intp *, npy_intp *, void *) 961 int PyUFunc_GetPyValues \ 962 (char *, int *, int *, PyObject **) 963 int PyUFunc_checkfperr \ 964 (int, PyObject *, int *) 965 void PyUFunc_clearfperr() 966 int PyUFunc_getfperr() 967 int PyUFunc_handlefperr \ 968 (int, PyObject *, int, int *) 969 int PyUFunc_ReplaceLoopBySignature \ 970 (ufunc, PyUFuncGenericFunction, int *, PyUFuncGenericFunction *) 971 object PyUFunc_FromFuncAndDataAndSignature \ 972 (PyUFuncGenericFunction *, void **, char *, int, int, int, 973 int, char *, char *, int, char *) 974 975 int _import_umath() except -1 976 977cdef inline void set_array_base(ndarray arr, object base): 978 Py_INCREF(base) # important to do this before stealing the reference below! 979 PyArray_SetBaseObject(arr, base) 980 981cdef inline object get_array_base(ndarray arr): 982 base = PyArray_BASE(arr) 983 if base is NULL: 984 return None 985 return <object>base 986 987# Versions of the import_* functions which are more suitable for 988# Cython code. 989cdef inline int import_array() except -1: 990 try: 991 __pyx_import_array() 992 except Exception: 993 raise ImportError("numpy.core.multiarray failed to import") 994 995cdef inline int import_umath() except -1: 996 try: 997 _import_umath() 998 except Exception: 999 raise ImportError("numpy.core.umath failed to import") 1000 1001cdef inline int import_ufunc() except -1: 1002 try: 1003 _import_umath() 1004 except Exception: 1005 raise ImportError("numpy.core.umath failed to import") 1006 1007 1008cdef inline bint is_timedelta64_object(object obj): 1009 """ 1010 Cython equivalent of `isinstance(obj, np.timedelta64)` 1011 1012 Parameters 1013 ---------- 1014 obj : object 1015 1016 Returns 1017 ------- 1018 bool 1019 """ 1020 return PyObject_TypeCheck(obj, &PyTimedeltaArrType_Type) 1021 1022 1023cdef inline bint is_datetime64_object(object obj): 1024 """ 1025 Cython equivalent of `isinstance(obj, np.datetime64)` 1026 1027 Parameters 1028 ---------- 1029 obj : object 1030 1031 Returns 1032 ------- 1033 bool 1034 """ 1035 return PyObject_TypeCheck(obj, &PyDatetimeArrType_Type) 1036 1037 1038cdef inline npy_datetime get_datetime64_value(object obj) nogil: 1039 """ 1040 returns the int64 value underlying scalar numpy datetime64 object 1041 1042 Note that to interpret this as a datetime, the corresponding unit is 1043 also needed. That can be found using `get_datetime64_unit`. 1044 """ 1045 return (<PyDatetimeScalarObject*>obj).obval 1046 1047 1048cdef inline npy_timedelta get_timedelta64_value(object obj) nogil: 1049 """ 1050 returns the int64 value underlying scalar numpy timedelta64 object 1051 """ 1052 return (<PyTimedeltaScalarObject*>obj).obval 1053 1054 1055cdef inline NPY_DATETIMEUNIT get_datetime64_unit(object obj) nogil: 1056 """ 1057 returns the unit part of the dtype for a numpy datetime64 object. 1058 """ 1059 return <NPY_DATETIMEUNIT>(<PyDatetimeScalarObject*>obj).obmeta.base 1060