__init__.pxd 43 KB

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