__init__.cython-30.pxd 46 KB

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