1 # NumPy static imports for Cython
3 # If any of the PyArray_* functions are called, import_array must be
6 # This also defines backwards-compatability buffer acquisition
7 # code for use in Python 2.x (or Python <= 2.5 when NumPy starts
8 # implementing PEP-3118 directly).
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.
14 # Author: Dag Sverre Seljebotn
17 DEF _buffer_format_string_len = 255
19 cimport cpython.buffer as pybuf
20 from cpython.ref cimport Py_INCREF, Py_XDECREF
21 from cpython.object cimport PyObject
22 from cpython.type cimport type
23 cimport libc.stdlib as stdlib
24 cimport libc.stdio as stdio
26 cdef extern from "Python.h":
27 ctypedef int Py_intptr_t
29 cdef extern from "numpy/arrayobject.h":
30 ctypedef Py_intptr_t npy_intp
31 ctypedef size_t npy_uintp
87 ctypedef enum NPY_ORDER:
92 ctypedef enum NPY_CLIPMODE:
97 ctypedef enum NPY_SCALARKIND:
106 ctypedef enum NPY_SORTKIND:
111 ctypedef enum NPY_SEARCHSIDE:
151 npy_intp NPY_MAX_ELSIZE
153 ctypedef void (*PyArray_VectorUnaryFunc)(void *, void *, npy_intp, void *, void *)
155 ctypedef class numpy.dtype [object PyArray_Descr]:
156 # Use PyDataType_* macros when possible, however there are no macros
157 # for accessing some of the fields, so some are defined. Please
158 # ask on cython-dev if you need more.
160 cdef int itemsize "elsize"
165 ctypedef extern class numpy.flatiter [object PyArrayIterObject]:
169 ctypedef extern class numpy.broadcast [object PyArrayMultiIterObject]:
173 ctypedef struct PyArrayObject:
174 # For use in situations where ndarray can't replace PyArrayObject*,
175 # like PyArrayObject**.
178 ctypedef class numpy.ndarray [object PyArrayObject]:
179 cdef __cythonbufferdefaults__ = {"mode": "strided"}
182 # Only taking a few of the most commonly used and stable fields.
183 # One should use PyArray_* macros instead to access the C fields.
186 npy_intp *shape "dimensions"
191 # Note: This syntax (function definition in pxd files) is an
192 # experimental exception made for __getbuffer__ and __releasebuffer__
193 # -- the details of this may change.
194 def __getbuffer__(ndarray self, Py_buffer* info, int flags):
195 # This implementation of getbuffer is geared towards Cython
196 # requirements, and does not yet fullfill the PEP.
197 # In particular strided access is always provided regardless
200 if info == NULL: return
202 cdef int copy_shape, i, ndim
203 cdef int endian_detector = 1
204 cdef bint little_endian = ((<char*>&endian_detector)[0] != 0)
206 ndim = PyArray_NDIM(self)
208 if sizeof(npy_intp) != sizeof(Py_ssize_t):
213 if ((flags & pybuf.PyBUF_C_CONTIGUOUS == pybuf.PyBUF_C_CONTIGUOUS)
214 and not PyArray_CHKFLAGS(self, NPY_C_CONTIGUOUS)):
215 raise ValueError(u"ndarray is not C contiguous")
217 if ((flags & pybuf.PyBUF_F_CONTIGUOUS == pybuf.PyBUF_F_CONTIGUOUS)
218 and not PyArray_CHKFLAGS(self, NPY_F_CONTIGUOUS)):
219 raise ValueError(u"ndarray is not Fortran contiguous")
221 info.buf = PyArray_DATA(self)
224 # Allocate new buffer for strides and shape info.
225 # This is allocated as one block, strides first.
226 info.strides = <Py_ssize_t*>stdlib.malloc(sizeof(Py_ssize_t) * <size_t>ndim * 2)
227 info.shape = info.strides + ndim
228 for i in range(ndim):
229 info.strides[i] = PyArray_STRIDES(self)[i]
230 info.shape[i] = PyArray_DIMS(self)[i]
232 info.strides = <Py_ssize_t*>PyArray_STRIDES(self)
233 info.shape = <Py_ssize_t*>PyArray_DIMS(self)
234 info.suboffsets = NULL
235 info.itemsize = PyArray_ITEMSIZE(self)
236 info.readonly = not PyArray_ISWRITEABLE(self)
240 cdef dtype descr = self.descr
244 cdef bint hasfields = PyDataType_HASFIELDS(descr)
246 if not hasfields and not copy_shape:
247 # do not call releasebuffer
250 # need to call releasebuffer
255 if ((descr.byteorder == c'>' and little_endian) or
256 (descr.byteorder == c'<' and not little_endian)):
257 raise ValueError(u"Non-native byte order not supported")
258 if t == NPY_BYTE: f = "b"
259 elif t == NPY_UBYTE: f = "B"
260 elif t == NPY_SHORT: f = "h"
261 elif t == NPY_USHORT: f = "H"
262 elif t == NPY_INT: f = "i"
263 elif t == NPY_UINT: f = "I"
264 elif t == NPY_LONG: f = "l"
265 elif t == NPY_ULONG: f = "L"
266 elif t == NPY_LONGLONG: f = "q"
267 elif t == NPY_ULONGLONG: f = "Q"
268 elif t == NPY_FLOAT: f = "f"
269 elif t == NPY_DOUBLE: f = "d"
270 elif t == NPY_LONGDOUBLE: f = "g"
271 elif t == NPY_CFLOAT: f = "Zf"
272 elif t == NPY_CDOUBLE: f = "Zd"
273 elif t == NPY_CLONGDOUBLE: f = "Zg"
274 elif t == NPY_OBJECT: f = "O"
276 raise ValueError(u"unknown dtype code in numpy.pxd (%d)" % t)
280 info.format = <char*>stdlib.malloc(_buffer_format_string_len)
281 info.format[0] = c'^' # Native data types, manual alignment
283 f = _util_dtypestring(descr, info.format + 1,
284 info.format + _buffer_format_string_len,
286 f[0] = c'\0' # Terminate format string
288 def __releasebuffer__(ndarray self, Py_buffer* info):
289 if PyArray_HASFIELDS(self):
290 stdlib.free(info.format)
291 if sizeof(npy_intp) != sizeof(Py_ssize_t):
292 stdlib.free(info.strides)
293 # info.shape was stored after info.strides in the same block
296 ctypedef unsigned char npy_bool
298 ctypedef signed char npy_byte
299 ctypedef signed short npy_short
300 ctypedef signed int npy_int
301 ctypedef signed long npy_long
302 ctypedef signed long long npy_longlong
304 ctypedef unsigned char npy_ubyte
305 ctypedef unsigned short npy_ushort
306 ctypedef unsigned int npy_uint
307 ctypedef unsigned long npy_ulong
308 ctypedef unsigned long long npy_ulonglong
310 ctypedef float npy_float
311 ctypedef double npy_double
312 ctypedef long double npy_longdouble
314 ctypedef signed char npy_int8
315 ctypedef signed short npy_int16
316 ctypedef signed int npy_int32
317 ctypedef signed long long npy_int64
318 ctypedef signed long long npy_int96
319 ctypedef signed long long npy_int128
321 ctypedef unsigned char npy_uint8
322 ctypedef unsigned short npy_uint16
323 ctypedef unsigned int npy_uint32
324 ctypedef unsigned long long npy_uint64
325 ctypedef unsigned long long npy_uint96
326 ctypedef unsigned long long npy_uint128
328 ctypedef float npy_float32
329 ctypedef double npy_float64
330 ctypedef long double npy_float80
331 ctypedef long double npy_float96
332 ctypedef long double npy_float128
334 ctypedef struct npy_cfloat:
338 ctypedef struct npy_cdouble:
342 ctypedef struct npy_clongdouble:
346 ctypedef struct npy_complex64:
350 ctypedef struct npy_complex128:
354 ctypedef struct npy_complex160:
358 ctypedef struct npy_complex192:
362 ctypedef struct npy_complex256:
366 ctypedef struct PyArray_Dims:
373 # Macros from ndarrayobject.h
375 bint PyArray_CHKFLAGS(ndarray m, int flags)
376 bint PyArray_ISCONTIGUOUS(ndarray m)
377 bint PyArray_ISWRITEABLE(ndarray m)
378 bint PyArray_ISALIGNED(ndarray m)
380 int PyArray_NDIM(ndarray)
381 bint PyArray_ISONESEGMENT(ndarray)
382 bint PyArray_ISFORTRAN(ndarray)
383 int PyArray_FORTRANIF(ndarray)
385 void* PyArray_DATA(ndarray)
386 char* PyArray_BYTES(ndarray)
387 npy_intp* PyArray_DIMS(ndarray)
388 npy_intp* PyArray_STRIDES(ndarray)
389 npy_intp PyArray_DIM(ndarray, size_t)
390 npy_intp PyArray_STRIDE(ndarray, size_t)
392 # object PyArray_BASE(ndarray) wrong refcount semantics
393 # dtype PyArray_DESCR(ndarray) wrong refcount semantics
394 int PyArray_FLAGS(ndarray)
395 npy_intp PyArray_ITEMSIZE(ndarray)
396 int PyArray_TYPE(ndarray arr)
398 object PyArray_GETITEM(ndarray arr, void *itemptr)
399 int PyArray_SETITEM(ndarray arr, void *itemptr, object obj)
401 bint PyTypeNum_ISBOOL(int)
402 bint PyTypeNum_ISUNSIGNED(int)
403 bint PyTypeNum_ISSIGNED(int)
404 bint PyTypeNum_ISINTEGER(int)
405 bint PyTypeNum_ISFLOAT(int)
406 bint PyTypeNum_ISNUMBER(int)
407 bint PyTypeNum_ISSTRING(int)
408 bint PyTypeNum_ISCOMPLEX(int)
409 bint PyTypeNum_ISPYTHON(int)
410 bint PyTypeNum_ISFLEXIBLE(int)
411 bint PyTypeNum_ISUSERDEF(int)
412 bint PyTypeNum_ISEXTENDED(int)
413 bint PyTypeNum_ISOBJECT(int)
415 bint PyDataType_ISBOOL(dtype)
416 bint PyDataType_ISUNSIGNED(dtype)
417 bint PyDataType_ISSIGNED(dtype)
418 bint PyDataType_ISINTEGER(dtype)
419 bint PyDataType_ISFLOAT(dtype)
420 bint PyDataType_ISNUMBER(dtype)
421 bint PyDataType_ISSTRING(dtype)
422 bint PyDataType_ISCOMPLEX(dtype)
423 bint PyDataType_ISPYTHON(dtype)
424 bint PyDataType_ISFLEXIBLE(dtype)
425 bint PyDataType_ISUSERDEF(dtype)
426 bint PyDataType_ISEXTENDED(dtype)
427 bint PyDataType_ISOBJECT(dtype)
428 bint PyDataType_HASFIELDS(dtype)
430 bint PyArray_ISBOOL(ndarray)
431 bint PyArray_ISUNSIGNED(ndarray)
432 bint PyArray_ISSIGNED(ndarray)
433 bint PyArray_ISINTEGER(ndarray)
434 bint PyArray_ISFLOAT(ndarray)
435 bint PyArray_ISNUMBER(ndarray)
436 bint PyArray_ISSTRING(ndarray)
437 bint PyArray_ISCOMPLEX(ndarray)
438 bint PyArray_ISPYTHON(ndarray)
439 bint PyArray_ISFLEXIBLE(ndarray)
440 bint PyArray_ISUSERDEF(ndarray)
441 bint PyArray_ISEXTENDED(ndarray)
442 bint PyArray_ISOBJECT(ndarray)
443 bint PyArray_HASFIELDS(ndarray)
445 bint PyArray_ISVARIABLE(ndarray)
447 bint PyArray_SAFEALIGNEDCOPY(ndarray)
448 bint PyArray_ISNBO(char) # works on ndarray.byteorder
449 bint PyArray_IsNativeByteOrder(char) # works on ndarray.byteorder
450 bint PyArray_ISNOTSWAPPED(ndarray)
451 bint PyArray_ISBYTESWAPPED(ndarray)
453 bint PyArray_FLAGSWAP(ndarray, int)
455 bint PyArray_ISCARRAY(ndarray)
456 bint PyArray_ISCARRAY_RO(ndarray)
457 bint PyArray_ISFARRAY(ndarray)
458 bint PyArray_ISFARRAY_RO(ndarray)
459 bint PyArray_ISBEHAVED(ndarray)
460 bint PyArray_ISBEHAVED_RO(ndarray)
463 bint PyDataType_ISNOTSWAPPED(dtype)
464 bint PyDataType_ISBYTESWAPPED(dtype)
466 bint PyArray_DescrCheck(object)
468 bint PyArray_Check(object)
469 bint PyArray_CheckExact(object)
471 # Cannot be supported due to out arg:
472 # bint PyArray_HasArrayInterfaceType(object, dtype, object, object&)
473 # bint PyArray_HasArrayInterface(op, out)
476 bint PyArray_IsZeroDim(object)
477 # Cannot be supported due to ## ## in macro:
478 # bint PyArray_IsScalar(object, verbatim work)
479 bint PyArray_CheckScalar(object)
480 bint PyArray_IsPythonNumber(object)
481 bint PyArray_IsPythonScalar(object)
482 bint PyArray_IsAnyScalar(object)
483 bint PyArray_CheckAnyScalar(object)
484 ndarray PyArray_GETCONTIGUOUS(ndarray)
485 bint PyArray_SAMESHAPE(ndarray, ndarray)
486 npy_intp PyArray_SIZE(ndarray)
487 npy_intp PyArray_NBYTES(ndarray)
489 object PyArray_FROM_O(object)
490 object PyArray_FROM_OF(object m, int flags)
491 object PyArray_FROM_OT(object m, int type)
492 object PyArray_FROM_OTF(object m, int type, int flags)
493 object PyArray_FROMANY(object m, int type, int min, int max, int flags)
494 object PyArray_ZEROS(int nd, npy_intp* dims, int type, int fortran)
495 object PyArray_EMPTY(int nd, npy_intp* dims, int type, int fortran)
496 void PyArray_FILLWBYTE(object, int val)
497 npy_intp PyArray_REFCOUNT(object)
498 object PyArray_ContiguousFromAny(op, int, int min_depth, int max_depth)
499 unsigned char PyArray_EquivArrTypes(ndarray a1, ndarray a2)
500 bint PyArray_EquivByteorders(int b1, int b2)
501 object PyArray_SimpleNew(int nd, npy_intp* dims, int typenum)
502 object PyArray_SimpleNewFromData(int nd, npy_intp* dims, int typenum, void* data)
503 #object PyArray_SimpleNewFromDescr(int nd, npy_intp* dims, dtype descr)
504 object PyArray_ToScalar(void* data, ndarray arr)
506 void* PyArray_GETPTR1(ndarray m, npy_intp i)
507 void* PyArray_GETPTR2(ndarray m, npy_intp i, npy_intp j)
508 void* PyArray_GETPTR3(ndarray m, npy_intp i, npy_intp j, npy_intp k)
509 void* PyArray_GETPTR4(ndarray m, npy_intp i, npy_intp j, npy_intp k, npy_intp l)
511 void PyArray_XDECREF_ERR(ndarray)
512 # Cannot be supported due to out arg
513 # void PyArray_DESCR_REPLACE(descr)
516 object PyArray_Copy(ndarray)
517 object PyArray_FromObject(object op, int type, int min_depth, int max_depth)
518 object PyArray_ContiguousFromObject(object op, int type, int min_depth, int max_depth)
519 object PyArray_CopyFromObject(object op, int type, int min_depth, int max_depth)
521 object PyArray_Cast(ndarray mp, int type_num)
522 object PyArray_Take(ndarray ap, object items, int axis)
523 object PyArray_Put(ndarray ap, object items, object values)
525 void PyArray_ITER_RESET(flatiter it) nogil
526 void PyArray_ITER_NEXT(flatiter it) nogil
527 void PyArray_ITER_GOTO(flatiter it, npy_intp* destination) nogil
528 void PyArray_ITER_GOTO1D(flatiter it, npy_intp ind) nogil
529 void* PyArray_ITER_DATA(flatiter it) nogil
530 bint PyArray_ITER_NOTDONE(flatiter it) nogil
532 void PyArray_MultiIter_RESET(broadcast multi) nogil
533 void PyArray_MultiIter_NEXT(broadcast multi) nogil
534 void PyArray_MultiIter_GOTO(broadcast multi, npy_intp dest) nogil
535 void PyArray_MultiIter_GOTO1D(broadcast multi, npy_intp ind) nogil
536 void* PyArray_MultiIter_DATA(broadcast multi, npy_intp i) nogil
537 void PyArray_MultiIter_NEXTi(broadcast multi, npy_intp i) nogil
538 bint PyArray_MultiIter_NOTDONE(broadcast multi) nogil
540 # Functions from __multiarray_api.h
542 # Functions taking dtype and returning object/ndarray are disabled
543 # for now as they steal dtype references. I'm conservative and disable
544 # more than is probably needed until it can be checked further.
545 int PyArray_SetNumericOps (object)
546 object PyArray_GetNumericOps ()
547 int PyArray_INCREF (ndarray)
548 int PyArray_XDECREF (ndarray)
549 void PyArray_SetStringFunction (object, int)
550 dtype PyArray_DescrFromType (int)
551 object PyArray_TypeObjectFromType (int)
552 char * PyArray_Zero (ndarray)
553 char * PyArray_One (ndarray)
554 #object PyArray_CastToType (ndarray, dtype, int)
555 int PyArray_CastTo (ndarray, ndarray)
556 int PyArray_CastAnyTo (ndarray, ndarray)
557 int PyArray_CanCastSafely (int, int)
558 npy_bool PyArray_CanCastTo (dtype, dtype)
559 int PyArray_ObjectType (object, int)
560 dtype PyArray_DescrFromObject (object, dtype)
561 #ndarray* PyArray_ConvertToCommonType (object, int *)
562 dtype PyArray_DescrFromScalar (object)
563 dtype PyArray_DescrFromTypeObject (object)
564 npy_intp PyArray_Size (object)
565 #object PyArray_Scalar (void *, dtype, object)
566 #object PyArray_FromScalar (object, dtype)
567 void PyArray_ScalarAsCtype (object, void *)
568 #int PyArray_CastScalarToCtype (object, void *, dtype)
569 #int PyArray_CastScalarDirect (object, dtype, void *, int)
570 object PyArray_ScalarFromObject (object)
571 #PyArray_VectorUnaryFunc * PyArray_GetCastFunc (dtype, int)
572 object PyArray_FromDims (int, int *, int)
573 #object PyArray_FromDimsAndDataAndDescr (int, int *, dtype, char *)
574 #object PyArray_FromAny (object, dtype, int, int, int, object)
575 object PyArray_EnsureArray (object)
576 object PyArray_EnsureAnyArray (object)
577 #object PyArray_FromFile (stdio.FILE *, dtype, npy_intp, char *)
578 #object PyArray_FromString (char *, npy_intp, dtype, npy_intp, char *)
579 #object PyArray_FromBuffer (object, dtype, npy_intp, npy_intp)
580 #object PyArray_FromIter (object, dtype, npy_intp)
581 object PyArray_Return (ndarray)
582 #object PyArray_GetField (ndarray, dtype, int)
583 #int PyArray_SetField (ndarray, dtype, int, object)
584 object PyArray_Byteswap (ndarray, npy_bool)
585 object PyArray_Resize (ndarray, PyArray_Dims *, int, NPY_ORDER)
586 int PyArray_MoveInto (ndarray, ndarray)
587 int PyArray_CopyInto (ndarray, ndarray)
588 int PyArray_CopyAnyInto (ndarray, ndarray)
589 int PyArray_CopyObject (ndarray, object)
590 object PyArray_NewCopy (ndarray, NPY_ORDER)
591 object PyArray_ToList (ndarray)
592 object PyArray_ToString (ndarray, NPY_ORDER)
593 int PyArray_ToFile (ndarray, stdio.FILE *, char *, char *)
594 int PyArray_Dump (object, object, int)
595 object PyArray_Dumps (object, int)
596 int PyArray_ValidType (int)
597 void PyArray_UpdateFlags (ndarray, int)
598 object PyArray_New (type, int, npy_intp *, int, npy_intp *, void *, int, int, object)
599 #object PyArray_NewFromDescr (type, dtype, int, npy_intp *, npy_intp *, void *, int, object)
600 #dtype PyArray_DescrNew (dtype)
601 dtype PyArray_DescrNewFromType (int)
602 double PyArray_GetPriority (object, double)
603 object PyArray_IterNew (object)
604 object PyArray_MultiIterNew (int, ...)
606 int PyArray_PyIntAsInt (object)
607 npy_intp PyArray_PyIntAsIntp (object)
608 int PyArray_Broadcast (broadcast)
609 void PyArray_FillObjectArray (ndarray, object)
610 int PyArray_FillWithScalar (ndarray, object)
611 npy_bool PyArray_CheckStrides (int, int, npy_intp, npy_intp, npy_intp *, npy_intp *)
612 dtype PyArray_DescrNewByteorder (dtype, char)
613 object PyArray_IterAllButAxis (object, int *)
614 #object PyArray_CheckFromAny (object, dtype, int, int, int, object)
615 #object PyArray_FromArray (ndarray, dtype, int)
616 object PyArray_FromInterface (object)
617 object PyArray_FromStructInterface (object)
618 #object PyArray_FromArrayAttr (object, dtype, object)
619 #NPY_SCALARKIND PyArray_ScalarKind (int, ndarray*)
620 int PyArray_CanCoerceScalar (int, int, NPY_SCALARKIND)
621 object PyArray_NewFlagsObject (object)
622 npy_bool PyArray_CanCastScalar (type, type)
623 #int PyArray_CompareUCS4 (npy_ucs4 *, npy_ucs4 *, register size_t)
624 int PyArray_RemoveSmallest (broadcast)
625 int PyArray_ElementStrides (object)
626 void PyArray_Item_INCREF (char *, dtype)
627 void PyArray_Item_XDECREF (char *, dtype)
628 object PyArray_FieldNames (object)
629 object PyArray_Transpose (ndarray, PyArray_Dims *)
630 object PyArray_TakeFrom (ndarray, object, int, ndarray, NPY_CLIPMODE)
631 object PyArray_PutTo (ndarray, object, object, NPY_CLIPMODE)
632 object PyArray_PutMask (ndarray, object, object)
633 object PyArray_Repeat (ndarray, object, int)
634 object PyArray_Choose (ndarray, object, ndarray, NPY_CLIPMODE)
635 int PyArray_Sort (ndarray, int, NPY_SORTKIND)
636 object PyArray_ArgSort (ndarray, int, NPY_SORTKIND)
637 object PyArray_SearchSorted (ndarray, object, NPY_SEARCHSIDE)
638 object PyArray_ArgMax (ndarray, int, ndarray)
639 object PyArray_ArgMin (ndarray, int, ndarray)
640 object PyArray_Reshape (ndarray, object)
641 object PyArray_Newshape (ndarray, PyArray_Dims *, NPY_ORDER)
642 object PyArray_Squeeze (ndarray)
643 #object PyArray_View (ndarray, dtype, type)
644 object PyArray_SwapAxes (ndarray, int, int)
645 object PyArray_Max (ndarray, int, ndarray)
646 object PyArray_Min (ndarray, int, ndarray)
647 object PyArray_Ptp (ndarray, int, ndarray)
648 object PyArray_Mean (ndarray, int, int, ndarray)
649 object PyArray_Trace (ndarray, int, int, int, int, ndarray)
650 object PyArray_Diagonal (ndarray, int, int, int)
651 object PyArray_Clip (ndarray, object, object, ndarray)
652 object PyArray_Conjugate (ndarray, ndarray)
653 object PyArray_Nonzero (ndarray)
654 object PyArray_Std (ndarray, int, int, ndarray, int)
655 object PyArray_Sum (ndarray, int, int, ndarray)
656 object PyArray_CumSum (ndarray, int, int, ndarray)
657 object PyArray_Prod (ndarray, int, int, ndarray)
658 object PyArray_CumProd (ndarray, int, int, ndarray)
659 object PyArray_All (ndarray, int, ndarray)
660 object PyArray_Any (ndarray, int, ndarray)
661 object PyArray_Compress (ndarray, object, int, ndarray)
662 object PyArray_Flatten (ndarray, NPY_ORDER)
663 object PyArray_Ravel (ndarray, NPY_ORDER)
664 npy_intp PyArray_MultiplyList (npy_intp *, int)
665 int PyArray_MultiplyIntList (int *, int)
666 void * PyArray_GetPtr (ndarray, npy_intp*)
667 int PyArray_CompareLists (npy_intp *, npy_intp *, int)
668 #int PyArray_AsCArray (object*, void *, npy_intp *, int, dtype)
669 #int PyArray_As1D (object*, char **, int *, int)
670 #int PyArray_As2D (object*, char ***, int *, int *, int)
671 int PyArray_Free (object, void *)
672 #int PyArray_Converter (object, object*)
673 int PyArray_IntpFromSequence (object, npy_intp *, int)
674 object PyArray_Concatenate (object, int)
675 object PyArray_InnerProduct (object, object)
676 object PyArray_MatrixProduct (object, object)
677 object PyArray_CopyAndTranspose (object)
678 object PyArray_Correlate (object, object, int)
679 int PyArray_TypestrConvert (int, int)
680 #int PyArray_DescrConverter (object, dtype*)
681 #int PyArray_DescrConverter2 (object, dtype*)
682 int PyArray_IntpConverter (object, PyArray_Dims *)
683 #int PyArray_BufferConverter (object, chunk)
684 int PyArray_AxisConverter (object, int *)
685 int PyArray_BoolConverter (object, npy_bool *)
686 int PyArray_ByteorderConverter (object, char *)
687 int PyArray_OrderConverter (object, NPY_ORDER *)
688 unsigned char PyArray_EquivTypes (dtype, dtype)
689 #object PyArray_Zeros (int, npy_intp *, dtype, int)
690 #object PyArray_Empty (int, npy_intp *, dtype, int)
691 object PyArray_Where (object, object, object)
692 object PyArray_Arange (double, double, double, int)
693 #object PyArray_ArangeObj (object, object, object, dtype)
694 int PyArray_SortkindConverter (object, NPY_SORTKIND *)
695 object PyArray_LexSort (object, int)
696 object PyArray_Round (ndarray, int, ndarray)
697 unsigned char PyArray_EquivTypenums (int, int)
698 int PyArray_RegisterDataType (dtype)
699 int PyArray_RegisterCastFunc (dtype, int, PyArray_VectorUnaryFunc *)
700 int PyArray_RegisterCanCast (dtype, int, NPY_SCALARKIND)
701 #void PyArray_InitArrFuncs (PyArray_ArrFuncs *)
702 object PyArray_IntTupleFromIntp (int, npy_intp *)
703 int PyArray_TypeNumFromName (char *)
704 int PyArray_ClipmodeConverter (object, NPY_CLIPMODE *)
705 #int PyArray_OutputConverter (object, ndarray*)
706 object PyArray_BroadcastToShape (object, npy_intp *, int)
707 void _PyArray_SigintHandler (int)
708 void* _PyArray_GetSigintBuf ()
709 #int PyArray_DescrAlignConverter (object, dtype*)
710 #int PyArray_DescrAlignConverter2 (object, dtype*)
711 int PyArray_SearchsideConverter (object, void *)
712 object PyArray_CheckAxis (ndarray, int *, int)
713 npy_intp PyArray_OverflowMultiplyList (npy_intp *, int)
714 int PyArray_CompareString (char *, char *, size_t)
717 # Typedefs that matches the runtime dtype objects in
720 # The ones that are commented out needs an IFDEF function
721 # in Cython to enable them only on the right systems.
723 ctypedef npy_int8 int8_t
724 ctypedef npy_int16 int16_t
725 ctypedef npy_int32 int32_t
726 ctypedef npy_int64 int64_t
727 #ctypedef npy_int96 int96_t
728 #ctypedef npy_int128 int128_t
730 ctypedef npy_uint8 uint8_t
731 ctypedef npy_uint16 uint16_t
732 ctypedef npy_uint32 uint32_t
733 ctypedef npy_uint64 uint64_t
734 #ctypedef npy_uint96 uint96_t
735 #ctypedef npy_uint128 uint128_t
737 ctypedef npy_float32 float32_t
738 ctypedef npy_float64 float64_t
739 #ctypedef npy_float80 float80_t
740 #ctypedef npy_float128 float128_t
742 ctypedef float complex complex64_t
743 ctypedef double complex complex128_t
745 # The int types are mapped a bit surprising --
746 # numpy.int corresponds to 'l' and numpy.long to 'q'
747 ctypedef npy_long int_t
748 ctypedef npy_longlong long_t
749 ctypedef npy_longlong longlong_t
751 ctypedef npy_ulong uint_t
752 ctypedef npy_ulonglong ulong_t
753 ctypedef npy_ulonglong ulonglong_t
755 ctypedef npy_intp intp_t
756 ctypedef npy_uintp uintp_t
758 ctypedef npy_double float_t
759 ctypedef npy_double double_t
760 ctypedef npy_longdouble longdouble_t
762 ctypedef npy_cfloat cfloat_t
763 ctypedef npy_cdouble cdouble_t
764 ctypedef npy_clongdouble clongdouble_t
766 ctypedef npy_cdouble complex_t
768 cdef inline object PyArray_MultiIterNew1(a):
769 return PyArray_MultiIterNew(1, <void*>a)
771 cdef inline object PyArray_MultiIterNew2(a, b):
772 return PyArray_MultiIterNew(2, <void*>a, <void*>b)
774 cdef inline object PyArray_MultiIterNew3(a, b, c):
775 return PyArray_MultiIterNew(3, <void*>a, <void*>b, <void*> c)
777 cdef inline object PyArray_MultiIterNew4(a, b, c, d):
778 return PyArray_MultiIterNew(4, <void*>a, <void*>b, <void*>c, <void*> d)
780 cdef inline object PyArray_MultiIterNew5(a, b, c, d, e):
781 return PyArray_MultiIterNew(5, <void*>a, <void*>b, <void*>c, <void*> d, <void*> e)
783 cdef inline char* _util_dtypestring(dtype descr, char* f, char* end, int* offset) except NULL:
784 # Recursive utility function used in __getbuffer__ to get format
785 # string. The new location in the format string is returned.
788 cdef int delta_offset
790 cdef int endian_detector = 1
791 cdef bint little_endian = ((<char*>&endian_detector)[0] != 0)
794 for childname in descr.names:
795 fields = descr.fields[childname]
796 child, new_offset = fields
798 if (end - f) - <int>(new_offset - offset[0]) < 15:
799 raise RuntimeError(u"Format string allocated too short, see comment in numpy.pxd")
801 if ((child.byteorder == c'>' and little_endian) or
802 (child.byteorder == c'<' and not little_endian)):
803 raise ValueError(u"Non-native byte order not supported")
804 # One could encode it in the format string and have Cython
805 # complain instead, BUT: < and > in format strings also imply
806 # standardized sizes for datatypes, and we rely on native in
807 # order to avoid reencoding data types based on their size.
809 # A proper PEP 3118 exporter for other clients than Cython
810 # must deal properly with this!
812 # Output padding bytes
813 while offset[0] < new_offset:
814 f[0] = 120 # "x"; pad byte
818 offset[0] += child.itemsize
820 if not PyDataType_HASFIELDS(child):
823 raise RuntimeError(u"Format string allocated too short.")
825 # Until ticket #99 is fixed, use integers to avoid warnings
826 if t == NPY_BYTE: f[0] = 98 #"b"
827 elif t == NPY_UBYTE: f[0] = 66 #"B"
828 elif t == NPY_SHORT: f[0] = 104 #"h"
829 elif t == NPY_USHORT: f[0] = 72 #"H"
830 elif t == NPY_INT: f[0] = 105 #"i"
831 elif t == NPY_UINT: f[0] = 73 #"I"
832 elif t == NPY_LONG: f[0] = 108 #"l"
833 elif t == NPY_ULONG: f[0] = 76 #"L"
834 elif t == NPY_LONGLONG: f[0] = 113 #"q"
835 elif t == NPY_ULONGLONG: f[0] = 81 #"Q"
836 elif t == NPY_FLOAT: f[0] = 102 #"f"
837 elif t == NPY_DOUBLE: f[0] = 100 #"d"
838 elif t == NPY_LONGDOUBLE: f[0] = 103 #"g"
839 elif t == NPY_CFLOAT: f[0] = 90; f[1] = 102; f += 1 # Zf
840 elif t == NPY_CDOUBLE: f[0] = 90; f[1] = 100; f += 1 # Zd
841 elif t == NPY_CLONGDOUBLE: f[0] = 90; f[1] = 103; f += 1 # Zg
842 elif t == NPY_OBJECT: f[0] = 79 #"O"
844 raise ValueError(u"unknown dtype code in numpy.pxd (%d)" % t)
847 # Cython ignores struct boundary information ("T{...}"),
849 f = _util_dtypestring(child, f, end, offset)
857 cdef extern from "numpy/ufuncobject.h":
859 ctypedef void (*PyUFuncGenericFunction) (char **, npy_intp *, npy_intp *, void *)
861 ctypedef extern class numpy.ufunc [object PyUFuncObject]:
865 PyUFuncGenericFunction *functions
886 UFUNC_MASK_DIVIDEBYZERO
890 UFUNC_SHIFT_DIVIDEBYZERO
892 UFUNC_SHIFT_UNDERFLOW
894 UFUNC_FPE_DIVIDEBYZERO
901 object PyUFunc_FromFuncAndData(PyUFuncGenericFunction *,
902 void **, char *, int, int, int, int, char *, char *, int)
903 int PyUFunc_RegisterLoopForType(ufunc, int,
904 PyUFuncGenericFunction, int *, void *)
905 int PyUFunc_GenericFunction \
906 (ufunc, PyObject *, PyObject *, PyArrayObject **)
907 void PyUFunc_f_f_As_d_d \
908 (char **, npy_intp *, npy_intp *, void *)
910 (char **, npy_intp *, npy_intp *, void *)
912 (char **, npy_intp *, npy_intp *, void *)
914 (char **, npy_intp *, npy_intp *, void *)
915 void PyUFunc_F_F_As_D_D \
916 (char **, npy_intp *, npy_intp *, void *)
918 (char **, npy_intp *, npy_intp *, void *)
920 (char **, npy_intp *, npy_intp *, void *)
922 (char **, npy_intp *, npy_intp *, void *)
924 (char **, npy_intp *, npy_intp *, void *)
925 void PyUFunc_ff_f_As_dd_d \
926 (char **, npy_intp *, npy_intp *, void *)
928 (char **, npy_intp *, npy_intp *, void *)
930 (char **, npy_intp *, npy_intp *, void *)
932 (char **, npy_intp *, npy_intp *, void *)
933 void PyUFunc_FF_F_As_DD_D \
934 (char **, npy_intp *, npy_intp *, void *)
936 (char **, npy_intp *, npy_intp *, void *)
938 (char **, npy_intp *, npy_intp *, void *)
940 (char **, npy_intp *, npy_intp *, void *)
942 (char **, npy_intp *, npy_intp *, void *)
943 void PyUFunc_O_O_method \
944 (char **, npy_intp *, npy_intp *, void *)
945 void PyUFunc_OO_O_method \
946 (char **, npy_intp *, npy_intp *, void *)
948 (char **, npy_intp *, npy_intp *, void *)
949 int PyUFunc_GetPyValues \
950 (char *, int *, int *, PyObject **)
951 int PyUFunc_checkfperr \
952 (int, PyObject *, int *)
953 void PyUFunc_clearfperr()
954 int PyUFunc_getfperr()
955 int PyUFunc_handlefperr \
956 (int, PyObject *, int, int *)
957 int PyUFunc_ReplaceLoopBySignature \
958 (ufunc, PyUFuncGenericFunction, int *, PyUFuncGenericFunction *)
959 object PyUFunc_FromFuncAndDataAndSignature \
960 (PyUFuncGenericFunction *, void **, char *, int, int, int,
961 int, char *, char *, int, char *)
966 cdef inline void set_array_base(ndarray arr, object base):
967 cdef PyObject* baseptr
971 Py_INCREF(base) # important to do this before decref below!
972 baseptr = <PyObject*>base
976 cdef inline object get_array_base(ndarray arr):
980 return <object>arr.base