__init__.pxd 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  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. DEF _buffer_format_string_len = 255
  9. cimport cpython.buffer as pybuf
  10. from cpython.ref cimport Py_INCREF
  11. from cpython.mem cimport PyObject_Malloc, PyObject_Free
  12. from cpython.object cimport PyObject, PyTypeObject
  13. from cpython.buffer cimport PyObject_GetBuffer
  14. from cpython.type cimport type
  15. cimport libc.stdio as stdio
  16. cdef extern from *:
  17. # Leave a marker that the NumPy declarations came from NumPy itself and not from Cython.
  18. # See https://github.com/cython/cython/issues/3573
  19. """
  20. /* Using NumPy API declarations from "numpy/__init__.pxd" */
  21. """
  22. cdef extern from "Python.h":
  23. ctypedef int Py_intptr_t
  24. bint PyObject_TypeCheck(object obj, PyTypeObject* type)
  25. cdef extern from "numpy/arrayobject.h":
  26. # It would be nice to use size_t and ssize_t, but ssize_t has special
  27. # implicit conversion rules, so just use "long".
  28. # Note: The actual type only matters for Cython promotion, so long
  29. # is closer than int, but could lead to incorrect promotion.
  30. # (Not to worrying, and always the status-quo.)
  31. ctypedef signed long npy_intp
  32. ctypedef unsigned long npy_uintp
  33. ctypedef unsigned char npy_bool
  34. ctypedef signed char npy_byte
  35. ctypedef signed short npy_short
  36. ctypedef signed int npy_int
  37. ctypedef signed long npy_long
  38. ctypedef signed long long npy_longlong
  39. ctypedef unsigned char npy_ubyte
  40. ctypedef unsigned short npy_ushort
  41. ctypedef unsigned int npy_uint
  42. ctypedef unsigned long npy_ulong
  43. ctypedef unsigned long long npy_ulonglong
  44. ctypedef float npy_float
  45. ctypedef double npy_double
  46. ctypedef long double npy_longdouble
  47. ctypedef signed char npy_int8
  48. ctypedef signed short npy_int16
  49. ctypedef signed int npy_int32
  50. ctypedef signed long long npy_int64
  51. ctypedef unsigned char npy_uint8
  52. ctypedef unsigned short npy_uint16
  53. ctypedef unsigned int npy_uint32
  54. ctypedef unsigned long long npy_uint64
  55. ctypedef float npy_float32
  56. ctypedef double npy_float64
  57. ctypedef long double npy_float80
  58. ctypedef long double npy_float96
  59. ctypedef long double npy_float128
  60. ctypedef struct npy_cfloat:
  61. pass
  62. ctypedef struct npy_cdouble:
  63. pass
  64. ctypedef struct npy_clongdouble:
  65. pass
  66. ctypedef struct npy_complex64:
  67. pass
  68. ctypedef struct npy_complex128:
  69. pass
  70. ctypedef struct npy_complex160:
  71. pass
  72. ctypedef struct npy_complex192:
  73. pass
  74. ctypedef struct npy_complex256:
  75. pass
  76. ctypedef struct PyArray_Dims:
  77. npy_intp *ptr
  78. int len
  79. cdef enum NPY_TYPES:
  80. NPY_BOOL
  81. NPY_BYTE
  82. NPY_UBYTE
  83. NPY_SHORT
  84. NPY_USHORT
  85. NPY_INT
  86. NPY_UINT
  87. NPY_LONG
  88. NPY_ULONG
  89. NPY_LONGLONG
  90. NPY_ULONGLONG
  91. NPY_FLOAT
  92. NPY_DOUBLE
  93. NPY_LONGDOUBLE
  94. NPY_CFLOAT
  95. NPY_CDOUBLE
  96. NPY_CLONGDOUBLE
  97. NPY_OBJECT
  98. NPY_STRING
  99. NPY_UNICODE
  100. NPY_VSTRING
  101. NPY_VOID
  102. NPY_DATETIME
  103. NPY_TIMEDELTA
  104. NPY_NTYPES_LEGACY
  105. NPY_NOTYPE
  106. NPY_INT8
  107. NPY_INT16
  108. NPY_INT32
  109. NPY_INT64
  110. NPY_UINT8
  111. NPY_UINT16
  112. NPY_UINT32
  113. NPY_UINT64
  114. NPY_FLOAT16
  115. NPY_FLOAT32
  116. NPY_FLOAT64
  117. NPY_FLOAT80
  118. NPY_FLOAT96
  119. NPY_FLOAT128
  120. NPY_COMPLEX64
  121. NPY_COMPLEX128
  122. NPY_COMPLEX160
  123. NPY_COMPLEX192
  124. NPY_COMPLEX256
  125. NPY_INTP
  126. NPY_UINTP
  127. NPY_DEFAULT_INT # Not a compile time constant (normally)!
  128. ctypedef enum NPY_ORDER:
  129. NPY_ANYORDER
  130. NPY_CORDER
  131. NPY_FORTRANORDER
  132. NPY_KEEPORDER
  133. ctypedef enum NPY_CASTING:
  134. NPY_NO_CASTING
  135. NPY_EQUIV_CASTING
  136. NPY_SAFE_CASTING
  137. NPY_SAME_KIND_CASTING
  138. NPY_UNSAFE_CASTING
  139. NPY_SAME_VALUE_CASTING
  140. ctypedef enum NPY_CLIPMODE:
  141. NPY_CLIP
  142. NPY_WRAP
  143. NPY_RAISE
  144. ctypedef enum NPY_SCALARKIND:
  145. NPY_NOSCALAR,
  146. NPY_BOOL_SCALAR,
  147. NPY_INTPOS_SCALAR,
  148. NPY_INTNEG_SCALAR,
  149. NPY_FLOAT_SCALAR,
  150. NPY_COMPLEX_SCALAR,
  151. NPY_OBJECT_SCALAR
  152. ctypedef enum NPY_SORTKIND:
  153. NPY_QUICKSORT
  154. NPY_HEAPSORT
  155. NPY_MERGESORT
  156. ctypedef enum NPY_SEARCHSIDE:
  157. NPY_SEARCHLEFT
  158. NPY_SEARCHRIGHT
  159. enum:
  160. NPY_ARRAY_C_CONTIGUOUS
  161. NPY_ARRAY_F_CONTIGUOUS
  162. NPY_ARRAY_OWNDATA
  163. NPY_ARRAY_FORCECAST
  164. NPY_ARRAY_ENSURECOPY
  165. NPY_ARRAY_ENSUREARRAY
  166. NPY_ARRAY_ELEMENTSTRIDES
  167. NPY_ARRAY_ALIGNED
  168. NPY_ARRAY_NOTSWAPPED
  169. NPY_ARRAY_WRITEABLE
  170. NPY_ARRAY_WRITEBACKIFCOPY
  171. NPY_ARRAY_BEHAVED
  172. NPY_ARRAY_BEHAVED_NS
  173. NPY_ARRAY_CARRAY
  174. NPY_ARRAY_CARRAY_RO
  175. NPY_ARRAY_FARRAY
  176. NPY_ARRAY_FARRAY_RO
  177. NPY_ARRAY_DEFAULT
  178. NPY_ARRAY_IN_ARRAY
  179. NPY_ARRAY_OUT_ARRAY
  180. NPY_ARRAY_INOUT_ARRAY
  181. NPY_ARRAY_IN_FARRAY
  182. NPY_ARRAY_OUT_FARRAY
  183. NPY_ARRAY_INOUT_FARRAY
  184. NPY_ARRAY_UPDATE_ALL
  185. cdef enum:
  186. NPY_MAXDIMS # 64 on NumPy 2.x and 32 on NumPy 1.x
  187. NPY_RAVEL_AXIS # Used for functions like PyArray_Mean
  188. ctypedef void (*PyArray_VectorUnaryFunc)(void *, void *, npy_intp, void *, void *)
  189. ctypedef struct PyArray_ArrayDescr:
  190. # shape is a tuple, but Cython doesn't support "tuple shape"
  191. # inside a non-PyObject declaration, so we have to declare it
  192. # as just a PyObject*.
  193. PyObject* shape
  194. ctypedef struct PyArray_Descr:
  195. pass
  196. ctypedef class numpy.dtype [object PyArray_Descr, check_size ignore]:
  197. # Use PyDataType_* macros when possible, however there are no macros
  198. # for accessing some of the fields, so some are defined.
  199. cdef PyTypeObject* typeobj
  200. cdef char kind
  201. cdef char type
  202. # Numpy sometimes mutates this without warning (e.g. it'll
  203. # sometimes change "|" to "<" in shared dtype objects on
  204. # little-endian machines). If this matters to you, use
  205. # PyArray_IsNativeByteOrder(dtype.byteorder) instead of
  206. # directly accessing this field.
  207. cdef char byteorder
  208. # Flags are not directly accessible on Cython <3. Use PyDataType_FLAGS.
  209. # cdef char flags
  210. cdef int type_num
  211. # itemsize/elsize, alignment, fields, names, and subarray must
  212. # use the `PyDataType_*` accessor macros. With Cython 3 you can
  213. # still use getter attributes `dtype.itemsize`
  214. ctypedef class numpy.flatiter [object PyArrayIterObject, check_size ignore]:
  215. # Use through macros
  216. pass
  217. ctypedef class numpy.broadcast [object PyArrayMultiIterObject, check_size ignore]:
  218. cdef int numiter
  219. cdef npy_intp size, index
  220. cdef int nd
  221. cdef npy_intp *dimensions
  222. cdef void **iters
  223. ctypedef struct PyArrayObject:
  224. # For use in situations where ndarray can't replace PyArrayObject*,
  225. # like PyArrayObject**.
  226. pass
  227. ctypedef class numpy.ndarray [object PyArrayObject, check_size ignore]:
  228. cdef __cythonbufferdefaults__ = {"mode": "strided"}
  229. cdef:
  230. # Only taking a few of the most commonly used and stable fields.
  231. # One should use PyArray_* macros instead to access the C fields.
  232. char *data
  233. int ndim "nd"
  234. npy_intp *shape "dimensions"
  235. npy_intp *strides
  236. dtype descr # deprecated since NumPy 1.7 !
  237. PyObject* base # NOT PUBLIC, DO NOT USE !
  238. int _import_array() except -1
  239. # A second definition so _import_array isn't marked as used when we use it here.
  240. # Do not use - subject to change any time.
  241. int __pyx_import_array "_import_array"() except -1
  242. #
  243. # Macros from ndarrayobject.h
  244. #
  245. bint PyArray_CHKFLAGS(ndarray m, int flags) nogil
  246. bint PyArray_IS_C_CONTIGUOUS(ndarray arr) nogil
  247. bint PyArray_IS_F_CONTIGUOUS(ndarray arr) nogil
  248. bint PyArray_ISCONTIGUOUS(ndarray m) nogil
  249. bint PyArray_ISWRITEABLE(ndarray m) nogil
  250. bint PyArray_ISALIGNED(ndarray m) nogil
  251. int PyArray_NDIM(ndarray) nogil
  252. bint PyArray_ISONESEGMENT(ndarray) nogil
  253. bint PyArray_ISFORTRAN(ndarray) nogil
  254. int PyArray_FORTRANIF(ndarray) nogil
  255. void* PyArray_DATA(ndarray) nogil
  256. char* PyArray_BYTES(ndarray) nogil
  257. npy_intp* PyArray_DIMS(ndarray) nogil
  258. npy_intp* PyArray_STRIDES(ndarray) nogil
  259. npy_intp PyArray_DIM(ndarray, size_t) nogil
  260. npy_intp PyArray_STRIDE(ndarray, size_t) nogil
  261. PyObject *PyArray_BASE(ndarray) nogil # returns borrowed reference!
  262. PyArray_Descr *PyArray_DESCR(ndarray) nogil # returns borrowed reference to dtype!
  263. PyArray_Descr *PyArray_DTYPE(ndarray) nogil # returns borrowed reference to dtype! NP 1.7+ alias for descr.
  264. int PyArray_FLAGS(ndarray) nogil
  265. void PyArray_CLEARFLAGS(ndarray, int flags) nogil # Added in NumPy 1.7
  266. void PyArray_ENABLEFLAGS(ndarray, int flags) nogil # Added in NumPy 1.7
  267. npy_intp PyArray_ITEMSIZE(ndarray) nogil
  268. int PyArray_TYPE(ndarray arr) nogil
  269. object PyArray_GETITEM(ndarray arr, void *itemptr)
  270. int PyArray_SETITEM(ndarray arr, void *itemptr, object obj) except -1
  271. bint PyTypeNum_ISBOOL(int) nogil
  272. bint PyTypeNum_ISUNSIGNED(int) nogil
  273. bint PyTypeNum_ISSIGNED(int) nogil
  274. bint PyTypeNum_ISINTEGER(int) nogil
  275. bint PyTypeNum_ISFLOAT(int) nogil
  276. bint PyTypeNum_ISNUMBER(int) nogil
  277. bint PyTypeNum_ISSTRING(int) nogil
  278. bint PyTypeNum_ISCOMPLEX(int) nogil
  279. bint PyTypeNum_ISFLEXIBLE(int) nogil
  280. bint PyTypeNum_ISUSERDEF(int) nogil
  281. bint PyTypeNum_ISEXTENDED(int) nogil
  282. bint PyTypeNum_ISOBJECT(int) nogil
  283. npy_intp PyDataType_ELSIZE(dtype) nogil
  284. npy_intp PyDataType_ALIGNMENT(dtype) nogil
  285. PyObject* PyDataType_METADATA(dtype) nogil
  286. PyArray_ArrayDescr* PyDataType_SUBARRAY(dtype) nogil
  287. PyObject* PyDataType_NAMES(dtype) nogil
  288. PyObject* PyDataType_FIELDS(dtype) nogil
  289. bint PyDataType_ISBOOL(dtype) nogil
  290. bint PyDataType_ISUNSIGNED(dtype) nogil
  291. bint PyDataType_ISSIGNED(dtype) nogil
  292. bint PyDataType_ISINTEGER(dtype) nogil
  293. bint PyDataType_ISFLOAT(dtype) nogil
  294. bint PyDataType_ISNUMBER(dtype) nogil
  295. bint PyDataType_ISSTRING(dtype) nogil
  296. bint PyDataType_ISCOMPLEX(dtype) nogil
  297. bint PyDataType_ISFLEXIBLE(dtype) nogil
  298. bint PyDataType_ISUSERDEF(dtype) nogil
  299. bint PyDataType_ISEXTENDED(dtype) nogil
  300. bint PyDataType_ISOBJECT(dtype) nogil
  301. bint PyDataType_HASFIELDS(dtype) nogil
  302. bint PyDataType_HASSUBARRAY(dtype) nogil
  303. npy_uint64 PyDataType_FLAGS(dtype) nogil
  304. bint PyArray_ISBOOL(ndarray) nogil
  305. bint PyArray_ISUNSIGNED(ndarray) nogil
  306. bint PyArray_ISSIGNED(ndarray) nogil
  307. bint PyArray_ISINTEGER(ndarray) nogil
  308. bint PyArray_ISFLOAT(ndarray) nogil
  309. bint PyArray_ISNUMBER(ndarray) nogil
  310. bint PyArray_ISSTRING(ndarray) nogil
  311. bint PyArray_ISCOMPLEX(ndarray) nogil
  312. bint PyArray_ISFLEXIBLE(ndarray) nogil
  313. bint PyArray_ISUSERDEF(ndarray) nogil
  314. bint PyArray_ISEXTENDED(ndarray) nogil
  315. bint PyArray_ISOBJECT(ndarray) nogil
  316. bint PyArray_HASFIELDS(ndarray) nogil
  317. bint PyArray_ISVARIABLE(ndarray) nogil
  318. bint PyArray_SAFEALIGNEDCOPY(ndarray) nogil
  319. bint PyArray_ISNBO(char) nogil # works on ndarray.byteorder
  320. bint PyArray_IsNativeByteOrder(char) nogil # works on ndarray.byteorder
  321. bint PyArray_ISNOTSWAPPED(ndarray) nogil
  322. bint PyArray_ISBYTESWAPPED(ndarray) nogil
  323. bint PyArray_FLAGSWAP(ndarray, int) nogil
  324. bint PyArray_ISCARRAY(ndarray) nogil
  325. bint PyArray_ISCARRAY_RO(ndarray) nogil
  326. bint PyArray_ISFARRAY(ndarray) nogil
  327. bint PyArray_ISFARRAY_RO(ndarray) nogil
  328. bint PyArray_ISBEHAVED(ndarray) nogil
  329. bint PyArray_ISBEHAVED_RO(ndarray) nogil
  330. bint PyDataType_ISNOTSWAPPED(dtype) nogil
  331. bint PyDataType_ISBYTESWAPPED(dtype) nogil
  332. bint PyArray_DescrCheck(object)
  333. bint PyArray_Check(object)
  334. bint PyArray_CheckExact(object)
  335. # Cannot be supported due to out arg:
  336. # bint PyArray_HasArrayInterfaceType(object, dtype, object, object&)
  337. # bint PyArray_HasArrayInterface(op, out)
  338. bint PyArray_IsZeroDim(object)
  339. # Cannot be supported due to ## ## in macro:
  340. # bint PyArray_IsScalar(object, verbatim work)
  341. bint PyArray_CheckScalar(object)
  342. bint PyArray_IsPythonNumber(object)
  343. bint PyArray_IsPythonScalar(object)
  344. bint PyArray_IsAnyScalar(object)
  345. bint PyArray_CheckAnyScalar(object)
  346. ndarray PyArray_GETCONTIGUOUS(ndarray)
  347. bint PyArray_SAMESHAPE(ndarray, ndarray) nogil
  348. npy_intp PyArray_SIZE(ndarray) nogil
  349. npy_intp PyArray_NBYTES(ndarray) nogil
  350. object PyArray_FROM_O(object)
  351. object PyArray_FROM_OF(object m, int flags)
  352. object PyArray_FROM_OT(object m, int type)
  353. object PyArray_FROM_OTF(object m, int type, int flags)
  354. object PyArray_FROMANY(object m, int type, int min, int max, int flags)
  355. object PyArray_ZEROS(int nd, npy_intp* dims, int type, int fortran)
  356. object PyArray_EMPTY(int nd, npy_intp* dims, int type, int fortran)
  357. void PyArray_FILLWBYTE(ndarray, int val)
  358. object PyArray_ContiguousFromAny(op, int, int min_depth, int max_depth)
  359. unsigned char PyArray_EquivArrTypes(ndarray a1, ndarray a2)
  360. bint PyArray_EquivByteorders(int b1, int b2) nogil
  361. object PyArray_SimpleNew(int nd, npy_intp* dims, int typenum)
  362. object PyArray_SimpleNewFromData(int nd, npy_intp* dims, int typenum, void* data)
  363. #object PyArray_SimpleNewFromDescr(int nd, npy_intp* dims, dtype descr)
  364. object PyArray_ToScalar(void* data, ndarray arr)
  365. void* PyArray_GETPTR1(ndarray m, npy_intp i) nogil
  366. void* PyArray_GETPTR2(ndarray m, npy_intp i, npy_intp j) nogil
  367. void* PyArray_GETPTR3(ndarray m, npy_intp i, npy_intp j, npy_intp k) nogil
  368. void* PyArray_GETPTR4(ndarray m, npy_intp i, npy_intp j, npy_intp k, npy_intp l) nogil
  369. # Cannot be supported due to out arg
  370. # void PyArray_DESCR_REPLACE(descr)
  371. object PyArray_Copy(ndarray)
  372. object PyArray_FromObject(object op, int type, int min_depth, int max_depth)
  373. object PyArray_ContiguousFromObject(object op, int type, int min_depth, int max_depth)
  374. object PyArray_CopyFromObject(object op, int type, int min_depth, int max_depth)
  375. object PyArray_Cast(ndarray mp, int type_num)
  376. object PyArray_Take(ndarray ap, object items, int axis)
  377. object PyArray_Put(ndarray ap, object items, object values)
  378. void PyArray_ITER_RESET(flatiter it) nogil
  379. void PyArray_ITER_NEXT(flatiter it) nogil
  380. void PyArray_ITER_GOTO(flatiter it, npy_intp* destination) nogil
  381. void PyArray_ITER_GOTO1D(flatiter it, npy_intp ind) nogil
  382. void* PyArray_ITER_DATA(flatiter it) nogil
  383. bint PyArray_ITER_NOTDONE(flatiter it) nogil
  384. void PyArray_MultiIter_RESET(broadcast multi) nogil
  385. void PyArray_MultiIter_NEXT(broadcast multi) nogil
  386. void PyArray_MultiIter_GOTO(broadcast multi, npy_intp dest) nogil
  387. void PyArray_MultiIter_GOTO1D(broadcast multi, npy_intp ind) nogil
  388. void* PyArray_MultiIter_DATA(broadcast multi, npy_intp i) nogil
  389. void PyArray_MultiIter_NEXTi(broadcast multi, npy_intp i) nogil
  390. bint PyArray_MultiIter_NOTDONE(broadcast multi) nogil
  391. npy_intp PyArray_MultiIter_SIZE(broadcast multi) nogil
  392. int PyArray_MultiIter_NDIM(broadcast multi) nogil
  393. npy_intp PyArray_MultiIter_INDEX(broadcast multi) nogil
  394. int PyArray_MultiIter_NUMITER(broadcast multi) nogil
  395. npy_intp* PyArray_MultiIter_DIMS(broadcast multi) nogil
  396. void** PyArray_MultiIter_ITERS(broadcast multi) nogil
  397. # Functions from __multiarray_api.h
  398. # Functions taking dtype and returning object/ndarray are disabled
  399. # for now as they steal dtype references. I'm conservative and disable
  400. # more than is probably needed until it can be checked further.
  401. int PyArray_INCREF (ndarray) except * # uses PyArray_Item_INCREF...
  402. int PyArray_XDECREF (ndarray) except * # uses PyArray_Item_DECREF...
  403. dtype PyArray_DescrFromType (int)
  404. object PyArray_TypeObjectFromType (int)
  405. char * PyArray_Zero (ndarray)
  406. char * PyArray_One (ndarray)
  407. #object PyArray_CastToType (ndarray, dtype, int)
  408. int PyArray_CanCastSafely (int, int) # writes errors
  409. npy_bool PyArray_CanCastTo (dtype, dtype) # writes errors
  410. int PyArray_ObjectType (object, int) except 0
  411. dtype PyArray_DescrFromObject (object, dtype)
  412. #ndarray* PyArray_ConvertToCommonType (object, int *)
  413. dtype PyArray_DescrFromScalar (object)
  414. dtype PyArray_DescrFromTypeObject (object)
  415. npy_intp PyArray_Size (object)
  416. #object PyArray_Scalar (void *, dtype, object)
  417. #object PyArray_FromScalar (object, dtype)
  418. void PyArray_ScalarAsCtype (object, void *)
  419. #int PyArray_CastScalarToCtype (object, void *, dtype)
  420. #int PyArray_CastScalarDirect (object, dtype, void *, int)
  421. #PyArray_VectorUnaryFunc * PyArray_GetCastFunc (dtype, int)
  422. #object PyArray_FromAny (object, dtype, int, int, int, object)
  423. object PyArray_EnsureArray (object)
  424. object PyArray_EnsureAnyArray (object)
  425. #object PyArray_FromFile (stdio.FILE *, dtype, npy_intp, char *)
  426. #object PyArray_FromString (char *, npy_intp, dtype, npy_intp, char *)
  427. #object PyArray_FromBuffer (object, dtype, npy_intp, npy_intp)
  428. #object PyArray_FromIter (object, dtype, npy_intp)
  429. object PyArray_Return (ndarray)
  430. #object PyArray_GetField (ndarray, dtype, int)
  431. #int PyArray_SetField (ndarray, dtype, int, object) except -1
  432. object PyArray_Byteswap (ndarray, npy_bool)
  433. object PyArray_Resize (ndarray, PyArray_Dims *, int, NPY_ORDER)
  434. int PyArray_CopyInto (ndarray, ndarray) except -1
  435. int PyArray_CopyAnyInto (ndarray, ndarray) except -1
  436. int PyArray_CopyObject (ndarray, object) except -1
  437. object PyArray_NewCopy (ndarray, NPY_ORDER)
  438. object PyArray_ToList (ndarray)
  439. object PyArray_ToString (ndarray, NPY_ORDER)
  440. int PyArray_ToFile (ndarray, stdio.FILE *, char *, char *) except -1
  441. int PyArray_Dump (object, object, int) except -1
  442. object PyArray_Dumps (object, int)
  443. int PyArray_ValidType (int) # Cannot error
  444. void PyArray_UpdateFlags (ndarray, int)
  445. object PyArray_New (type, int, npy_intp *, int, npy_intp *, void *, int, int, object)
  446. #object PyArray_NewFromDescr (type, dtype, int, npy_intp *, npy_intp *, void *, int, object)
  447. #dtype PyArray_DescrNew (dtype)
  448. dtype PyArray_DescrNewFromType (int)
  449. double PyArray_GetPriority (object, double) # clears errors as of 1.25
  450. object PyArray_IterNew (object)
  451. object PyArray_MultiIterNew (int, ...)
  452. int PyArray_PyIntAsInt (object) except? -1
  453. npy_intp PyArray_PyIntAsIntp (object)
  454. int PyArray_Broadcast (broadcast) except -1
  455. int PyArray_FillWithScalar (ndarray, object) except -1
  456. npy_bool PyArray_CheckStrides (int, int, npy_intp, npy_intp, npy_intp *, npy_intp *)
  457. dtype PyArray_DescrNewByteorder (dtype, char)
  458. object PyArray_IterAllButAxis (object, int *)
  459. #object PyArray_CheckFromAny (object, dtype, int, int, int, object)
  460. #object PyArray_FromArray (ndarray, dtype, int)
  461. object PyArray_FromInterface (object)
  462. object PyArray_FromStructInterface (object)
  463. #object PyArray_FromArrayAttr (object, dtype, object)
  464. #NPY_SCALARKIND PyArray_ScalarKind (int, ndarray*)
  465. int PyArray_CanCoerceScalar (int, int, NPY_SCALARKIND)
  466. npy_bool PyArray_CanCastScalar (type, type)
  467. int PyArray_RemoveSmallest (broadcast) except -1
  468. int PyArray_ElementStrides (object)
  469. void PyArray_Item_INCREF (char *, dtype) except *
  470. void PyArray_Item_XDECREF (char *, dtype) except *
  471. object PyArray_Transpose (ndarray, PyArray_Dims *)
  472. object PyArray_TakeFrom (ndarray, object, int, ndarray, NPY_CLIPMODE)
  473. object PyArray_PutTo (ndarray, object, object, NPY_CLIPMODE)
  474. object PyArray_PutMask (ndarray, object, object)
  475. object PyArray_Repeat (ndarray, object, int)
  476. object PyArray_Choose (ndarray, object, ndarray, NPY_CLIPMODE)
  477. int PyArray_Sort (ndarray, int, NPY_SORTKIND) except -1
  478. object PyArray_ArgSort (ndarray, int, NPY_SORTKIND)
  479. object PyArray_SearchSorted (ndarray, object, NPY_SEARCHSIDE, PyObject *)
  480. object PyArray_ArgMax (ndarray, int, ndarray)
  481. object PyArray_ArgMin (ndarray, int, ndarray)
  482. object PyArray_Reshape (ndarray, object)
  483. object PyArray_Newshape (ndarray, PyArray_Dims *, NPY_ORDER)
  484. object PyArray_Squeeze (ndarray)
  485. #object PyArray_View (ndarray, dtype, type)
  486. object PyArray_SwapAxes (ndarray, int, int)
  487. object PyArray_Max (ndarray, int, ndarray)
  488. object PyArray_Min (ndarray, int, ndarray)
  489. object PyArray_Ptp (ndarray, int, ndarray)
  490. object PyArray_Mean (ndarray, int, int, ndarray)
  491. object PyArray_Trace (ndarray, int, int, int, int, ndarray)
  492. object PyArray_Diagonal (ndarray, int, int, int)
  493. object PyArray_Clip (ndarray, object, object, ndarray)
  494. object PyArray_Conjugate (ndarray, ndarray)
  495. object PyArray_Nonzero (ndarray)
  496. object PyArray_Std (ndarray, int, int, ndarray, int)
  497. object PyArray_Sum (ndarray, int, int, ndarray)
  498. object PyArray_CumSum (ndarray, int, int, ndarray)
  499. object PyArray_Prod (ndarray, int, int, ndarray)
  500. object PyArray_CumProd (ndarray, int, int, ndarray)
  501. object PyArray_All (ndarray, int, ndarray)
  502. object PyArray_Any (ndarray, int, ndarray)
  503. object PyArray_Compress (ndarray, object, int, ndarray)
  504. object PyArray_Flatten (ndarray, NPY_ORDER)
  505. object PyArray_Ravel (ndarray, NPY_ORDER)
  506. npy_intp PyArray_MultiplyList (npy_intp *, int)
  507. int PyArray_MultiplyIntList (int *, int)
  508. void * PyArray_GetPtr (ndarray, npy_intp*)
  509. int PyArray_CompareLists (npy_intp *, npy_intp *, int)
  510. #int PyArray_AsCArray (object*, void *, npy_intp *, int, dtype)
  511. int PyArray_Free (object, void *)
  512. #int PyArray_Converter (object, object*)
  513. int PyArray_IntpFromSequence (object, npy_intp *, int) except -1
  514. object PyArray_Concatenate (object, int)
  515. object PyArray_InnerProduct (object, object)
  516. object PyArray_MatrixProduct (object, object)
  517. object PyArray_Correlate (object, object, int)
  518. #int PyArray_DescrConverter (object, dtype*) except 0
  519. #int PyArray_DescrConverter2 (object, dtype*) except 0
  520. int PyArray_IntpConverter (object, PyArray_Dims *) except 0
  521. #int PyArray_BufferConverter (object, chunk) except 0
  522. int PyArray_AxisConverter (object, int *) except 0
  523. int PyArray_BoolConverter (object, npy_bool *) except 0
  524. int PyArray_ByteorderConverter (object, char *) except 0
  525. int PyArray_OrderConverter (object, NPY_ORDER *) except 0
  526. unsigned char PyArray_EquivTypes (dtype, dtype) # clears errors
  527. #object PyArray_Zeros (int, npy_intp *, dtype, int)
  528. #object PyArray_Empty (int, npy_intp *, dtype, int)
  529. object PyArray_Where (object, object, object)
  530. object PyArray_Arange (double, double, double, int)
  531. #object PyArray_ArangeObj (object, object, object, dtype)
  532. int PyArray_SortkindConverter (object, NPY_SORTKIND *) except 0
  533. object PyArray_LexSort (object, int)
  534. object PyArray_Round (ndarray, int, ndarray)
  535. unsigned char PyArray_EquivTypenums (int, int)
  536. int PyArray_RegisterDataType (dtype) except -1
  537. int PyArray_RegisterCastFunc (dtype, int, PyArray_VectorUnaryFunc *) except -1
  538. int PyArray_RegisterCanCast (dtype, int, NPY_SCALARKIND) except -1
  539. #void PyArray_InitArrFuncs (PyArray_ArrFuncs *)
  540. object PyArray_IntTupleFromIntp (int, npy_intp *)
  541. int PyArray_ClipmodeConverter (object, NPY_CLIPMODE *) except 0
  542. #int PyArray_OutputConverter (object, ndarray*) except 0
  543. object PyArray_BroadcastToShape (object, npy_intp *, int)
  544. #int PyArray_DescrAlignConverter (object, dtype*) except 0
  545. #int PyArray_DescrAlignConverter2 (object, dtype*) except 0
  546. int PyArray_SearchsideConverter (object, void *) except 0
  547. object PyArray_CheckAxis (ndarray, int *, int)
  548. npy_intp PyArray_OverflowMultiplyList (npy_intp *, int)
  549. int PyArray_SetBaseObject(ndarray, base) except -1 # NOTE: steals a reference to base! Use "set_array_base()" instead.
  550. # The memory handler functions require the NumPy 1.22 API
  551. # and may require defining NPY_TARGET_VERSION
  552. ctypedef struct PyDataMemAllocator:
  553. void *ctx
  554. void* (*malloc) (void *ctx, size_t size)
  555. void* (*calloc) (void *ctx, size_t nelem, size_t elsize)
  556. void* (*realloc) (void *ctx, void *ptr, size_t new_size)
  557. void (*free) (void *ctx, void *ptr, size_t size)
  558. ctypedef struct PyDataMem_Handler:
  559. char* name
  560. npy_uint8 version
  561. PyDataMemAllocator allocator
  562. object PyDataMem_SetHandler(object handler)
  563. object PyDataMem_GetHandler()
  564. # additional datetime related functions are defined below
  565. # Typedefs that matches the runtime dtype objects in
  566. # the numpy module.
  567. # The ones that are commented out needs an IFDEF function
  568. # in Cython to enable them only on the right systems.
  569. ctypedef npy_int8 int8_t
  570. ctypedef npy_int16 int16_t
  571. ctypedef npy_int32 int32_t
  572. ctypedef npy_int64 int64_t
  573. ctypedef npy_uint8 uint8_t
  574. ctypedef npy_uint16 uint16_t
  575. ctypedef npy_uint32 uint32_t
  576. ctypedef npy_uint64 uint64_t
  577. ctypedef npy_float32 float32_t
  578. ctypedef npy_float64 float64_t
  579. #ctypedef npy_float80 float80_t
  580. #ctypedef npy_float128 float128_t
  581. ctypedef float complex complex64_t
  582. ctypedef double complex complex128_t
  583. ctypedef npy_longlong longlong_t
  584. ctypedef npy_ulonglong ulonglong_t
  585. ctypedef npy_intp intp_t
  586. ctypedef npy_uintp uintp_t
  587. ctypedef npy_double float_t
  588. ctypedef npy_double double_t
  589. ctypedef npy_longdouble longdouble_t
  590. ctypedef float complex cfloat_t
  591. ctypedef double complex cdouble_t
  592. ctypedef double complex complex_t
  593. ctypedef long double complex clongdouble_t
  594. cdef inline object PyArray_MultiIterNew1(a):
  595. return PyArray_MultiIterNew(1, <void*>a)
  596. cdef inline object PyArray_MultiIterNew2(a, b):
  597. return PyArray_MultiIterNew(2, <void*>a, <void*>b)
  598. cdef inline object PyArray_MultiIterNew3(a, b, c):
  599. return PyArray_MultiIterNew(3, <void*>a, <void*>b, <void*> c)
  600. cdef inline object PyArray_MultiIterNew4(a, b, c, d):
  601. return PyArray_MultiIterNew(4, <void*>a, <void*>b, <void*>c, <void*> d)
  602. cdef inline object PyArray_MultiIterNew5(a, b, c, d, e):
  603. return PyArray_MultiIterNew(5, <void*>a, <void*>b, <void*>c, <void*> d, <void*> e)
  604. cdef inline tuple PyDataType_SHAPE(dtype d):
  605. if PyDataType_HASSUBARRAY(d):
  606. return <tuple>d.subarray.shape
  607. else:
  608. return ()
  609. cdef extern from "numpy/ndarrayobject.h":
  610. PyTypeObject PyTimedeltaArrType_Type
  611. PyTypeObject PyDatetimeArrType_Type
  612. ctypedef int64_t npy_timedelta
  613. ctypedef int64_t npy_datetime
  614. cdef extern from "numpy/ndarraytypes.h":
  615. ctypedef struct PyArray_DatetimeMetaData:
  616. NPY_DATETIMEUNIT base
  617. int64_t num
  618. ctypedef struct npy_datetimestruct:
  619. int64_t year
  620. int32_t month, day, hour, min, sec, us, ps, as
  621. # Iterator API added in v1.6
  622. #
  623. # These don't match the definition in the C API because Cython can't wrap
  624. # function pointers that return functions.
  625. # https://github.com/cython/cython/issues/6720
  626. ctypedef int (*NpyIter_IterNextFunc "NpyIter_IterNextFunc *")(NpyIter* it) noexcept nogil
  627. ctypedef void (*NpyIter_GetMultiIndexFunc "NpyIter_GetMultiIndexFunc *")(NpyIter* it, npy_intp* outcoords) noexcept nogil
  628. cdef extern from "numpy/arrayscalars.h":
  629. # abstract types
  630. ctypedef class numpy.generic [object PyObject]:
  631. pass
  632. ctypedef class numpy.number [object PyObject]:
  633. pass
  634. ctypedef class numpy.integer [object PyObject]:
  635. pass
  636. ctypedef class numpy.signedinteger [object PyObject]:
  637. pass
  638. ctypedef class numpy.unsignedinteger [object PyObject]:
  639. pass
  640. ctypedef class numpy.inexact [object PyObject]:
  641. pass
  642. ctypedef class numpy.floating [object PyObject]:
  643. pass
  644. ctypedef class numpy.complexfloating [object PyObject]:
  645. pass
  646. ctypedef class numpy.flexible [object PyObject]:
  647. pass
  648. ctypedef class numpy.character [object PyObject]:
  649. pass
  650. ctypedef struct PyDatetimeScalarObject:
  651. # PyObject_HEAD
  652. npy_datetime obval
  653. PyArray_DatetimeMetaData obmeta
  654. ctypedef struct PyTimedeltaScalarObject:
  655. # PyObject_HEAD
  656. npy_timedelta obval
  657. PyArray_DatetimeMetaData obmeta
  658. ctypedef enum NPY_DATETIMEUNIT:
  659. NPY_FR_Y
  660. NPY_FR_M
  661. NPY_FR_W
  662. NPY_FR_D
  663. NPY_FR_B
  664. NPY_FR_h
  665. NPY_FR_m
  666. NPY_FR_s
  667. NPY_FR_ms
  668. NPY_FR_us
  669. NPY_FR_ns
  670. NPY_FR_ps
  671. NPY_FR_fs
  672. NPY_FR_as
  673. NPY_FR_GENERIC
  674. cdef extern from "numpy/arrayobject.h":
  675. # These are part of the C-API defined in `__multiarray_api.h`
  676. # NumPy internal definitions in datetime_strings.c:
  677. int get_datetime_iso_8601_strlen "NpyDatetime_GetDatetimeISO8601StrLen" (
  678. int local, NPY_DATETIMEUNIT base)
  679. int make_iso_8601_datetime "NpyDatetime_MakeISO8601Datetime" (
  680. npy_datetimestruct *dts, char *outstr, npy_intp outlen,
  681. int local, int utc, NPY_DATETIMEUNIT base, int tzoffset,
  682. NPY_CASTING casting) except -1
  683. # NumPy internal definition in datetime.c:
  684. # May return 1 to indicate that object does not appear to be a datetime
  685. # (returns 0 on success).
  686. int convert_pydatetime_to_datetimestruct "NpyDatetime_ConvertPyDateTimeToDatetimeStruct" (
  687. PyObject *obj, npy_datetimestruct *out,
  688. NPY_DATETIMEUNIT *out_bestunit, int apply_tzinfo) except -1
  689. int convert_datetime64_to_datetimestruct "NpyDatetime_ConvertDatetime64ToDatetimeStruct" (
  690. PyArray_DatetimeMetaData *meta, npy_datetime dt,
  691. npy_datetimestruct *out) except -1
  692. int convert_datetimestruct_to_datetime64 "NpyDatetime_ConvertDatetimeStructToDatetime64"(
  693. PyArray_DatetimeMetaData *meta, const npy_datetimestruct *dts,
  694. npy_datetime *out) except -1
  695. #
  696. # ufunc API
  697. #
  698. cdef extern from "numpy/ufuncobject.h":
  699. ctypedef void (*PyUFuncGenericFunction) (char **, npy_intp *, npy_intp *, void *)
  700. ctypedef class numpy.ufunc [object PyUFuncObject, check_size ignore]:
  701. cdef:
  702. int nin, nout, nargs
  703. int identity
  704. PyUFuncGenericFunction *functions
  705. void **data
  706. int ntypes
  707. int check_return
  708. char *name
  709. char *types
  710. char *doc
  711. void *ptr
  712. PyObject *obj
  713. PyObject *userloops
  714. cdef enum:
  715. PyUFunc_Zero
  716. PyUFunc_One
  717. PyUFunc_None
  718. # deprecated
  719. UFUNC_FPE_DIVIDEBYZERO
  720. UFUNC_FPE_OVERFLOW
  721. UFUNC_FPE_UNDERFLOW
  722. UFUNC_FPE_INVALID
  723. # use these instead
  724. NPY_FPE_DIVIDEBYZERO
  725. NPY_FPE_OVERFLOW
  726. NPY_FPE_UNDERFLOW
  727. NPY_FPE_INVALID
  728. object PyUFunc_FromFuncAndData(PyUFuncGenericFunction *,
  729. void **, char *, int, int, int, int, char *, char *, int)
  730. int PyUFunc_RegisterLoopForType(ufunc, int,
  731. PyUFuncGenericFunction, int *, void *) except -1
  732. void PyUFunc_f_f_As_d_d \
  733. (char **, npy_intp *, npy_intp *, void *)
  734. void PyUFunc_d_d \
  735. (char **, npy_intp *, npy_intp *, void *)
  736. void PyUFunc_f_f \
  737. (char **, npy_intp *, npy_intp *, void *)
  738. void PyUFunc_g_g \
  739. (char **, npy_intp *, npy_intp *, void *)
  740. void PyUFunc_F_F_As_D_D \
  741. (char **, npy_intp *, npy_intp *, void *)
  742. void PyUFunc_F_F \
  743. (char **, npy_intp *, npy_intp *, void *)
  744. void PyUFunc_D_D \
  745. (char **, npy_intp *, npy_intp *, void *)
  746. void PyUFunc_G_G \
  747. (char **, npy_intp *, npy_intp *, void *)
  748. void PyUFunc_O_O \
  749. (char **, npy_intp *, npy_intp *, void *)
  750. void PyUFunc_ff_f_As_dd_d \
  751. (char **, npy_intp *, npy_intp *, void *)
  752. void PyUFunc_ff_f \
  753. (char **, npy_intp *, npy_intp *, void *)
  754. void PyUFunc_dd_d \
  755. (char **, npy_intp *, npy_intp *, void *)
  756. void PyUFunc_gg_g \
  757. (char **, npy_intp *, npy_intp *, void *)
  758. void PyUFunc_FF_F_As_DD_D \
  759. (char **, npy_intp *, npy_intp *, void *)
  760. void PyUFunc_DD_D \
  761. (char **, npy_intp *, npy_intp *, void *)
  762. void PyUFunc_FF_F \
  763. (char **, npy_intp *, npy_intp *, void *)
  764. void PyUFunc_GG_G \
  765. (char **, npy_intp *, npy_intp *, void *)
  766. void PyUFunc_OO_O \
  767. (char **, npy_intp *, npy_intp *, void *)
  768. void PyUFunc_O_O_method \
  769. (char **, npy_intp *, npy_intp *, void *)
  770. void PyUFunc_OO_O_method \
  771. (char **, npy_intp *, npy_intp *, void *)
  772. void PyUFunc_On_Om \
  773. (char **, npy_intp *, npy_intp *, void *)
  774. void PyUFunc_clearfperr()
  775. int PyUFunc_getfperr()
  776. int PyUFunc_ReplaceLoopBySignature \
  777. (ufunc, PyUFuncGenericFunction, int *, PyUFuncGenericFunction *)
  778. object PyUFunc_FromFuncAndDataAndSignature \
  779. (PyUFuncGenericFunction *, void **, char *, int, int, int,
  780. int, char *, char *, int, char *)
  781. int _import_umath() except -1
  782. cdef inline void set_array_base(ndarray arr, object base):
  783. Py_INCREF(base) # important to do this before stealing the reference below!
  784. PyArray_SetBaseObject(arr, base)
  785. cdef inline object get_array_base(ndarray arr):
  786. base = PyArray_BASE(arr)
  787. if base is NULL:
  788. return None
  789. return <object>base
  790. # Versions of the import_* functions which are more suitable for
  791. # Cython code.
  792. cdef inline int import_array() except -1:
  793. try:
  794. __pyx_import_array()
  795. except Exception:
  796. raise ImportError("numpy._core.multiarray failed to import")
  797. cdef inline int import_umath() except -1:
  798. try:
  799. _import_umath()
  800. except Exception:
  801. raise ImportError("numpy._core.umath failed to import")
  802. cdef inline int import_ufunc() except -1:
  803. try:
  804. _import_umath()
  805. except Exception:
  806. raise ImportError("numpy._core.umath failed to import")
  807. cdef inline bint is_timedelta64_object(object obj):
  808. """
  809. Cython equivalent of `isinstance(obj, np.timedelta64)`
  810. Parameters
  811. ----------
  812. obj : object
  813. Returns
  814. -------
  815. bool
  816. """
  817. return PyObject_TypeCheck(obj, &PyTimedeltaArrType_Type)
  818. cdef inline bint is_datetime64_object(object obj):
  819. """
  820. Cython equivalent of `isinstance(obj, np.datetime64)`
  821. Parameters
  822. ----------
  823. obj : object
  824. Returns
  825. -------
  826. bool
  827. """
  828. return PyObject_TypeCheck(obj, &PyDatetimeArrType_Type)
  829. cdef inline npy_datetime get_datetime64_value(object obj) nogil:
  830. """
  831. returns the int64 value underlying scalar numpy datetime64 object
  832. Note that to interpret this as a datetime, the corresponding unit is
  833. also needed. That can be found using `get_datetime64_unit`.
  834. """
  835. return (<PyDatetimeScalarObject*>obj).obval
  836. cdef inline npy_timedelta get_timedelta64_value(object obj) nogil:
  837. """
  838. returns the int64 value underlying scalar numpy timedelta64 object
  839. """
  840. return (<PyTimedeltaScalarObject*>obj).obval
  841. cdef inline NPY_DATETIMEUNIT get_datetime64_unit(object obj) nogil:
  842. """
  843. returns the unit part of the dtype for a numpy datetime64 object.
  844. """
  845. return <NPY_DATETIMEUNIT>(<PyDatetimeScalarObject*>obj).obmeta.base
  846. cdef extern from "numpy/arrayobject.h":
  847. ctypedef struct NpyIter:
  848. pass
  849. cdef enum:
  850. NPY_FAIL
  851. NPY_SUCCEED
  852. cdef enum:
  853. # Track an index representing C order
  854. NPY_ITER_C_INDEX
  855. # Track an index representing Fortran order
  856. NPY_ITER_F_INDEX
  857. # Track a multi-index
  858. NPY_ITER_MULTI_INDEX
  859. # User code external to the iterator does the 1-dimensional innermost loop
  860. NPY_ITER_EXTERNAL_LOOP
  861. # Convert all the operands to a common data type
  862. NPY_ITER_COMMON_DTYPE
  863. # Operands may hold references, requiring API access during iteration
  864. NPY_ITER_REFS_OK
  865. # Zero-sized operands should be permitted, iteration checks IterSize for 0
  866. NPY_ITER_ZEROSIZE_OK
  867. # Permits reductions (size-0 stride with dimension size > 1)
  868. NPY_ITER_REDUCE_OK
  869. # Enables sub-range iteration
  870. NPY_ITER_RANGED
  871. # Enables buffering
  872. NPY_ITER_BUFFERED
  873. # When buffering is enabled, grows the inner loop if possible
  874. NPY_ITER_GROWINNER
  875. # Delay allocation of buffers until first Reset* call
  876. NPY_ITER_DELAY_BUFALLOC
  877. # When NPY_KEEPORDER is specified, disable reversing negative-stride axes
  878. NPY_ITER_DONT_NEGATE_STRIDES
  879. NPY_ITER_COPY_IF_OVERLAP
  880. # The operand will be read from and written to
  881. NPY_ITER_READWRITE
  882. # The operand will only be read from
  883. NPY_ITER_READONLY
  884. # The operand will only be written to
  885. NPY_ITER_WRITEONLY
  886. # The operand's data must be in native byte order
  887. NPY_ITER_NBO
  888. # The operand's data must be aligned
  889. NPY_ITER_ALIGNED
  890. # The operand's data must be contiguous (within the inner loop)
  891. NPY_ITER_CONTIG
  892. # The operand may be copied to satisfy requirements
  893. NPY_ITER_COPY
  894. # The operand may be copied with WRITEBACKIFCOPY to satisfy requirements
  895. NPY_ITER_UPDATEIFCOPY
  896. # Allocate the operand if it is NULL
  897. NPY_ITER_ALLOCATE
  898. # If an operand is allocated, don't use any subtype
  899. NPY_ITER_NO_SUBTYPE
  900. # This is a virtual array slot, operand is NULL but temporary data is there
  901. NPY_ITER_VIRTUAL
  902. # Require that the dimension match the iterator dimensions exactly
  903. NPY_ITER_NO_BROADCAST
  904. # A mask is being used on this array, affects buffer -> array copy
  905. NPY_ITER_WRITEMASKED
  906. # This array is the mask for all WRITEMASKED operands
  907. NPY_ITER_ARRAYMASK
  908. # Assume iterator order data access for COPY_IF_OVERLAP
  909. NPY_ITER_OVERLAP_ASSUME_ELEMENTWISE
  910. # construction and destruction functions
  911. NpyIter* NpyIter_New(ndarray arr, npy_uint32 flags, NPY_ORDER order,
  912. NPY_CASTING casting, dtype datatype) except NULL
  913. NpyIter* NpyIter_MultiNew(npy_intp nop, PyArrayObject** op, npy_uint32 flags,
  914. NPY_ORDER order, NPY_CASTING casting, npy_uint32*
  915. op_flags, PyArray_Descr** op_dtypes) except NULL
  916. NpyIter* NpyIter_AdvancedNew(npy_intp nop, PyArrayObject** op,
  917. npy_uint32 flags, NPY_ORDER order,
  918. NPY_CASTING casting, npy_uint32* op_flags,
  919. PyArray_Descr** op_dtypes, int oa_ndim,
  920. int** op_axes, const npy_intp* itershape,
  921. npy_intp buffersize) except NULL
  922. NpyIter* NpyIter_Copy(NpyIter* it) except NULL
  923. int NpyIter_RemoveAxis(NpyIter* it, int axis) except NPY_FAIL
  924. int NpyIter_RemoveMultiIndex(NpyIter* it) except NPY_FAIL
  925. int NpyIter_EnableExternalLoop(NpyIter* it) except NPY_FAIL
  926. int NpyIter_Deallocate(NpyIter* it) except NPY_FAIL
  927. int NpyIter_Reset(NpyIter* it, char** errmsg) except NPY_FAIL
  928. int NpyIter_ResetToIterIndexRange(NpyIter* it, npy_intp istart,
  929. npy_intp iend, char** errmsg) except NPY_FAIL
  930. int NpyIter_ResetBasePointers(NpyIter* it, char** baseptrs, char** errmsg) except NPY_FAIL
  931. int NpyIter_GotoMultiIndex(NpyIter* it, const npy_intp* multi_index) except NPY_FAIL
  932. int NpyIter_GotoIndex(NpyIter* it, npy_intp index) except NPY_FAIL
  933. npy_intp NpyIter_GetIterSize(NpyIter* it) nogil
  934. npy_intp NpyIter_GetIterIndex(NpyIter* it) nogil
  935. void NpyIter_GetIterIndexRange(NpyIter* it, npy_intp* istart,
  936. npy_intp* iend) nogil
  937. int NpyIter_GotoIterIndex(NpyIter* it, npy_intp iterindex) except NPY_FAIL
  938. npy_bool NpyIter_HasDelayedBufAlloc(NpyIter* it) nogil
  939. npy_bool NpyIter_HasExternalLoop(NpyIter* it) nogil
  940. npy_bool NpyIter_HasMultiIndex(NpyIter* it) nogil
  941. npy_bool NpyIter_HasIndex(NpyIter* it) nogil
  942. npy_bool NpyIter_RequiresBuffering(NpyIter* it) nogil
  943. npy_bool NpyIter_IsBuffered(NpyIter* it) nogil
  944. npy_bool NpyIter_IsGrowInner(NpyIter* it) nogil
  945. npy_intp NpyIter_GetBufferSize(NpyIter* it) nogil
  946. int NpyIter_GetNDim(NpyIter* it) nogil
  947. int NpyIter_GetNOp(NpyIter* it) nogil
  948. npy_intp* NpyIter_GetAxisStrideArray(NpyIter* it, int axis) except NULL
  949. int NpyIter_GetShape(NpyIter* it, npy_intp* outshape) nogil
  950. PyArray_Descr** NpyIter_GetDescrArray(NpyIter* it)
  951. PyArrayObject** NpyIter_GetOperandArray(NpyIter* it)
  952. ndarray NpyIter_GetIterView(NpyIter* it, npy_intp i)
  953. void NpyIter_GetReadFlags(NpyIter* it, char* outreadflags)
  954. void NpyIter_GetWriteFlags(NpyIter* it, char* outwriteflags)
  955. int NpyIter_CreateCompatibleStrides(NpyIter* it, npy_intp itemsize,
  956. npy_intp* outstrides) except NPY_FAIL
  957. npy_bool NpyIter_IsFirstVisit(NpyIter* it, int iop) nogil
  958. # functions for iterating an NpyIter object
  959. #
  960. # These don't match the definition in the C API because Cython can't wrap
  961. # function pointers that return functions.
  962. NpyIter_IterNextFunc* NpyIter_GetIterNext(NpyIter* it, char** errmsg) except NULL
  963. NpyIter_GetMultiIndexFunc* NpyIter_GetGetMultiIndex(NpyIter* it,
  964. char** errmsg) except NULL
  965. char** NpyIter_GetDataPtrArray(NpyIter* it) nogil
  966. char** NpyIter_GetInitialDataPtrArray(NpyIter* it) nogil
  967. npy_intp* NpyIter_GetIndexPtr(NpyIter* it)
  968. npy_intp* NpyIter_GetInnerStrideArray(NpyIter* it) nogil
  969. npy_intp* NpyIter_GetInnerLoopSizePtr(NpyIter* it) nogil
  970. void NpyIter_GetInnerFixedStrideArray(NpyIter* it, npy_intp* outstrides) nogil
  971. npy_bool NpyIter_IterationNeedsAPI(NpyIter* it) nogil
  972. void NpyIter_DebugPrint(NpyIter* it)
  973. # NpyString API
  974. cdef extern from "numpy/ndarraytypes.h":
  975. ctypedef struct npy_string_allocator:
  976. pass
  977. ctypedef struct npy_packed_static_string:
  978. pass
  979. ctypedef struct npy_static_string:
  980. size_t size
  981. const char *buf
  982. ctypedef struct PyArray_StringDTypeObject:
  983. PyArray_Descr base
  984. PyObject *na_object
  985. char coerce
  986. char has_nan_na
  987. char has_string_na
  988. char array_owned
  989. npy_static_string default_string
  990. npy_static_string na_name
  991. npy_string_allocator *allocator
  992. cdef extern from "numpy/arrayobject.h":
  993. npy_string_allocator *NpyString_acquire_allocator(const PyArray_StringDTypeObject *descr)
  994. void NpyString_acquire_allocators(size_t n_descriptors, PyArray_Descr *const descrs[], npy_string_allocator *allocators[])
  995. void NpyString_release_allocator(npy_string_allocator *allocator)
  996. void NpyString_release_allocators(size_t length, npy_string_allocator *allocators[])
  997. int NpyString_load(npy_string_allocator *allocator, const npy_packed_static_string *packed_string, npy_static_string *unpacked_string)
  998. int NpyString_pack_null(npy_string_allocator *allocator, npy_packed_static_string *packed_string)
  999. int NpyString_pack(npy_string_allocator *allocator, npy_packed_static_string *packed_string, const char *buf, size_t size)