__init__.pxd 34 KB

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