__init__.pxd 34 KB

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