Home | History | Annotate | Download | only in numpy
      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-compatability 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 
     17 DEF _buffer_format_string_len = 255
     18 
     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
     25 
     26 cdef extern from "Python.h":
     27     ctypedef int Py_intptr_t
     28 
     29 cdef extern from "numpy/arrayobject.h":
     30     ctypedef Py_intptr_t npy_intp
     31     ctypedef size_t npy_uintp
     32 
     33     cdef enum NPY_TYPES:
     34         NPY_BOOL
     35         NPY_BYTE
     36         NPY_UBYTE
     37         NPY_SHORT
     38         NPY_USHORT
     39         NPY_INT
     40         NPY_UINT
     41         NPY_LONG
     42         NPY_ULONG
     43         NPY_LONGLONG
     44         NPY_ULONGLONG
     45         NPY_FLOAT
     46         NPY_DOUBLE
     47         NPY_LONGDOUBLE
     48         NPY_CFLOAT
     49         NPY_CDOUBLE
     50         NPY_CLONGDOUBLE
     51         NPY_OBJECT
     52         NPY_STRING
     53         NPY_UNICODE
     54         NPY_VOID
     55         NPY_NTYPES
     56         NPY_NOTYPE
     57 
     58         NPY_INT8
     59         NPY_INT16
     60         NPY_INT32
     61         NPY_INT64
     62         NPY_INT128
     63         NPY_INT256
     64         NPY_UINT8
     65         NPY_UINT16
     66         NPY_UINT32
     67         NPY_UINT64
     68         NPY_UINT128
     69         NPY_UINT256
     70         NPY_FLOAT16
     71         NPY_FLOAT32
     72         NPY_FLOAT64
     73         NPY_FLOAT80
     74         NPY_FLOAT96
     75         NPY_FLOAT128
     76         NPY_FLOAT256
     77         NPY_COMPLEX32
     78         NPY_COMPLEX64
     79         NPY_COMPLEX128
     80         NPY_COMPLEX160
     81         NPY_COMPLEX192
     82         NPY_COMPLEX256
     83         NPY_COMPLEX512
     84 
     85         NPY_INTP
     86 
     87     ctypedef enum NPY_ORDER:
     88         NPY_ANYORDER
     89         NPY_CORDER
     90         NPY_FORTRANORDER
     91 
     92     ctypedef enum NPY_CLIPMODE:
     93         NPY_CLIP
     94         NPY_WRAP
     95         NPY_RAISE
     96 
     97     ctypedef enum NPY_SCALARKIND:
     98         NPY_NOSCALAR,
     99         NPY_BOOL_SCALAR,
    100         NPY_INTPOS_SCALAR,
    101         NPY_INTNEG_SCALAR,
    102         NPY_FLOAT_SCALAR,
    103         NPY_COMPLEX_SCALAR,
    104         NPY_OBJECT_SCALAR
    105 
    106     ctypedef enum NPY_SORTKIND:
    107         NPY_QUICKSORT
    108         NPY_HEAPSORT
    109         NPY_MERGESORT
    110 
    111     ctypedef enum NPY_SEARCHSIDE:
    112         NPY_SEARCHLEFT
    113         NPY_SEARCHRIGHT
    114 
    115     enum:
    116         NPY_C_CONTIGUOUS
    117         NPY_F_CONTIGUOUS
    118         NPY_CONTIGUOUS
    119         NPY_FORTRAN
    120         NPY_OWNDATA
    121         NPY_FORCECAST
    122         NPY_ENSURECOPY
    123         NPY_ENSUREARRAY
    124         NPY_ELEMENTSTRIDES
    125         NPY_ALIGNED
    126         NPY_NOTSWAPPED
    127         NPY_WRITEABLE
    128         NPY_UPDATEIFCOPY
    129         NPY_ARR_HAS_DESCR
    130 
    131         NPY_BEHAVED
    132         NPY_BEHAVED_NS
    133         NPY_CARRAY
    134         NPY_CARRAY_RO
    135         NPY_FARRAY
    136         NPY_FARRAY_RO
    137         NPY_DEFAULT
    138 
    139         NPY_IN_ARRAY
    140         NPY_OUT_ARRAY
    141         NPY_INOUT_ARRAY
    142         NPY_IN_FARRAY
    143         NPY_OUT_FARRAY
    144         NPY_INOUT_FARRAY
    145 
    146         NPY_UPDATE_ALL
    147 
    148     cdef enum:
    149         NPY_MAXDIMS
    150 
    151     npy_intp NPY_MAX_ELSIZE
    152 
    153     ctypedef void (*PyArray_VectorUnaryFunc)(void *, void *, npy_intp, void *,  void *)
    154 
    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.
    159         cdef int type_num
    160         cdef int itemsize "elsize"
    161         cdef char byteorder
    162         cdef object fields
    163         cdef tuple names
    164 
    165     ctypedef extern class numpy.flatiter [object PyArrayIterObject]:
    166         # Use through macros
    167         pass
    168 
    169     ctypedef extern class numpy.broadcast [object PyArrayMultiIterObject]:
    170         # Use through macros
    171         pass
    172 
    173     ctypedef struct PyArrayObject:
    174         # For use in situations where ndarray can't replace PyArrayObject*,
    175         # like PyArrayObject**.
    176         pass
    177 
    178     ctypedef class numpy.ndarray [object PyArrayObject]:
    179         cdef __cythonbufferdefaults__ = {"mode": "strided"}
    180 
    181         cdef:
    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.
    184             char *data
    185             int ndim "nd"
    186             npy_intp *shape "dimensions"
    187             npy_intp *strides
    188             dtype descr
    189             PyObject* base
    190 
    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
    198             # of flags
    199 
    200             if info == NULL: return
    201 
    202             cdef int copy_shape, i, ndim
    203             cdef int endian_detector = 1
    204             cdef bint little_endian = ((<char*>&endian_detector)[0] != 0)
    205 
    206             ndim = PyArray_NDIM(self)
    207 
    208             if sizeof(npy_intp) != sizeof(Py_ssize_t):
    209                 copy_shape = 1
    210             else:
    211                 copy_shape = 0
    212 
    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")
    216 
    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")
    220 
    221             info.buf = PyArray_DATA(self)
    222             info.ndim = ndim
    223             if copy_shape:
    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]
    231             else:
    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)
    237 
    238             cdef int t
    239             cdef char* f = NULL
    240             cdef dtype descr = self.descr
    241             cdef list stack
    242             cdef int offset
    243 
    244             cdef bint hasfields = PyDataType_HASFIELDS(descr)
    245 
    246             if not hasfields and not copy_shape:
    247                 # do not call releasebuffer
    248                 info.obj = None
    249             else:
    250                 # need to call releasebuffer
    251                 info.obj = self
    252 
    253             if not hasfields:
    254                 t = descr.type_num
    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"
    275                 else:
    276                     raise ValueError(u"unknown dtype code in numpy.pxd (%d)" % t)
    277                 info.format = f
    278                 return
    279             else:
    280                 info.format = <char*>stdlib.malloc(_buffer_format_string_len)
    281                 info.format[0] = c'^' # Native data types, manual alignment
    282                 offset = 0
    283                 f = _util_dtypestring(descr, info.format + 1,
    284                                       info.format + _buffer_format_string_len,
    285                                       &offset)
    286                 f[0] = c'\0' # Terminate format string
    287 
    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
    294 
    295 
    296     ctypedef unsigned char      npy_bool
    297 
    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
    303 
    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
    309 
    310     ctypedef float        npy_float
    311     ctypedef double       npy_double
    312     ctypedef long double  npy_longdouble
    313 
    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
    320 
    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
    327 
    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
    333 
    334     ctypedef struct npy_cfloat:
    335         double real
    336         double imag
    337 
    338     ctypedef struct npy_cdouble:
    339         double real
    340         double imag
    341 
    342     ctypedef struct npy_clongdouble:
    343         double real
    344         double imag
    345 
    346     ctypedef struct npy_complex64:
    347         double real
    348         double imag
    349 
    350     ctypedef struct npy_complex128:
    351         double real
    352         double imag
    353 
    354     ctypedef struct npy_complex160:
    355         double real
    356         double imag
    357 
    358     ctypedef struct npy_complex192:
    359         double real
    360         double imag
    361 
    362     ctypedef struct npy_complex256:
    363         double real
    364         double imag
    365 
    366     ctypedef struct PyArray_Dims:
    367         npy_intp *ptr
    368         int len
    369 
    370     void import_array()
    371 
    372     #
    373     # Macros from ndarrayobject.h
    374     #
    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)
    379 
    380     int PyArray_NDIM(ndarray)
    381     bint PyArray_ISONESEGMENT(ndarray)
    382     bint PyArray_ISFORTRAN(ndarray)
    383     int PyArray_FORTRANIF(ndarray)
    384 
    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)
    391 
    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)
    397 
    398     object PyArray_GETITEM(ndarray arr, void *itemptr)
    399     int PyArray_SETITEM(ndarray arr, void *itemptr, object obj)
    400 
    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)
    414 
    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)
    429 
    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)
    444 
    445     bint PyArray_ISVARIABLE(ndarray)
    446 
    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)
    452 
    453     bint PyArray_FLAGSWAP(ndarray, int)
    454 
    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)
    461 
    462 
    463     bint PyDataType_ISNOTSWAPPED(dtype)
    464     bint PyDataType_ISBYTESWAPPED(dtype)
    465 
    466     bint PyArray_DescrCheck(object)
    467 
    468     bint PyArray_Check(object)
    469     bint PyArray_CheckExact(object)
    470 
    471     # Cannot be supported due to out arg:
    472     # bint PyArray_HasArrayInterfaceType(object, dtype, object, object&)
    473     # bint PyArray_HasArrayInterface(op, out)
    474 
    475 
    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)
    488 
    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)
    505 
    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)
    510 
    511     void PyArray_XDECREF_ERR(ndarray)
    512     # Cannot be supported due to out arg
    513     # void PyArray_DESCR_REPLACE(descr)
    514 
    515 
    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)
    520 
    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)
    524 
    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
    531 
    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
    539 
    540     # Functions from __multiarray_api.h
    541 
    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, ...)
    605 
    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)
    715 
    716 
    717 # Typedefs that matches the runtime dtype objects in
    718 # the numpy module.
    719 
    720 # The ones that are commented out needs an IFDEF function
    721 # in Cython to enable them only on the right systems.
    722 
    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
    729 
    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
    736 
    737 ctypedef npy_float32    float32_t
    738 ctypedef npy_float64    float64_t
    739 #ctypedef npy_float80    float80_t
    740 #ctypedef npy_float128   float128_t
    741 
    742 ctypedef float complex  complex64_t
    743 ctypedef double complex complex128_t
    744 
    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
    750 
    751 ctypedef npy_ulong      uint_t
    752 ctypedef npy_ulonglong  ulong_t
    753 ctypedef npy_ulonglong  ulonglong_t
    754 
    755 ctypedef npy_intp       intp_t
    756 ctypedef npy_uintp      uintp_t
    757 
    758 ctypedef npy_double     float_t
    759 ctypedef npy_double     double_t
    760 ctypedef npy_longdouble longdouble_t
    761 
    762 ctypedef npy_cfloat      cfloat_t
    763 ctypedef npy_cdouble     cdouble_t
    764 ctypedef npy_clongdouble clongdouble_t
    765 
    766 ctypedef npy_cdouble     complex_t
    767 
    768 cdef inline object PyArray_MultiIterNew1(a):
    769     return PyArray_MultiIterNew(1, <void*>a)
    770 
    771 cdef inline object PyArray_MultiIterNew2(a, b):
    772     return PyArray_MultiIterNew(2, <void*>a, <void*>b)
    773 
    774 cdef inline object PyArray_MultiIterNew3(a, b, c):
    775     return PyArray_MultiIterNew(3, <void*>a, <void*>b, <void*> c)
    776 
    777 cdef inline object PyArray_MultiIterNew4(a, b, c, d):
    778     return PyArray_MultiIterNew(4, <void*>a, <void*>b, <void*>c, <void*> d)
    779 
    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)
    782 
    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.
    786 
    787     cdef dtype child
    788     cdef int delta_offset
    789     cdef tuple i
    790     cdef int endian_detector = 1
    791     cdef bint little_endian = ((<char*>&endian_detector)[0] != 0)
    792     cdef tuple fields
    793 
    794     for childname in descr.names:
    795         fields = descr.fields[childname]
    796         child, new_offset = fields
    797 
    798         if (end - f) - <int>(new_offset - offset[0]) < 15:
    799             raise RuntimeError(u"Format string allocated too short, see comment in numpy.pxd")
    800 
    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.
    808             #
    809             # A proper PEP 3118 exporter for other clients than Cython
    810             # must deal properly with this!
    811 
    812         # Output padding bytes
    813         while offset[0] < new_offset:
    814             f[0] = 120 # "x"; pad byte
    815             f += 1
    816             offset[0] += 1
    817 
    818         offset[0] += child.itemsize
    819 
    820         if not PyDataType_HASFIELDS(child):
    821             t = child.type_num
    822             if end - f < 5:
    823                 raise RuntimeError(u"Format string allocated too short.")
    824 
    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"
    843             else:
    844                 raise ValueError(u"unknown dtype code in numpy.pxd (%d)" % t)
    845             f += 1
    846         else:
    847             # Cython ignores struct boundary information ("T{...}"),
    848             # so don't output it
    849             f = _util_dtypestring(child, f, end, offset)
    850     return f
    851 
    852 
    853 #
    854 # ufunc API
    855 #
    856 
    857 cdef extern from "numpy/ufuncobject.h":
    858 
    859     ctypedef void (*PyUFuncGenericFunction) (char **, npy_intp *, npy_intp *, void *)
    860 
    861     ctypedef extern class numpy.ufunc [object PyUFuncObject]:
    862         cdef:
    863             int nin, nout, nargs
    864             int identity
    865             PyUFuncGenericFunction *functions
    866             void **data
    867             int ntypes
    868             int check_return
    869             char *name
    870             char *types
    871             char *doc
    872             void *ptr
    873             PyObject *obj
    874             PyObject *userloops
    875 
    876     cdef enum:
    877         PyUFunc_Zero
    878         PyUFunc_One
    879         PyUFunc_None
    880         UFUNC_ERR_IGNORE
    881         UFUNC_ERR_WARN
    882         UFUNC_ERR_RAISE
    883         UFUNC_ERR_CALL
    884         UFUNC_ERR_PRINT
    885         UFUNC_ERR_LOG
    886         UFUNC_MASK_DIVIDEBYZERO
    887         UFUNC_MASK_OVERFLOW
    888         UFUNC_MASK_UNDERFLOW
    889         UFUNC_MASK_INVALID
    890         UFUNC_SHIFT_DIVIDEBYZERO
    891         UFUNC_SHIFT_OVERFLOW
    892         UFUNC_SHIFT_UNDERFLOW
    893         UFUNC_SHIFT_INVALID
    894         UFUNC_FPE_DIVIDEBYZERO
    895         UFUNC_FPE_OVERFLOW
    896         UFUNC_FPE_UNDERFLOW
    897         UFUNC_FPE_INVALID
    898         UFUNC_ERR_DEFAULT
    899         UFUNC_ERR_DEFAULT2
    900 
    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 *)
    909     void PyUFunc_d_d \
    910          (char **, npy_intp *, npy_intp *, void *)
    911     void PyUFunc_f_f \
    912          (char **, npy_intp *, npy_intp *, void *)
    913     void PyUFunc_g_g \
    914          (char **, npy_intp *, npy_intp *, void *)
    915     void PyUFunc_F_F_As_D_D \
    916          (char **, npy_intp *, npy_intp *, void *)
    917     void PyUFunc_F_F \
    918          (char **, npy_intp *, npy_intp *, void *)
    919     void PyUFunc_D_D \
    920          (char **, npy_intp *, npy_intp *, void *)
    921     void PyUFunc_G_G \
    922          (char **, npy_intp *, npy_intp *, void *)
    923     void PyUFunc_O_O \
    924          (char **, npy_intp *, npy_intp *, void *)
    925     void PyUFunc_ff_f_As_dd_d \
    926          (char **, npy_intp *, npy_intp *, void *)
    927     void PyUFunc_ff_f \
    928          (char **, npy_intp *, npy_intp *, void *)
    929     void PyUFunc_dd_d \
    930          (char **, npy_intp *, npy_intp *, void *)
    931     void PyUFunc_gg_g \
    932          (char **, npy_intp *, npy_intp *, void *)
    933     void PyUFunc_FF_F_As_DD_D \
    934          (char **, npy_intp *, npy_intp *, void *)
    935     void PyUFunc_DD_D \
    936          (char **, npy_intp *, npy_intp *, void *)
    937     void PyUFunc_FF_F \
    938          (char **, npy_intp *, npy_intp *, void *)
    939     void PyUFunc_GG_G \
    940          (char **, npy_intp *, npy_intp *, void *)
    941     void PyUFunc_OO_O \
    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 *)
    947     void PyUFunc_On_Om \
    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 *)
    962 
    963     void import_ufunc()
    964 
    965 
    966 cdef inline void set_array_base(ndarray arr, object base):
    967      cdef PyObject* baseptr
    968      if base is None:
    969          baseptr = NULL
    970      else:
    971          Py_INCREF(base) # important to do this before decref below!
    972          baseptr = <PyObject*>base
    973      Py_XDECREF(arr.base)
    974      arr.base = baseptr
    975 
    976 cdef inline object get_array_base(ndarray arr):
    977     if arr.base is NULL:
    978         return None
    979     else:
    980         return <object>arr.base
    981