__init__.pxd 36 KB

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