__init__.cython-30.pxd 46 KB

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