__init__.cython-30.pxd 35 KB

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