__init__.pxd 42 KB

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