__init__.cython-30.pxd 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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 "Python.h":
  19. ctypedef Py_ssize_t Py_intptr_t
  20. cdef extern from "numpy/arrayobject.h":
  21. ctypedef Py_intptr_t npy_intp
  22. ctypedef size_t npy_uintp
  23. cdef enum NPY_TYPES:
  24. NPY_BOOL
  25. NPY_BYTE
  26. NPY_UBYTE
  27. NPY_SHORT
  28. NPY_USHORT
  29. NPY_INT
  30. NPY_UINT
  31. NPY_LONG
  32. NPY_ULONG
  33. NPY_LONGLONG
  34. NPY_ULONGLONG
  35. NPY_FLOAT
  36. NPY_DOUBLE
  37. NPY_LONGDOUBLE
  38. NPY_CFLOAT
  39. NPY_CDOUBLE
  40. NPY_CLONGDOUBLE
  41. NPY_OBJECT
  42. NPY_STRING
  43. NPY_UNICODE
  44. NPY_VOID
  45. NPY_DATETIME
  46. NPY_TIMEDELTA
  47. NPY_NTYPES
  48. NPY_NOTYPE
  49. NPY_INT8
  50. NPY_INT16
  51. NPY_INT32
  52. NPY_INT64
  53. NPY_INT128
  54. NPY_INT256
  55. NPY_UINT8
  56. NPY_UINT16
  57. NPY_UINT32
  58. NPY_UINT64
  59. NPY_UINT128
  60. NPY_UINT256
  61. NPY_FLOAT16
  62. NPY_FLOAT32
  63. NPY_FLOAT64
  64. NPY_FLOAT80
  65. NPY_FLOAT96
  66. NPY_FLOAT128
  67. NPY_FLOAT256
  68. NPY_COMPLEX32
  69. NPY_COMPLEX64
  70. NPY_COMPLEX128
  71. NPY_COMPLEX160
  72. NPY_COMPLEX192
  73. NPY_COMPLEX256
  74. NPY_COMPLEX512
  75. NPY_INTP
  76. ctypedef enum NPY_ORDER:
  77. NPY_ANYORDER
  78. NPY_CORDER
  79. NPY_FORTRANORDER
  80. NPY_KEEPORDER
  81. ctypedef enum NPY_CASTING:
  82. NPY_NO_CASTING
  83. NPY_EQUIV_CASTING
  84. NPY_SAFE_CASTING
  85. NPY_SAME_KIND_CASTING
  86. NPY_UNSAFE_CASTING
  87. ctypedef enum NPY_CLIPMODE:
  88. NPY_CLIP
  89. NPY_WRAP
  90. NPY_RAISE
  91. ctypedef enum NPY_SCALARKIND:
  92. NPY_NOSCALAR,
  93. NPY_BOOL_SCALAR,
  94. NPY_INTPOS_SCALAR,
  95. NPY_INTNEG_SCALAR,
  96. NPY_FLOAT_SCALAR,
  97. NPY_COMPLEX_SCALAR,
  98. NPY_OBJECT_SCALAR
  99. ctypedef enum NPY_SORTKIND:
  100. NPY_QUICKSORT
  101. NPY_HEAPSORT
  102. NPY_MERGESORT
  103. ctypedef enum NPY_SEARCHSIDE:
  104. NPY_SEARCHLEFT
  105. NPY_SEARCHRIGHT
  106. enum:
  107. # DEPRECATED since NumPy 1.7 ! Do not use in new code!
  108. NPY_C_CONTIGUOUS
  109. NPY_F_CONTIGUOUS
  110. NPY_CONTIGUOUS
  111. NPY_FORTRAN
  112. NPY_OWNDATA
  113. NPY_FORCECAST
  114. NPY_ENSURECOPY
  115. NPY_ENSUREARRAY
  116. NPY_ELEMENTSTRIDES
  117. NPY_ALIGNED
  118. NPY_NOTSWAPPED
  119. NPY_WRITEABLE
  120. NPY_UPDATEIFCOPY
  121. NPY_ARR_HAS_DESCR
  122. NPY_BEHAVED
  123. NPY_BEHAVED_NS
  124. NPY_CARRAY
  125. NPY_CARRAY_RO
  126. NPY_FARRAY
  127. NPY_FARRAY_RO
  128. NPY_DEFAULT
  129. NPY_IN_ARRAY
  130. NPY_OUT_ARRAY
  131. NPY_INOUT_ARRAY
  132. NPY_IN_FARRAY
  133. NPY_OUT_FARRAY
  134. NPY_INOUT_FARRAY
  135. NPY_UPDATE_ALL
  136. enum:
  137. # Added in NumPy 1.7 to replace the deprecated enums above.
  138. NPY_ARRAY_C_CONTIGUOUS
  139. NPY_ARRAY_F_CONTIGUOUS
  140. NPY_ARRAY_OWNDATA
  141. NPY_ARRAY_FORCECAST
  142. NPY_ARRAY_ENSURECOPY
  143. NPY_ARRAY_ENSUREARRAY
  144. NPY_ARRAY_ELEMENTSTRIDES
  145. NPY_ARRAY_ALIGNED
  146. NPY_ARRAY_NOTSWAPPED
  147. NPY_ARRAY_WRITEABLE
  148. NPY_ARRAY_UPDATEIFCOPY
  149. NPY_ARRAY_BEHAVED
  150. NPY_ARRAY_BEHAVED_NS
  151. NPY_ARRAY_CARRAY
  152. NPY_ARRAY_CARRAY_RO
  153. NPY_ARRAY_FARRAY
  154. NPY_ARRAY_FARRAY_RO
  155. NPY_ARRAY_DEFAULT
  156. NPY_ARRAY_IN_ARRAY
  157. NPY_ARRAY_OUT_ARRAY
  158. NPY_ARRAY_INOUT_ARRAY
  159. NPY_ARRAY_IN_FARRAY
  160. NPY_ARRAY_OUT_FARRAY
  161. NPY_ARRAY_INOUT_FARRAY
  162. NPY_ARRAY_UPDATE_ALL
  163. cdef enum:
  164. NPY_MAXDIMS
  165. npy_intp NPY_MAX_ELSIZE
  166. ctypedef void (*PyArray_VectorUnaryFunc)(void *, void *, npy_intp, void *, void *)
  167. ctypedef struct PyArray_ArrayDescr:
  168. # shape is a tuple, but Cython doesn't support "tuple shape"
  169. # inside a non-PyObject declaration, so we have to declare it
  170. # as just a PyObject*.
  171. PyObject* shape
  172. ctypedef struct PyArray_Descr:
  173. pass
  174. ctypedef class numpy.dtype [object PyArray_Descr, check_size ignore]:
  175. # Use PyDataType_* macros when possible, however there are no macros
  176. # for accessing some of the fields, so some are defined.
  177. cdef PyTypeObject* typeobj
  178. cdef char kind
  179. cdef char type
  180. # Numpy sometimes mutates this without warning (e.g. it'll
  181. # sometimes change "|" to "<" in shared dtype objects on
  182. # little-endian machines). If this matters to you, use
  183. # PyArray_IsNativeByteOrder(dtype.byteorder) instead of
  184. # directly accessing this field.
  185. cdef char byteorder
  186. cdef char flags
  187. cdef int type_num
  188. cdef int itemsize "elsize"
  189. cdef int alignment
  190. cdef object fields
  191. cdef tuple names
  192. # Use PyDataType_HASSUBARRAY to test whether this field is
  193. # valid (the pointer can be NULL). Most users should access
  194. # this field via the inline helper method PyDataType_SHAPE.
  195. cdef PyArray_ArrayDescr* subarray
  196. ctypedef class numpy.flatiter [object PyArrayIterObject, check_size ignore]:
  197. # Use through macros
  198. pass
  199. ctypedef class numpy.broadcast [object PyArrayMultiIterObject, check_size ignore]:
  200. # Use through macros
  201. pass
  202. ctypedef struct PyArrayObject:
  203. # For use in situations where ndarray can't replace PyArrayObject*,
  204. # like PyArrayObject**.
  205. pass
  206. ctypedef class numpy.ndarray [object PyArrayObject, check_size ignore]:
  207. cdef __cythonbufferdefaults__ = {"mode": "strided"}
  208. # NOTE: no field declarations since direct access is deprecated since NumPy 1.7
  209. # Instead, we use properties that map to the corresponding C-API functions.
  210. @property
  211. cdef inline PyObject* base(self) nogil:
  212. """Returns a borrowed reference to the object owning the data/memory.
  213. """
  214. return PyArray_BASE(self)
  215. @property
  216. cdef inline dtype descr(self):
  217. """Returns an owned reference to the dtype of the array.
  218. """
  219. return <dtype>PyArray_DESCR(self)
  220. @property
  221. cdef inline int ndim(self) nogil:
  222. """Returns the number of dimensions in the array.
  223. """
  224. return PyArray_NDIM(self)
  225. @property
  226. cdef inline npy_intp *shape(self) nogil:
  227. """Returns a pointer to the dimensions/shape of the array.
  228. The number of elements matches the number of dimensions of the array (ndim).
  229. Can return NULL for 0-dimensional arrays.
  230. """
  231. return PyArray_DIMS(self)
  232. @property
  233. cdef inline npy_intp *strides(self) nogil:
  234. """Returns a pointer to the strides of the array.
  235. The number of elements matches the number of dimensions of the array (ndim).
  236. """
  237. return PyArray_STRIDES(self)
  238. @property
  239. cdef inline npy_intp size(self) nogil:
  240. """Returns the total size (in number of elements) of the array.
  241. """
  242. return PyArray_SIZE(self)
  243. @property
  244. cdef inline char* data(self) nogil:
  245. """The pointer to the data buffer as a char*.
  246. This is provided for legacy reasons to avoid direct struct field access.
  247. For new code that needs this access, you probably want to cast the result
  248. of `PyArray_DATA()` instead, which returns a 'void*'.
  249. """
  250. return PyArray_BYTES(self)
  251. ctypedef unsigned char npy_bool
  252. ctypedef signed char npy_byte
  253. ctypedef signed short npy_short
  254. ctypedef signed int npy_int
  255. ctypedef signed long npy_long
  256. ctypedef signed long long npy_longlong
  257. ctypedef unsigned char npy_ubyte
  258. ctypedef unsigned short npy_ushort
  259. ctypedef unsigned int npy_uint
  260. ctypedef unsigned long npy_ulong
  261. ctypedef unsigned long long npy_ulonglong
  262. ctypedef float npy_float
  263. ctypedef double npy_double
  264. ctypedef long double npy_longdouble
  265. ctypedef signed char npy_int8
  266. ctypedef signed short npy_int16
  267. ctypedef signed int npy_int32
  268. ctypedef signed long long npy_int64
  269. ctypedef signed long long npy_int96
  270. ctypedef signed long long npy_int128
  271. ctypedef unsigned char npy_uint8
  272. ctypedef unsigned short npy_uint16
  273. ctypedef unsigned int npy_uint32
  274. ctypedef unsigned long long npy_uint64
  275. ctypedef unsigned long long npy_uint96
  276. ctypedef unsigned long long npy_uint128
  277. ctypedef float npy_float32
  278. ctypedef double npy_float64
  279. ctypedef long double npy_float80
  280. ctypedef long double npy_float96
  281. ctypedef long double npy_float128
  282. ctypedef struct npy_cfloat:
  283. double real
  284. double imag
  285. ctypedef struct npy_cdouble:
  286. double real
  287. double imag
  288. ctypedef struct npy_clongdouble:
  289. long double real
  290. long double imag
  291. ctypedef struct npy_complex64:
  292. float real
  293. float imag
  294. ctypedef struct npy_complex128:
  295. double real
  296. double imag
  297. ctypedef struct npy_complex160:
  298. long double real
  299. long double imag
  300. ctypedef struct npy_complex192:
  301. long double real
  302. long double imag
  303. ctypedef struct npy_complex256:
  304. long double real
  305. long double imag
  306. ctypedef struct PyArray_Dims:
  307. npy_intp *ptr
  308. int len
  309. int _import_array() except -1
  310. # A second definition so _import_array isn't marked as used when we use it here.
  311. # Do not use - subject to change any time.
  312. int __pyx_import_array "_import_array"() except -1
  313. #
  314. # Macros from ndarrayobject.h
  315. #
  316. bint PyArray_CHKFLAGS(ndarray m, int flags) nogil
  317. bint PyArray_IS_C_CONTIGUOUS(ndarray arr) nogil
  318. bint PyArray_IS_F_CONTIGUOUS(ndarray arr) nogil
  319. bint PyArray_ISCONTIGUOUS(ndarray m) nogil
  320. bint PyArray_ISWRITEABLE(ndarray m) nogil
  321. bint PyArray_ISALIGNED(ndarray m) nogil
  322. int PyArray_NDIM(ndarray) nogil
  323. bint PyArray_ISONESEGMENT(ndarray) nogil
  324. bint PyArray_ISFORTRAN(ndarray) nogil
  325. int PyArray_FORTRANIF(ndarray) nogil
  326. void* PyArray_DATA(ndarray) nogil
  327. char* PyArray_BYTES(ndarray) nogil
  328. npy_intp* PyArray_DIMS(ndarray) nogil
  329. npy_intp* PyArray_STRIDES(ndarray) nogil
  330. npy_intp PyArray_DIM(ndarray, size_t) nogil
  331. npy_intp PyArray_STRIDE(ndarray, size_t) nogil
  332. PyObject *PyArray_BASE(ndarray) nogil # returns borrowed reference!
  333. PyArray_Descr *PyArray_DESCR(ndarray) nogil # returns borrowed reference to dtype!
  334. PyArray_Descr *PyArray_DTYPE(ndarray) nogil # returns borrowed reference to dtype! NP 1.7+ alias for descr.
  335. int PyArray_FLAGS(ndarray) nogil
  336. void PyArray_CLEARFLAGS(ndarray, int flags) nogil # Added in NumPy 1.7
  337. void PyArray_ENABLEFLAGS(ndarray, int flags) nogil # Added in NumPy 1.7
  338. npy_intp PyArray_ITEMSIZE(ndarray) nogil
  339. int PyArray_TYPE(ndarray arr) nogil
  340. object PyArray_GETITEM(ndarray arr, void *itemptr)
  341. int PyArray_SETITEM(ndarray arr, void *itemptr, object obj)
  342. bint PyTypeNum_ISBOOL(int) nogil
  343. bint PyTypeNum_ISUNSIGNED(int) nogil
  344. bint PyTypeNum_ISSIGNED(int) nogil
  345. bint PyTypeNum_ISINTEGER(int) nogil
  346. bint PyTypeNum_ISFLOAT(int) nogil
  347. bint PyTypeNum_ISNUMBER(int) nogil
  348. bint PyTypeNum_ISSTRING(int) nogil
  349. bint PyTypeNum_ISCOMPLEX(int) nogil
  350. bint PyTypeNum_ISPYTHON(int) nogil
  351. bint PyTypeNum_ISFLEXIBLE(int) nogil
  352. bint PyTypeNum_ISUSERDEF(int) nogil
  353. bint PyTypeNum_ISEXTENDED(int) nogil
  354. bint PyTypeNum_ISOBJECT(int) 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_ISPYTHON(dtype) nogil
  364. bint PyDataType_ISFLEXIBLE(dtype) nogil
  365. bint PyDataType_ISUSERDEF(dtype) nogil
  366. bint PyDataType_ISEXTENDED(dtype) nogil
  367. bint PyDataType_ISOBJECT(dtype) nogil
  368. bint PyDataType_HASFIELDS(dtype) nogil
  369. bint PyDataType_HASSUBARRAY(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_ISPYTHON(ndarray) nogil
  379. bint PyArray_ISFLEXIBLE(ndarray) nogil
  380. bint PyArray_ISUSERDEF(ndarray) nogil
  381. bint PyArray_ISEXTENDED(ndarray) nogil
  382. bint PyArray_ISOBJECT(ndarray) nogil
  383. bint PyArray_HASFIELDS(ndarray) nogil
  384. bint PyArray_ISVARIABLE(ndarray) nogil
  385. bint PyArray_SAFEALIGNEDCOPY(ndarray) nogil
  386. bint PyArray_ISNBO(char) nogil # works on ndarray.byteorder
  387. bint PyArray_IsNativeByteOrder(char) nogil # works on ndarray.byteorder
  388. bint PyArray_ISNOTSWAPPED(ndarray) nogil
  389. bint PyArray_ISBYTESWAPPED(ndarray) nogil
  390. bint PyArray_FLAGSWAP(ndarray, int) nogil
  391. bint PyArray_ISCARRAY(ndarray) nogil
  392. bint PyArray_ISCARRAY_RO(ndarray) nogil
  393. bint PyArray_ISFARRAY(ndarray) nogil
  394. bint PyArray_ISFARRAY_RO(ndarray) nogil
  395. bint PyArray_ISBEHAVED(ndarray) nogil
  396. bint PyArray_ISBEHAVED_RO(ndarray) nogil
  397. bint PyDataType_ISNOTSWAPPED(dtype) nogil
  398. bint PyDataType_ISBYTESWAPPED(dtype) nogil
  399. bint PyArray_DescrCheck(object)
  400. bint PyArray_Check(object)
  401. bint PyArray_CheckExact(object)
  402. # Cannot be supported due to out arg:
  403. # bint PyArray_HasArrayInterfaceType(object, dtype, object, object&)
  404. # bint PyArray_HasArrayInterface(op, out)
  405. bint PyArray_IsZeroDim(object)
  406. # Cannot be supported due to ## ## in macro:
  407. # bint PyArray_IsScalar(object, verbatim work)
  408. bint PyArray_CheckScalar(object)
  409. bint PyArray_IsPythonNumber(object)
  410. bint PyArray_IsPythonScalar(object)
  411. bint PyArray_IsAnyScalar(object)
  412. bint PyArray_CheckAnyScalar(object)
  413. ndarray PyArray_GETCONTIGUOUS(ndarray)
  414. bint PyArray_SAMESHAPE(ndarray, ndarray) nogil
  415. npy_intp PyArray_SIZE(ndarray) nogil
  416. npy_intp PyArray_NBYTES(ndarray) nogil
  417. object PyArray_FROM_O(object)
  418. object PyArray_FROM_OF(object m, int flags)
  419. object PyArray_FROM_OT(object m, int type)
  420. object PyArray_FROM_OTF(object m, int type, int flags)
  421. object PyArray_FROMANY(object m, int type, int min, int max, int flags)
  422. object PyArray_ZEROS(int nd, npy_intp* dims, int type, int fortran)
  423. object PyArray_EMPTY(int nd, npy_intp* dims, int type, int fortran)
  424. void PyArray_FILLWBYTE(object, int val)
  425. npy_intp PyArray_REFCOUNT(object)
  426. object PyArray_ContiguousFromAny(op, int, int min_depth, int max_depth)
  427. unsigned char PyArray_EquivArrTypes(ndarray a1, ndarray a2)
  428. bint PyArray_EquivByteorders(int b1, int b2) nogil
  429. object PyArray_SimpleNew(int nd, npy_intp* dims, int typenum)
  430. object PyArray_SimpleNewFromData(int nd, npy_intp* dims, int typenum, void* data)
  431. #object PyArray_SimpleNewFromDescr(int nd, npy_intp* dims, dtype descr)
  432. object PyArray_ToScalar(void* data, ndarray arr)
  433. void* PyArray_GETPTR1(ndarray m, npy_intp i) nogil
  434. void* PyArray_GETPTR2(ndarray m, npy_intp i, npy_intp j) nogil
  435. void* PyArray_GETPTR3(ndarray m, npy_intp i, npy_intp j, npy_intp k) nogil
  436. void* PyArray_GETPTR4(ndarray m, npy_intp i, npy_intp j, npy_intp k, npy_intp l) nogil
  437. void PyArray_XDECREF_ERR(ndarray)
  438. # Cannot be supported due to out arg
  439. # void PyArray_DESCR_REPLACE(descr)
  440. object PyArray_Copy(ndarray)
  441. object PyArray_FromObject(object op, int type, int min_depth, int max_depth)
  442. object PyArray_ContiguousFromObject(object op, int type, int min_depth, int max_depth)
  443. object PyArray_CopyFromObject(object op, int type, int min_depth, int max_depth)
  444. object PyArray_Cast(ndarray mp, int type_num)
  445. object PyArray_Take(ndarray ap, object items, int axis)
  446. object PyArray_Put(ndarray ap, object items, object values)
  447. void PyArray_ITER_RESET(flatiter it) nogil
  448. void PyArray_ITER_NEXT(flatiter it) nogil
  449. void PyArray_ITER_GOTO(flatiter it, npy_intp* destination) nogil
  450. void PyArray_ITER_GOTO1D(flatiter it, npy_intp ind) nogil
  451. void* PyArray_ITER_DATA(flatiter it) nogil
  452. bint PyArray_ITER_NOTDONE(flatiter it) nogil
  453. void PyArray_MultiIter_RESET(broadcast multi) nogil
  454. void PyArray_MultiIter_NEXT(broadcast multi) nogil
  455. void PyArray_MultiIter_GOTO(broadcast multi, npy_intp dest) nogil
  456. void PyArray_MultiIter_GOTO1D(broadcast multi, npy_intp ind) nogil
  457. void* PyArray_MultiIter_DATA(broadcast multi, npy_intp i) nogil
  458. void PyArray_MultiIter_NEXTi(broadcast multi, npy_intp i) nogil
  459. bint PyArray_MultiIter_NOTDONE(broadcast multi) nogil
  460. # Functions from __multiarray_api.h
  461. # Functions taking dtype and returning object/ndarray are disabled
  462. # for now as they steal dtype references. I'm conservative and disable
  463. # more than is probably needed until it can be checked further.
  464. int PyArray_SetNumericOps (object)
  465. object PyArray_GetNumericOps ()
  466. int PyArray_INCREF (ndarray)
  467. int PyArray_XDECREF (ndarray)
  468. void PyArray_SetStringFunction (object, int)
  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_CastTo (ndarray, ndarray)
  475. int PyArray_CastAnyTo (ndarray, ndarray)
  476. int PyArray_CanCastSafely (int, int)
  477. npy_bool PyArray_CanCastTo (dtype, dtype)
  478. int PyArray_ObjectType (object, int)
  479. dtype PyArray_DescrFromObject (object, dtype)
  480. #ndarray* PyArray_ConvertToCommonType (object, int *)
  481. dtype PyArray_DescrFromScalar (object)
  482. dtype PyArray_DescrFromTypeObject (object)
  483. npy_intp PyArray_Size (object)
  484. #object PyArray_Scalar (void *, dtype, object)
  485. #object PyArray_FromScalar (object, dtype)
  486. void PyArray_ScalarAsCtype (object, void *)
  487. #int PyArray_CastScalarToCtype (object, void *, dtype)
  488. #int PyArray_CastScalarDirect (object, dtype, void *, int)
  489. object PyArray_ScalarFromObject (object)
  490. #PyArray_VectorUnaryFunc * PyArray_GetCastFunc (dtype, int)
  491. object PyArray_FromDims (int, int *, int)
  492. #object PyArray_FromDimsAndDataAndDescr (int, int *, dtype, char *)
  493. #object PyArray_FromAny (object, dtype, int, int, int, object)
  494. object PyArray_EnsureArray (object)
  495. object PyArray_EnsureAnyArray (object)
  496. #object PyArray_FromFile (stdio.FILE *, dtype, npy_intp, char *)
  497. #object PyArray_FromString (char *, npy_intp, dtype, npy_intp, char *)
  498. #object PyArray_FromBuffer (object, dtype, npy_intp, npy_intp)
  499. #object PyArray_FromIter (object, dtype, npy_intp)
  500. object PyArray_Return (ndarray)
  501. #object PyArray_GetField (ndarray, dtype, int)
  502. #int PyArray_SetField (ndarray, dtype, int, object)
  503. object PyArray_Byteswap (ndarray, npy_bool)
  504. object PyArray_Resize (ndarray, PyArray_Dims *, int, NPY_ORDER)
  505. int PyArray_MoveInto (ndarray, ndarray)
  506. int PyArray_CopyInto (ndarray, ndarray)
  507. int PyArray_CopyAnyInto (ndarray, ndarray)
  508. int PyArray_CopyObject (ndarray, object)
  509. object PyArray_NewCopy (ndarray, NPY_ORDER)
  510. object PyArray_ToList (ndarray)
  511. object PyArray_ToString (ndarray, NPY_ORDER)
  512. int PyArray_ToFile (ndarray, stdio.FILE *, char *, char *)
  513. int PyArray_Dump (object, object, int)
  514. object PyArray_Dumps (object, int)
  515. int PyArray_ValidType (int)
  516. void PyArray_UpdateFlags (ndarray, int)
  517. object PyArray_New (type, int, npy_intp *, int, npy_intp *, void *, int, int, object)
  518. #object PyArray_NewFromDescr (type, dtype, int, npy_intp *, npy_intp *, void *, int, object)
  519. #dtype PyArray_DescrNew (dtype)
  520. dtype PyArray_DescrNewFromType (int)
  521. double PyArray_GetPriority (object, double)
  522. object PyArray_IterNew (object)
  523. object PyArray_MultiIterNew (int, ...)
  524. int PyArray_PyIntAsInt (object)
  525. npy_intp PyArray_PyIntAsIntp (object)
  526. int PyArray_Broadcast (broadcast)
  527. void PyArray_FillObjectArray (ndarray, object)
  528. int PyArray_FillWithScalar (ndarray, object)
  529. npy_bool PyArray_CheckStrides (int, int, npy_intp, npy_intp, npy_intp *, npy_intp *)
  530. dtype PyArray_DescrNewByteorder (dtype, char)
  531. object PyArray_IterAllButAxis (object, int *)
  532. #object PyArray_CheckFromAny (object, dtype, int, int, int, object)
  533. #object PyArray_FromArray (ndarray, dtype, int)
  534. object PyArray_FromInterface (object)
  535. object PyArray_FromStructInterface (object)
  536. #object PyArray_FromArrayAttr (object, dtype, object)
  537. #NPY_SCALARKIND PyArray_ScalarKind (int, ndarray*)
  538. int PyArray_CanCoerceScalar (int, int, NPY_SCALARKIND)
  539. object PyArray_NewFlagsObject (object)
  540. npy_bool PyArray_CanCastScalar (type, type)
  541. #int PyArray_CompareUCS4 (npy_ucs4 *, npy_ucs4 *, register size_t)
  542. int PyArray_RemoveSmallest (broadcast)
  543. int PyArray_ElementStrides (object)
  544. void PyArray_Item_INCREF (char *, dtype)
  545. void PyArray_Item_XDECREF (char *, dtype)
  546. object PyArray_FieldNames (object)
  547. object PyArray_Transpose (ndarray, PyArray_Dims *)
  548. object PyArray_TakeFrom (ndarray, object, int, ndarray, NPY_CLIPMODE)
  549. object PyArray_PutTo (ndarray, object, object, NPY_CLIPMODE)
  550. object PyArray_PutMask (ndarray, object, object)
  551. object PyArray_Repeat (ndarray, object, int)
  552. object PyArray_Choose (ndarray, object, ndarray, NPY_CLIPMODE)
  553. int PyArray_Sort (ndarray, int, NPY_SORTKIND)
  554. object PyArray_ArgSort (ndarray, int, NPY_SORTKIND)
  555. object PyArray_SearchSorted (ndarray, object, NPY_SEARCHSIDE, PyObject *)
  556. object PyArray_ArgMax (ndarray, int, ndarray)
  557. object PyArray_ArgMin (ndarray, int, ndarray)
  558. object PyArray_Reshape (ndarray, object)
  559. object PyArray_Newshape (ndarray, PyArray_Dims *, NPY_ORDER)
  560. object PyArray_Squeeze (ndarray)
  561. #object PyArray_View (ndarray, dtype, type)
  562. object PyArray_SwapAxes (ndarray, int, int)
  563. object PyArray_Max (ndarray, int, ndarray)
  564. object PyArray_Min (ndarray, int, ndarray)
  565. object PyArray_Ptp (ndarray, int, ndarray)
  566. object PyArray_Mean (ndarray, int, int, ndarray)
  567. object PyArray_Trace (ndarray, int, int, int, int, ndarray)
  568. object PyArray_Diagonal (ndarray, int, int, int)
  569. object PyArray_Clip (ndarray, object, object, ndarray)
  570. object PyArray_Conjugate (ndarray, ndarray)
  571. object PyArray_Nonzero (ndarray)
  572. object PyArray_Std (ndarray, int, int, ndarray, int)
  573. object PyArray_Sum (ndarray, int, int, ndarray)
  574. object PyArray_CumSum (ndarray, int, int, ndarray)
  575. object PyArray_Prod (ndarray, int, int, ndarray)
  576. object PyArray_CumProd (ndarray, int, int, ndarray)
  577. object PyArray_All (ndarray, int, ndarray)
  578. object PyArray_Any (ndarray, int, ndarray)
  579. object PyArray_Compress (ndarray, object, int, ndarray)
  580. object PyArray_Flatten (ndarray, NPY_ORDER)
  581. object PyArray_Ravel (ndarray, NPY_ORDER)
  582. npy_intp PyArray_MultiplyList (npy_intp *, int)
  583. int PyArray_MultiplyIntList (int *, int)
  584. void * PyArray_GetPtr (ndarray, npy_intp*)
  585. int PyArray_CompareLists (npy_intp *, npy_intp *, int)
  586. #int PyArray_AsCArray (object*, void *, npy_intp *, int, dtype)
  587. #int PyArray_As1D (object*, char **, int *, int)
  588. #int PyArray_As2D (object*, char ***, int *, int *, int)
  589. int PyArray_Free (object, void *)
  590. #int PyArray_Converter (object, object*)
  591. int PyArray_IntpFromSequence (object, npy_intp *, int)
  592. object PyArray_Concatenate (object, int)
  593. object PyArray_InnerProduct (object, object)
  594. object PyArray_MatrixProduct (object, object)
  595. object PyArray_CopyAndTranspose (object)
  596. object PyArray_Correlate (object, object, int)
  597. int PyArray_TypestrConvert (int, int)
  598. #int PyArray_DescrConverter (object, dtype*)
  599. #int PyArray_DescrConverter2 (object, dtype*)
  600. int PyArray_IntpConverter (object, PyArray_Dims *)
  601. #int PyArray_BufferConverter (object, chunk)
  602. int PyArray_AxisConverter (object, int *)
  603. int PyArray_BoolConverter (object, npy_bool *)
  604. int PyArray_ByteorderConverter (object, char *)
  605. int PyArray_OrderConverter (object, NPY_ORDER *)
  606. unsigned char PyArray_EquivTypes (dtype, dtype)
  607. #object PyArray_Zeros (int, npy_intp *, dtype, int)
  608. #object PyArray_Empty (int, npy_intp *, dtype, int)
  609. object PyArray_Where (object, object, object)
  610. object PyArray_Arange (double, double, double, int)
  611. #object PyArray_ArangeObj (object, object, object, dtype)
  612. int PyArray_SortkindConverter (object, NPY_SORTKIND *)
  613. object PyArray_LexSort (object, int)
  614. object PyArray_Round (ndarray, int, ndarray)
  615. unsigned char PyArray_EquivTypenums (int, int)
  616. int PyArray_RegisterDataType (dtype)
  617. int PyArray_RegisterCastFunc (dtype, int, PyArray_VectorUnaryFunc *)
  618. int PyArray_RegisterCanCast (dtype, int, NPY_SCALARKIND)
  619. #void PyArray_InitArrFuncs (PyArray_ArrFuncs *)
  620. object PyArray_IntTupleFromIntp (int, npy_intp *)
  621. int PyArray_TypeNumFromName (char *)
  622. int PyArray_ClipmodeConverter (object, NPY_CLIPMODE *)
  623. #int PyArray_OutputConverter (object, ndarray*)
  624. object PyArray_BroadcastToShape (object, npy_intp *, int)
  625. void _PyArray_SigintHandler (int)
  626. void* _PyArray_GetSigintBuf ()
  627. #int PyArray_DescrAlignConverter (object, dtype*)
  628. #int PyArray_DescrAlignConverter2 (object, dtype*)
  629. int PyArray_SearchsideConverter (object, void *)
  630. object PyArray_CheckAxis (ndarray, int *, int)
  631. npy_intp PyArray_OverflowMultiplyList (npy_intp *, int)
  632. int PyArray_CompareString (char *, char *, size_t)
  633. int PyArray_SetBaseObject(ndarray, base) # NOTE: steals a reference to base! Use "set_array_base()" instead.
  634. # Typedefs that matches the runtime dtype objects in
  635. # the numpy module.
  636. # The ones that are commented out needs an IFDEF function
  637. # in Cython to enable them only on the right systems.
  638. ctypedef npy_int8 int8_t
  639. ctypedef npy_int16 int16_t
  640. ctypedef npy_int32 int32_t
  641. ctypedef npy_int64 int64_t
  642. #ctypedef npy_int96 int96_t
  643. #ctypedef npy_int128 int128_t
  644. ctypedef npy_uint8 uint8_t
  645. ctypedef npy_uint16 uint16_t
  646. ctypedef npy_uint32 uint32_t
  647. ctypedef npy_uint64 uint64_t
  648. #ctypedef npy_uint96 uint96_t
  649. #ctypedef npy_uint128 uint128_t
  650. ctypedef npy_float32 float32_t
  651. ctypedef npy_float64 float64_t
  652. #ctypedef npy_float80 float80_t
  653. #ctypedef npy_float128 float128_t
  654. ctypedef float complex complex64_t
  655. ctypedef double complex complex128_t
  656. # The int types are mapped a bit surprising --
  657. # numpy.int corresponds to 'l' and numpy.long to 'q'
  658. ctypedef npy_long int_t
  659. ctypedef npy_longlong long_t
  660. ctypedef npy_longlong longlong_t
  661. ctypedef npy_ulong uint_t
  662. ctypedef npy_ulonglong ulong_t
  663. ctypedef npy_ulonglong ulonglong_t
  664. ctypedef npy_intp intp_t
  665. ctypedef npy_uintp uintp_t
  666. ctypedef npy_double float_t
  667. ctypedef npy_double double_t
  668. ctypedef npy_longdouble longdouble_t
  669. ctypedef npy_cfloat cfloat_t
  670. ctypedef npy_cdouble cdouble_t
  671. ctypedef npy_clongdouble clongdouble_t
  672. ctypedef npy_cdouble complex_t
  673. cdef inline object PyArray_MultiIterNew1(a):
  674. return PyArray_MultiIterNew(1, <void*>a)
  675. cdef inline object PyArray_MultiIterNew2(a, b):
  676. return PyArray_MultiIterNew(2, <void*>a, <void*>b)
  677. cdef inline object PyArray_MultiIterNew3(a, b, c):
  678. return PyArray_MultiIterNew(3, <void*>a, <void*>b, <void*> c)
  679. cdef inline object PyArray_MultiIterNew4(a, b, c, d):
  680. return PyArray_MultiIterNew(4, <void*>a, <void*>b, <void*>c, <void*> d)
  681. cdef inline object PyArray_MultiIterNew5(a, b, c, d, e):
  682. return PyArray_MultiIterNew(5, <void*>a, <void*>b, <void*>c, <void*> d, <void*> e)
  683. cdef inline tuple PyDataType_SHAPE(dtype d):
  684. if PyDataType_HASSUBARRAY(d):
  685. return <tuple>d.subarray.shape
  686. else:
  687. return ()
  688. cdef extern from "numpy/ndarrayobject.h":
  689. PyTypeObject PyTimedeltaArrType_Type
  690. PyTypeObject PyDatetimeArrType_Type
  691. ctypedef int64_t npy_timedelta
  692. ctypedef int64_t npy_datetime
  693. cdef extern from "numpy/ndarraytypes.h":
  694. ctypedef struct PyArray_DatetimeMetaData:
  695. NPY_DATETIMEUNIT base
  696. int64_t num
  697. cdef extern from "numpy/arrayscalars.h":
  698. ctypedef struct PyDatetimeScalarObject:
  699. # PyObject_HEAD
  700. npy_datetime obval
  701. PyArray_DatetimeMetaData obmeta
  702. ctypedef struct PyTimedeltaScalarObject:
  703. # PyObject_HEAD
  704. npy_timedelta obval
  705. PyArray_DatetimeMetaData obmeta
  706. ctypedef enum NPY_DATETIMEUNIT:
  707. NPY_FR_Y
  708. NPY_FR_M
  709. NPY_FR_W
  710. NPY_FR_D
  711. NPY_FR_B
  712. NPY_FR_h
  713. NPY_FR_m
  714. NPY_FR_s
  715. NPY_FR_ms
  716. NPY_FR_us
  717. NPY_FR_ns
  718. NPY_FR_ps
  719. NPY_FR_fs
  720. NPY_FR_as
  721. #
  722. # ufunc API
  723. #
  724. cdef extern from "numpy/ufuncobject.h":
  725. ctypedef void (*PyUFuncGenericFunction) (char **, npy_intp *, npy_intp *, void *)
  726. ctypedef class numpy.ufunc [object PyUFuncObject, check_size ignore]:
  727. cdef:
  728. int nin, nout, nargs
  729. int identity
  730. PyUFuncGenericFunction *functions
  731. void **data
  732. int ntypes
  733. int check_return
  734. char *name
  735. char *types
  736. char *doc
  737. void *ptr
  738. PyObject *obj
  739. PyObject *userloops
  740. cdef enum:
  741. PyUFunc_Zero
  742. PyUFunc_One
  743. PyUFunc_None
  744. UFUNC_ERR_IGNORE
  745. UFUNC_ERR_WARN
  746. UFUNC_ERR_RAISE
  747. UFUNC_ERR_CALL
  748. UFUNC_ERR_PRINT
  749. UFUNC_ERR_LOG
  750. UFUNC_MASK_DIVIDEBYZERO
  751. UFUNC_MASK_OVERFLOW
  752. UFUNC_MASK_UNDERFLOW
  753. UFUNC_MASK_INVALID
  754. UFUNC_SHIFT_DIVIDEBYZERO
  755. UFUNC_SHIFT_OVERFLOW
  756. UFUNC_SHIFT_UNDERFLOW
  757. UFUNC_SHIFT_INVALID
  758. UFUNC_FPE_DIVIDEBYZERO
  759. UFUNC_FPE_OVERFLOW
  760. UFUNC_FPE_UNDERFLOW
  761. UFUNC_FPE_INVALID
  762. UFUNC_ERR_DEFAULT
  763. UFUNC_ERR_DEFAULT2
  764. object PyUFunc_FromFuncAndData(PyUFuncGenericFunction *,
  765. void **, char *, int, int, int, int, char *, char *, int)
  766. int PyUFunc_RegisterLoopForType(ufunc, int,
  767. PyUFuncGenericFunction, int *, void *)
  768. int PyUFunc_GenericFunction \
  769. (ufunc, PyObject *, PyObject *, PyArrayObject **)
  770. void PyUFunc_f_f_As_d_d \
  771. (char **, npy_intp *, npy_intp *, void *)
  772. void PyUFunc_d_d \
  773. (char **, npy_intp *, npy_intp *, void *)
  774. void PyUFunc_f_f \
  775. (char **, npy_intp *, npy_intp *, void *)
  776. void PyUFunc_g_g \
  777. (char **, npy_intp *, npy_intp *, void *)
  778. void PyUFunc_F_F_As_D_D \
  779. (char **, npy_intp *, npy_intp *, void *)
  780. void PyUFunc_F_F \
  781. (char **, npy_intp *, npy_intp *, void *)
  782. void PyUFunc_D_D \
  783. (char **, npy_intp *, npy_intp *, void *)
  784. void PyUFunc_G_G \
  785. (char **, npy_intp *, npy_intp *, void *)
  786. void PyUFunc_O_O \
  787. (char **, npy_intp *, npy_intp *, void *)
  788. void PyUFunc_ff_f_As_dd_d \
  789. (char **, npy_intp *, npy_intp *, void *)
  790. void PyUFunc_ff_f \
  791. (char **, npy_intp *, npy_intp *, void *)
  792. void PyUFunc_dd_d \
  793. (char **, npy_intp *, npy_intp *, void *)
  794. void PyUFunc_gg_g \
  795. (char **, npy_intp *, npy_intp *, void *)
  796. void PyUFunc_FF_F_As_DD_D \
  797. (char **, npy_intp *, npy_intp *, void *)
  798. void PyUFunc_DD_D \
  799. (char **, npy_intp *, npy_intp *, void *)
  800. void PyUFunc_FF_F \
  801. (char **, npy_intp *, npy_intp *, void *)
  802. void PyUFunc_GG_G \
  803. (char **, npy_intp *, npy_intp *, void *)
  804. void PyUFunc_OO_O \
  805. (char **, npy_intp *, npy_intp *, void *)
  806. void PyUFunc_O_O_method \
  807. (char **, npy_intp *, npy_intp *, void *)
  808. void PyUFunc_OO_O_method \
  809. (char **, npy_intp *, npy_intp *, void *)
  810. void PyUFunc_On_Om \
  811. (char **, npy_intp *, npy_intp *, void *)
  812. int PyUFunc_GetPyValues \
  813. (char *, int *, int *, PyObject **)
  814. int PyUFunc_checkfperr \
  815. (int, PyObject *, int *)
  816. void PyUFunc_clearfperr()
  817. int PyUFunc_getfperr()
  818. int PyUFunc_handlefperr \
  819. (int, PyObject *, int, int *)
  820. int PyUFunc_ReplaceLoopBySignature \
  821. (ufunc, PyUFuncGenericFunction, int *, PyUFuncGenericFunction *)
  822. object PyUFunc_FromFuncAndDataAndSignature \
  823. (PyUFuncGenericFunction *, void **, char *, int, int, int,
  824. int, char *, char *, int, char *)
  825. int _import_umath() except -1
  826. cdef inline void set_array_base(ndarray arr, object base):
  827. Py_INCREF(base) # important to do this before stealing the reference below!
  828. PyArray_SetBaseObject(arr, base)
  829. cdef inline object get_array_base(ndarray arr):
  830. base = PyArray_BASE(arr)
  831. if base is NULL:
  832. return None
  833. return <object>base
  834. # Versions of the import_* functions which are more suitable for
  835. # Cython code.
  836. cdef inline int import_array() except -1:
  837. try:
  838. __pyx_import_array()
  839. except Exception:
  840. raise ImportError("numpy.core.multiarray failed to import")
  841. cdef inline int import_umath() except -1:
  842. try:
  843. _import_umath()
  844. except Exception:
  845. raise ImportError("numpy.core.umath failed to import")
  846. cdef inline int import_ufunc() except -1:
  847. try:
  848. _import_umath()
  849. except Exception:
  850. raise ImportError("numpy.core.umath failed to import")
  851. cdef inline bint is_timedelta64_object(object obj):
  852. """
  853. Cython equivalent of `isinstance(obj, np.timedelta64)`
  854. Parameters
  855. ----------
  856. obj : object
  857. Returns
  858. -------
  859. bool
  860. """
  861. return PyObject_TypeCheck(obj, &PyTimedeltaArrType_Type)
  862. cdef inline bint is_datetime64_object(object obj):
  863. """
  864. Cython equivalent of `isinstance(obj, np.datetime64)`
  865. Parameters
  866. ----------
  867. obj : object
  868. Returns
  869. -------
  870. bool
  871. """
  872. return PyObject_TypeCheck(obj, &PyDatetimeArrType_Type)
  873. cdef inline npy_datetime get_datetime64_value(object obj) nogil:
  874. """
  875. returns the int64 value underlying scalar numpy datetime64 object
  876. Note that to interpret this as a datetime, the corresponding unit is
  877. also needed. That can be found using `get_datetime64_unit`.
  878. """
  879. return (<PyDatetimeScalarObject*>obj).obval
  880. cdef inline npy_timedelta get_timedelta64_value(object obj) nogil:
  881. """
  882. returns the int64 value underlying scalar numpy timedelta64 object
  883. """
  884. return (<PyTimedeltaScalarObject*>obj).obval
  885. cdef inline NPY_DATETIMEUNIT get_datetime64_unit(object obj) nogil:
  886. """
  887. returns the unit part of the dtype for a numpy datetime64 object.
  888. """
  889. return <NPY_DATETIMEUNIT>(<PyDatetimeScalarObject*>obj).obmeta.base