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