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
cimport libc.stdlib as stdlib
23
cimport libc.stdio as stdio
25
cdef extern from "Python.h":
26
ctypedef int Py_intptr_t
28
cdef extern from "numpy/arrayobject.h":
29
ctypedef Py_intptr_t npy_intp
30
ctypedef size_t npy_uintp
109
cdef enum requirements:
145
npy_intp NPY_MAX_ELSIZE
147
ctypedef void (*PyArray_VectorUnaryFunc)(void *, void *, npy_intp, void *, void *)
149
ctypedef class numpy.dtype [object PyArray_Descr]:
150
# Use PyDataType_* macros when possible, however there are no macros
151
# for accessing some of the fields, so some are defined. Please
152
# ask on cython-dev if you need more.
154
cdef int itemsize "elsize"
159
ctypedef extern class numpy.flatiter [object PyArrayIterObject]:
163
ctypedef extern class numpy.broadcast [object PyArrayMultiIterObject]:
167
ctypedef struct PyArrayObject:
168
# For use in situations where ndarray can't replace PyArrayObject*,
169
# like PyArrayObject**.
172
ctypedef class numpy.ndarray [object PyArrayObject]:
173
cdef __cythonbufferdefaults__ = {"mode": "strided"}
176
# Only taking a few of the most commonly used and stable fields.
177
# One should use PyArray_* macros instead to access the C fields.
180
npy_intp *shape "dimensions"
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 fullfill the PEP.
191
# In particular strided access is always provided regardless
193
cdef int copy_shape, i, ndim
194
cdef int endian_detector = 1
195
cdef bint little_endian = ((<char*>&endian_detector)[0] != 0)
197
ndim = PyArray_NDIM(self)
199
if sizeof(npy_intp) != sizeof(Py_ssize_t):
204
if ((flags & pybuf.PyBUF_C_CONTIGUOUS == pybuf.PyBUF_C_CONTIGUOUS)
205
and not PyArray_CHKFLAGS(self, NPY_C_CONTIGUOUS)):
206
raise ValueError(u"ndarray is not C contiguous")
208
if ((flags & pybuf.PyBUF_F_CONTIGUOUS == pybuf.PyBUF_F_CONTIGUOUS)
209
and not PyArray_CHKFLAGS(self, NPY_F_CONTIGUOUS)):
210
raise ValueError(u"ndarray is not Fortran contiguous")
212
info.buf = PyArray_DATA(self)
215
# Allocate new buffer for strides and shape info. This is allocated
216
# as one block, strides first.
217
info.strides = <Py_ssize_t*>stdlib.malloc(sizeof(Py_ssize_t) * ndim * 2)
218
info.shape = info.strides + ndim
219
for i in range(ndim):
220
info.strides[i] = PyArray_STRIDES(self)[i]
221
info.shape[i] = PyArray_DIMS(self)[i]
223
info.strides = <Py_ssize_t*>PyArray_STRIDES(self)
224
info.shape = <Py_ssize_t*>PyArray_DIMS(self)
225
info.suboffsets = NULL
226
info.itemsize = PyArray_ITEMSIZE(self)
227
info.readonly = not PyArray_ISWRITEABLE(self)
231
cdef dtype descr = self.descr
235
cdef bint hasfields = PyDataType_HASFIELDS(descr)
237
if not hasfields and not copy_shape:
238
# do not call releasebuffer
241
# need to call releasebuffer
246
if ((descr.byteorder == '>' and little_endian) or
247
(descr.byteorder == '<' and not little_endian)):
248
raise ValueError(u"Non-native byte order not supported")
249
if t == NPY_BYTE: f = "b"
250
elif t == NPY_UBYTE: f = "B"
251
elif t == NPY_SHORT: f = "h"
252
elif t == NPY_USHORT: f = "H"
253
elif t == NPY_INT: f = "i"
254
elif t == NPY_UINT: f = "I"
255
elif t == NPY_LONG: f = "l"
256
elif t == NPY_ULONG: f = "L"
257
elif t == NPY_LONGLONG: f = "q"
258
elif t == NPY_ULONGLONG: f = "Q"
259
elif t == NPY_FLOAT: f = "f"
260
elif t == NPY_DOUBLE: f = "d"
261
elif t == NPY_LONGDOUBLE: f = "g"
262
elif t == NPY_CFLOAT: f = "Zf"
263
elif t == NPY_CDOUBLE: f = "Zd"
264
elif t == NPY_CLONGDOUBLE: f = "Zg"
265
elif t == NPY_OBJECT: f = "O"
267
raise ValueError(u"unknown dtype code in numpy.pxd (%d)" % t)
271
info.format = <char*>stdlib.malloc(_buffer_format_string_len)
272
info.format[0] = '^' # Native data types, manual alignment
274
f = _util_dtypestring(descr, info.format + 1,
275
info.format + _buffer_format_string_len,
277
f[0] = 0 # Terminate format string
279
def __releasebuffer__(ndarray self, Py_buffer* info):
280
if PyArray_HASFIELDS(self):
281
stdlib.free(info.format)
282
if sizeof(npy_intp) != sizeof(Py_ssize_t):
283
stdlib.free(info.strides)
284
# info.shape was stored after info.strides in the same block
287
ctypedef signed char npy_bool
289
ctypedef signed char npy_byte
290
ctypedef signed short npy_short
291
ctypedef signed int npy_int
292
ctypedef signed long npy_long
293
ctypedef signed long long npy_longlong
295
ctypedef unsigned char npy_ubyte
296
ctypedef unsigned short npy_ushort
297
ctypedef unsigned int npy_uint
298
ctypedef unsigned long npy_ulong
299
ctypedef unsigned long long npy_ulonglong
301
ctypedef float npy_float
302
ctypedef double npy_double
303
ctypedef long double npy_longdouble
305
ctypedef signed char npy_int8
306
ctypedef signed short npy_int16
307
ctypedef signed int npy_int32
308
ctypedef signed long long npy_int64
309
ctypedef signed long long npy_int96
310
ctypedef signed long long npy_int128
312
ctypedef unsigned char npy_uint8
313
ctypedef unsigned short npy_uint16
314
ctypedef unsigned int npy_uint32
315
ctypedef unsigned long long npy_uint64
316
ctypedef unsigned long long npy_uint96
317
ctypedef unsigned long long npy_uint128
319
ctypedef float npy_float32
320
ctypedef double npy_float64
321
ctypedef long double npy_float80
322
ctypedef long double npy_float96
323
ctypedef long double npy_float128
325
ctypedef struct npy_cfloat:
329
ctypedef struct npy_cdouble:
333
ctypedef struct npy_clongdouble:
337
ctypedef struct npy_complex64:
341
ctypedef struct npy_complex128:
345
ctypedef struct npy_complex160:
349
ctypedef struct npy_complex192:
353
ctypedef struct npy_complex256:
357
ctypedef struct PyArray_Dims:
364
# Macros from ndarrayobject.h
366
bint PyArray_CHKFLAGS(ndarray m, int flags)
367
bint PyArray_ISCONTIGUOUS(ndarray m)
368
bint PyArray_ISWRITEABLE(ndarray m)
369
bint PyArray_ISALIGNED(ndarray m)
371
int PyArray_NDIM(ndarray)
372
bint PyArray_ISONESEGMENT(ndarray)
373
bint PyArray_ISFORTRAN(ndarray)
374
int PyArray_FORTRANIF(ndarray)
376
void* PyArray_DATA(ndarray)
377
char* PyArray_BYTES(ndarray)
378
npy_intp* PyArray_DIMS(ndarray)
379
npy_intp* PyArray_STRIDES(ndarray)
380
npy_intp PyArray_DIM(ndarray, size_t)
381
npy_intp PyArray_STRIDE(ndarray, size_t)
383
# object PyArray_BASE(ndarray) wrong refcount semantics
384
# dtype PyArray_DESCR(ndarray) wrong refcount semantics
385
int PyArray_FLAGS(ndarray)
386
npy_intp PyArray_ITEMSIZE(ndarray)
387
int PyArray_TYPE(ndarray arr)
389
object PyArray_GETITEM(ndarray arr, void *itemptr)
390
int PyArray_SETITEM(ndarray arr, void *itemptr, object obj)
392
bint PyTypeNum_ISBOOL(int)
393
bint PyTypeNum_ISUNSIGNED(int)
394
bint PyTypeNum_ISSIGNED(int)
395
bint PyTypeNum_ISINTEGER(int)
396
bint PyTypeNum_ISFLOAT(int)
397
bint PyTypeNum_ISNUMBER(int)
398
bint PyTypeNum_ISSTRING(int)
399
bint PyTypeNum_ISCOMPLEX(int)
400
bint PyTypeNum_ISPYTHON(int)
401
bint PyTypeNum_ISFLEXIBLE(int)
402
bint PyTypeNum_ISUSERDEF(int)
403
bint PyTypeNum_ISEXTENDED(int)
404
bint PyTypeNum_ISOBJECT(int)
406
bint PyDataType_ISBOOL(dtype)
407
bint PyDataType_ISUNSIGNED(dtype)
408
bint PyDataType_ISSIGNED(dtype)
409
bint PyDataType_ISINTEGER(dtype)
410
bint PyDataType_ISFLOAT(dtype)
411
bint PyDataType_ISNUMBER(dtype)
412
bint PyDataType_ISSTRING(dtype)
413
bint PyDataType_ISCOMPLEX(dtype)
414
bint PyDataType_ISPYTHON(dtype)
415
bint PyDataType_ISFLEXIBLE(dtype)
416
bint PyDataType_ISUSERDEF(dtype)
417
bint PyDataType_ISEXTENDED(dtype)
418
bint PyDataType_ISOBJECT(dtype)
419
bint PyDataType_HASFIELDS(dtype)
421
bint PyArray_ISBOOL(ndarray)
422
bint PyArray_ISUNSIGNED(ndarray)
423
bint PyArray_ISSIGNED(ndarray)
424
bint PyArray_ISINTEGER(ndarray)
425
bint PyArray_ISFLOAT(ndarray)
426
bint PyArray_ISNUMBER(ndarray)
427
bint PyArray_ISSTRING(ndarray)
428
bint PyArray_ISCOMPLEX(ndarray)
429
bint PyArray_ISPYTHON(ndarray)
430
bint PyArray_ISFLEXIBLE(ndarray)
431
bint PyArray_ISUSERDEF(ndarray)
432
bint PyArray_ISEXTENDED(ndarray)
433
bint PyArray_ISOBJECT(ndarray)
434
bint PyArray_HASFIELDS(ndarray)
436
bint PyArray_ISVARIABLE(ndarray)
438
bint PyArray_SAFEALIGNEDCOPY(ndarray)
439
bint PyArray_ISNBO(ndarray)
440
bint PyArray_IsNativeByteOrder(ndarray)
441
bint PyArray_ISNOTSWAPPED(ndarray)
442
bint PyArray_ISBYTESWAPPED(ndarray)
444
bint PyArray_FLAGSWAP(ndarray, int)
446
bint PyArray_ISCARRAY(ndarray)
447
bint PyArray_ISCARRAY_RO(ndarray)
448
bint PyArray_ISFARRAY(ndarray)
449
bint PyArray_ISFARRAY_RO(ndarray)
450
bint PyArray_ISBEHAVED(ndarray)
451
bint PyArray_ISBEHAVED_RO(ndarray)
454
bint PyDataType_ISNOTSWAPPED(dtype)
455
bint PyDataType_ISBYTESWAPPED(dtype)
457
bint PyArray_DescrCheck(object)
459
bint PyArray_Check(object)
460
bint PyArray_CheckExact(object)
462
# Cannot be supported due to out arg:
463
# bint PyArray_HasArrayInterfaceType(object, dtype, object, object&)
464
# bint PyArray_HasArrayInterface(op, out)
467
bint PyArray_IsZeroDim(object)
468
# Cannot be supported due to ## ## in macro:
469
# bint PyArray_IsScalar(object, verbatim work)
470
bint PyArray_CheckScalar(object)
471
bint PyArray_IsPythonNumber(object)
472
bint PyArray_IsPythonScalar(object)
473
bint PyArray_IsAnyScalar(object)
474
bint PyArray_CheckAnyScalar(object)
475
ndarray PyArray_GETCONTIGUOUS(ndarray)
476
bint PyArray_SAMESHAPE(ndarray, ndarray)
477
npy_intp PyArray_SIZE(ndarray)
478
npy_intp PyArray_NBYTES(ndarray)
480
object PyArray_FROM_O(object)
481
object PyArray_FROM_OF(object m, int flags)
482
bint PyArray_FROM_OT(object m, int type)
483
bint PyArray_FROM_OTF(object m, int type, int flags)
484
object PyArray_FROMANY(object m, int type, int min, int max, int flags)
485
object PyArray_ZEROS(int nd, npy_intp* dims, int type, int fortran)
486
object PyArray_EMPTY(int nd, npy_intp* dims, int type, int fortran)
487
void PyArray_FILLWBYTE(object, int val)
488
npy_intp PyArray_REFCOUNT(object)
489
object PyArray_ContiguousFromAny(op, int, int min_depth, int max_depth)
490
unsigned char PyArray_EquivArrTypes(ndarray a1, ndarray a2)
491
bint PyArray_EquivByteorders(int b1, int b2)
492
object PyArray_SimpleNew(int nd, npy_intp* dims, int typenum)
493
object PyArray_SimpleNewFromData(int nd, npy_intp* dims, int typenum, void* data)
494
#object PyArray_SimpleNewFromDescr(int nd, npy_intp* dims, dtype descr)
495
object PyArray_ToScalar(void* data, ndarray arr)
497
void* PyArray_GETPTR1(ndarray m, npy_intp i)
498
void* PyArray_GETPTR2(ndarray m, npy_intp i, npy_intp j)
499
void* PyArray_GETPTR3(ndarray m, npy_intp i, npy_intp j, npy_intp k)
500
void* PyArray_GETPTR4(ndarray m, npy_intp i, npy_intp j, npy_intp k, npy_intp l)
502
void PyArray_XDECREF_ERR(ndarray)
503
# Cannot be supported due to out arg
504
# void PyArray_DESCR_REPLACE(descr)
507
object PyArray_Copy(ndarray)
508
object PyArray_FromObject(object op, int type, int min_depth, int max_depth)
509
object PyArray_ContiguousFromObject(object op, int type, int min_depth, int max_depth)
510
object PyArray_CopyFromObject(object op, int type, int min_depth, int max_depth)
512
object PyArray_Cast(ndarray mp, int type_num)
513
object PyArray_Take(ndarray ap, object items, int axis)
514
object PyArray_Put(ndarray ap, object items, object values)
516
void PyArray_ITER_RESET(flatiter it) nogil
517
void PyArray_ITER_NEXT(flatiter it) nogil
518
void PyArray_ITER_GOTO(flatiter it, npy_intp* destination) nogil
519
void PyArray_ITER_GOTO1D(flatiter it, npy_intp ind) nogil
520
void* PyArray_ITER_DATA(flatiter it) nogil
521
bint PyArray_ITER_NOTDONE(flatiter it) nogil
523
void PyArray_MultiIter_RESET(broadcast multi) nogil
524
void PyArray_MultiIter_NEXT(broadcast multi) nogil
525
void PyArray_MultiIter_GOTO(broadcast multi, npy_intp dest) nogil
526
void PyArray_MultiIter_GOTO1D(broadcast multi, npy_intp ind) nogil
527
void* PyArray_MultiIter_DATA(broadcast multi, npy_intp i) nogil
528
void PyArray_MultiIter_NEXTi(broadcast multi, npy_intp i) nogil
529
bint PyArray_MultiIter_NOTDONE(broadcast multi) nogil
531
# Functions from __multiarray_api.h
533
# Functions taking dtype and returning object/ndarray are disabled
534
# for now as they steal dtype references. I'm conservative and disable
535
# more than is probably needed until it can be checked further.
536
int PyArray_SetNumericOps (object)
537
object PyArray_GetNumericOps ()
538
int PyArray_INCREF (ndarray)
539
int PyArray_XDECREF (ndarray)
540
void PyArray_SetStringFunction (object, int)
541
dtype PyArray_DescrFromType (int)
542
object PyArray_TypeObjectFromType (int)
543
char * PyArray_Zero (ndarray)
544
char * PyArray_One (ndarray)
545
#object PyArray_CastToType (ndarray, dtype, int)
546
int PyArray_CastTo (ndarray, ndarray)
547
int PyArray_CastAnyTo (ndarray, ndarray)
548
int PyArray_CanCastSafely (int, int)
549
npy_bool PyArray_CanCastTo (dtype, dtype)
550
int PyArray_ObjectType (object, int)
551
dtype PyArray_DescrFromObject (object, dtype)
552
#ndarray* PyArray_ConvertToCommonType (object, int *)
553
dtype PyArray_DescrFromScalar (object)
554
dtype PyArray_DescrFromTypeObject (object)
555
npy_intp PyArray_Size (object)
556
#object PyArray_Scalar (void *, dtype, object)
557
#object PyArray_FromScalar (object, dtype)
558
void PyArray_ScalarAsCtype (object, void *)
559
#int PyArray_CastScalarToCtype (object, void *, dtype)
560
#int PyArray_CastScalarDirect (object, dtype, void *, int)
561
object PyArray_ScalarFromObject (object)
562
#PyArray_VectorUnaryFunc * PyArray_GetCastFunc (dtype, int)
563
object PyArray_FromDims (int, int *, int)
564
#object PyArray_FromDimsAndDataAndDescr (int, int *, dtype, char *)
565
#object PyArray_FromAny (object, dtype, int, int, int, object)
566
object PyArray_EnsureArray (object)
567
object PyArray_EnsureAnyArray (object)
568
#object PyArray_FromFile (stdio.FILE *, dtype, npy_intp, char *)
569
#object PyArray_FromString (char *, npy_intp, dtype, npy_intp, char *)
570
#object PyArray_FromBuffer (object, dtype, npy_intp, npy_intp)
571
#object PyArray_FromIter (object, dtype, npy_intp)
572
object PyArray_Return (ndarray)
573
#object PyArray_GetField (ndarray, dtype, int)
574
#int PyArray_SetField (ndarray, dtype, int, object)
575
object PyArray_Byteswap (ndarray, npy_bool)
576
object PyArray_Resize (ndarray, PyArray_Dims *, int, NPY_ORDER)
577
int PyArray_MoveInto (ndarray, ndarray)
578
int PyArray_CopyInto (ndarray, ndarray)
579
int PyArray_CopyAnyInto (ndarray, ndarray)
580
int PyArray_CopyObject (ndarray, object)
581
object PyArray_NewCopy (ndarray, NPY_ORDER)
582
object PyArray_ToList (ndarray)
583
object PyArray_ToString (ndarray, NPY_ORDER)
584
int PyArray_ToFile (ndarray, stdio.FILE *, char *, char *)
585
int PyArray_Dump (object, object, int)
586
object PyArray_Dumps (object, int)
587
int PyArray_ValidType (int)
588
void PyArray_UpdateFlags (ndarray, int)
589
object PyArray_New (type, int, npy_intp *, int, npy_intp *, void *, int, int, object)
590
#object PyArray_NewFromDescr (type, dtype, int, npy_intp *, npy_intp *, void *, int, object)
591
#dtype PyArray_DescrNew (dtype)
592
dtype PyArray_DescrNewFromType (int)
593
double PyArray_GetPriority (object, double)
594
object PyArray_IterNew (object)
595
object PyArray_MultiIterNew (int, ...)
597
int PyArray_PyIntAsInt (object)
598
npy_intp PyArray_PyIntAsIntp (object)
599
int PyArray_Broadcast (broadcast)
600
void PyArray_FillObjectArray (ndarray, object)
601
int PyArray_FillWithScalar (ndarray, object)
602
npy_bool PyArray_CheckStrides (int, int, npy_intp, npy_intp, npy_intp *, npy_intp *)
603
dtype PyArray_DescrNewByteorder (dtype, char)
604
object PyArray_IterAllButAxis (object, int *)
605
#object PyArray_CheckFromAny (object, dtype, int, int, int, object)
606
#object PyArray_FromArray (ndarray, dtype, int)
607
object PyArray_FromInterface (object)
608
object PyArray_FromStructInterface (object)
609
#object PyArray_FromArrayAttr (object, dtype, object)
610
#NPY_SCALARKIND PyArray_ScalarKind (int, ndarray*)
611
int PyArray_CanCoerceScalar (int, int, NPY_SCALARKIND)
612
object PyArray_NewFlagsObject (object)
613
npy_bool PyArray_CanCastScalar (type, type)
614
#int PyArray_CompareUCS4 (npy_ucs4 *, npy_ucs4 *, register size_t)
615
int PyArray_RemoveSmallest (broadcast)
616
int PyArray_ElementStrides (object)
617
void PyArray_Item_INCREF (char *, dtype)
618
void PyArray_Item_XDECREF (char *, dtype)
619
object PyArray_FieldNames (object)
620
object PyArray_Transpose (ndarray, PyArray_Dims *)
621
object PyArray_TakeFrom (ndarray, object, int, ndarray, NPY_CLIPMODE)
622
object PyArray_PutTo (ndarray, object, object, NPY_CLIPMODE)
623
object PyArray_PutMask (ndarray, object, object)
624
object PyArray_Repeat (ndarray, object, int)
625
object PyArray_Choose (ndarray, object, ndarray, NPY_CLIPMODE)
626
int PyArray_Sort (ndarray, int, NPY_SORTKIND)
627
object PyArray_ArgSort (ndarray, int, NPY_SORTKIND)
628
object PyArray_SearchSorted (ndarray, object, NPY_SEARCHSIDE)
629
object PyArray_ArgMax (ndarray, int, ndarray)
630
object PyArray_ArgMin (ndarray, int, ndarray)
631
object PyArray_Reshape (ndarray, object)
632
object PyArray_Newshape (ndarray, PyArray_Dims *, NPY_ORDER)
633
object PyArray_Squeeze (ndarray)
634
#object PyArray_View (ndarray, dtype, type)
635
object PyArray_SwapAxes (ndarray, int, int)
636
object PyArray_Max (ndarray, int, ndarray)
637
object PyArray_Min (ndarray, int, ndarray)
638
object PyArray_Ptp (ndarray, int, ndarray)
639
object PyArray_Mean (ndarray, int, int, ndarray)
640
object PyArray_Trace (ndarray, int, int, int, int, ndarray)
641
object PyArray_Diagonal (ndarray, int, int, int)
642
object PyArray_Clip (ndarray, object, object, ndarray)
643
object PyArray_Conjugate (ndarray, ndarray)
644
object PyArray_Nonzero (ndarray)
645
object PyArray_Std (ndarray, int, int, ndarray, int)
646
object PyArray_Sum (ndarray, int, int, ndarray)
647
object PyArray_CumSum (ndarray, int, int, ndarray)
648
object PyArray_Prod (ndarray, int, int, ndarray)
649
object PyArray_CumProd (ndarray, int, int, ndarray)
650
object PyArray_All (ndarray, int, ndarray)
651
object PyArray_Any (ndarray, int, ndarray)
652
object PyArray_Compress (ndarray, object, int, ndarray)
653
object PyArray_Flatten (ndarray, NPY_ORDER)
654
object PyArray_Ravel (ndarray, NPY_ORDER)
655
npy_intp PyArray_MultiplyList (npy_intp *, int)
656
int PyArray_MultiplyIntList (int *, int)
657
void * PyArray_GetPtr (ndarray, npy_intp*)
658
int PyArray_CompareLists (npy_intp *, npy_intp *, int)
659
#int PyArray_AsCArray (object*, void *, npy_intp *, int, dtype)
660
#int PyArray_As1D (object*, char **, int *, int)
661
#int PyArray_As2D (object*, char ***, int *, int *, int)
662
int PyArray_Free (object, void *)
663
#int PyArray_Converter (object, object*)
664
int PyArray_IntpFromSequence (object, npy_intp *, int)
665
object PyArray_Concatenate (object, int)
666
object PyArray_InnerProduct (object, object)
667
object PyArray_MatrixProduct (object, object)
668
object PyArray_CopyAndTranspose (object)
669
object PyArray_Correlate (object, object, int)
670
int PyArray_TypestrConvert (int, int)
671
#int PyArray_DescrConverter (object, dtype*)
672
#int PyArray_DescrConverter2 (object, dtype*)
673
int PyArray_IntpConverter (object, PyArray_Dims *)
674
#int PyArray_BufferConverter (object, chunk)
675
int PyArray_AxisConverter (object, int *)
676
int PyArray_BoolConverter (object, npy_bool *)
677
int PyArray_ByteorderConverter (object, char *)
678
int PyArray_OrderConverter (object, NPY_ORDER *)
679
unsigned char PyArray_EquivTypes (dtype, dtype)
680
#object PyArray_Zeros (int, npy_intp *, dtype, int)
681
#object PyArray_Empty (int, npy_intp *, dtype, int)
682
object PyArray_Where (object, object, object)
683
object PyArray_Arange (double, double, double, int)
684
#object PyArray_ArangeObj (object, object, object, dtype)
685
int PyArray_SortkindConverter (object, NPY_SORTKIND *)
686
object PyArray_LexSort (object, int)
687
object PyArray_Round (ndarray, int, ndarray)
688
unsigned char PyArray_EquivTypenums (int, int)
689
int PyArray_RegisterDataType (dtype)
690
int PyArray_RegisterCastFunc (dtype, int, PyArray_VectorUnaryFunc *)
691
int PyArray_RegisterCanCast (dtype, int, NPY_SCALARKIND)
692
#void PyArray_InitArrFuncs (PyArray_ArrFuncs *)
693
object PyArray_IntTupleFromIntp (int, npy_intp *)
694
int PyArray_TypeNumFromName (char *)
695
int PyArray_ClipmodeConverter (object, NPY_CLIPMODE *)
696
#int PyArray_OutputConverter (object, ndarray*)
697
object PyArray_BroadcastToShape (object, npy_intp *, int)
698
void _PyArray_SigintHandler (int)
699
void* _PyArray_GetSigintBuf ()
700
#int PyArray_DescrAlignConverter (object, dtype*)
701
#int PyArray_DescrAlignConverter2 (object, dtype*)
702
int PyArray_SearchsideConverter (object, void *)
703
object PyArray_CheckAxis (ndarray, int *, int)
704
npy_intp PyArray_OverflowMultiplyList (npy_intp *, int)
705
int PyArray_CompareString (char *, char *, size_t)
708
# Typedefs that matches the runtime dtype objects in
711
# The ones that are commented out needs an IFDEF function
712
# in Cython to enable them only on the right systems.
714
ctypedef npy_int8 int8_t
715
ctypedef npy_int16 int16_t
716
ctypedef npy_int32 int32_t
717
ctypedef npy_int64 int64_t
718
#ctypedef npy_int96 int96_t
719
#ctypedef npy_int128 int128_t
721
ctypedef npy_uint8 uint8_t
722
ctypedef npy_uint16 uint16_t
723
ctypedef npy_uint32 uint32_t
724
ctypedef npy_uint64 uint64_t
725
#ctypedef npy_uint96 uint96_t
726
#ctypedef npy_uint128 uint128_t
728
ctypedef npy_float32 float32_t
729
ctypedef npy_float64 float64_t
730
#ctypedef npy_float80 float80_t
731
#ctypedef npy_float128 float128_t
733
ctypedef float complex complex64_t
734
ctypedef double complex complex128_t
736
# The int types are mapped a bit surprising --
737
# numpy.int corresponds to 'l' and numpy.long to 'q'
738
ctypedef npy_long int_t
739
ctypedef npy_longlong long_t
740
ctypedef npy_intp intp_t
741
ctypedef npy_uintp uintp_t
743
ctypedef npy_ulong uint_t
744
ctypedef npy_ulonglong ulong_t
746
ctypedef npy_double float_t
747
ctypedef npy_double double_t
748
ctypedef npy_longdouble longdouble_t
750
ctypedef npy_cfloat cfloat_t
751
ctypedef npy_cdouble cdouble_t
752
ctypedef npy_clongdouble clongdouble_t
754
ctypedef npy_cdouble complex_t
756
cdef inline object PyArray_MultiIterNew1(a):
757
return PyArray_MultiIterNew(1, <void*>a)
759
cdef inline object PyArray_MultiIterNew2(a, b):
760
return PyArray_MultiIterNew(2, <void*>a, <void*>b)
762
cdef inline object PyArray_MultiIterNew3(a, b, c):
763
return PyArray_MultiIterNew(3, <void*>a, <void*>b, <void*> c)
765
cdef inline object PyArray_MultiIterNew4(a, b, c, d):
766
return PyArray_MultiIterNew(4, <void*>a, <void*>b, <void*>c, <void*> d)
768
cdef inline object PyArray_MultiIterNew5(a, b, c, d, e):
769
return PyArray_MultiIterNew(5, <void*>a, <void*>b, <void*>c, <void*> d, <void*> e)
771
cdef inline char* _util_dtypestring(dtype descr, char* f, char* end, int* offset) except NULL:
772
# Recursive utility function used in __getbuffer__ to get format
773
# string. The new location in the format string is returned.
776
cdef int delta_offset
778
cdef int endian_detector = 1
779
cdef bint little_endian = ((<char*>&endian_detector)[0] != 0)
782
for childname in descr.names:
783
fields = descr.fields[childname]
784
child, new_offset = fields
786
if (end - f) - (new_offset - offset[0]) < 15:
787
raise RuntimeError(u"Format string allocated too short, see comment in numpy.pxd")
789
if ((child.byteorder == '>' and little_endian) or
790
(child.byteorder == '<' and not little_endian)):
791
raise ValueError(u"Non-native byte order not supported")
792
# One could encode it in the format string and have Cython
793
# complain instead, BUT: < and > in format strings also imply
794
# standardized sizes for datatypes, and we rely on native in
795
# order to avoid reencoding data types based on their size.
797
# A proper PEP 3118 exporter for other clients than Cython
798
# must deal properly with this!
800
# Output padding bytes
801
while offset[0] < new_offset:
802
f[0] = 120 # "x"; pad byte
806
offset[0] += child.itemsize
808
if not PyDataType_HASFIELDS(child):
811
raise RuntimeError(u"Format string allocated too short.")
813
# Until ticket #99 is fixed, use integers to avoid warnings
814
if t == NPY_BYTE: f[0] = 98 #"b"
815
elif t == NPY_UBYTE: f[0] = 66 #"B"
816
elif t == NPY_SHORT: f[0] = 104 #"h"
817
elif t == NPY_USHORT: f[0] = 72 #"H"
818
elif t == NPY_INT: f[0] = 105 #"i"
819
elif t == NPY_UINT: f[0] = 73 #"I"
820
elif t == NPY_LONG: f[0] = 108 #"l"
821
elif t == NPY_ULONG: f[0] = 76 #"L"
822
elif t == NPY_LONGLONG: f[0] = 113 #"q"
823
elif t == NPY_ULONGLONG: f[0] = 81 #"Q"
824
elif t == NPY_FLOAT: f[0] = 102 #"f"
825
elif t == NPY_DOUBLE: f[0] = 100 #"d"
826
elif t == NPY_LONGDOUBLE: f[0] = 103 #"g"
827
elif t == NPY_CFLOAT: f[0] = 90; f[1] = 102; f += 1 # Zf
828
elif t == NPY_CDOUBLE: f[0] = 90; f[1] = 100; f += 1 # Zd
829
elif t == NPY_CLONGDOUBLE: f[0] = 90; f[1] = 103; f += 1 # Zg
830
elif t == NPY_OBJECT: f[0] = 79 #"O"
832
raise ValueError(u"unknown dtype code in numpy.pxd (%d)" % t)
835
# Cython ignores struct boundary information ("T{...}"),
837
f = _util_dtypestring(child, f, end, offset)
845
cdef extern from "numpy/ufuncobject.h":
847
ctypedef void (*PyUFuncGenericFunction) (char **, npy_intp *, npy_intp *, void *)
849
ctypedef extern class numpy.ufunc [object PyUFuncObject]:
853
PyUFuncGenericFunction *functions
873
UFUNC_MASK_DIVIDEBYZERO
877
UFUNC_SHIFT_DIVIDEBYZERO
879
UFUNC_SHIFT_UNDERFLOW
881
UFUNC_FPE_DIVIDEBYZERO
888
object PyUFunc_FromFuncAndData(PyUFuncGenericFunction *,
889
void **, char *, int, int, int, int, char *, char *, int)
890
int PyUFunc_RegisterLoopForType(ufunc, int,
891
PyUFuncGenericFunction, int *, void *)
892
int PyUFunc_GenericFunction \
893
(ufunc, PyObject *, PyObject *, PyArrayObject **)
894
void PyUFunc_f_f_As_d_d \
895
(char **, npy_intp *, npy_intp *, void *)
897
(char **, npy_intp *, npy_intp *, void *)
899
(char **, npy_intp *, npy_intp *, void *)
901
(char **, npy_intp *, npy_intp *, void *)
902
void PyUFunc_F_F_As_D_D \
903
(char **, npy_intp *, npy_intp *, void *)
905
(char **, npy_intp *, npy_intp *, void *)
907
(char **, npy_intp *, npy_intp *, void *)
909
(char **, npy_intp *, npy_intp *, void *)
911
(char **, npy_intp *, npy_intp *, void *)
912
void PyUFunc_ff_f_As_dd_d \
913
(char **, npy_intp *, npy_intp *, void *)
915
(char **, npy_intp *, npy_intp *, void *)
917
(char **, npy_intp *, npy_intp *, void *)
919
(char **, npy_intp *, npy_intp *, void *)
920
void PyUFunc_FF_F_As_DD_D \
921
(char **, npy_intp *, npy_intp *, void *)
923
(char **, npy_intp *, npy_intp *, void *)
925
(char **, npy_intp *, npy_intp *, void *)
927
(char **, npy_intp *, npy_intp *, void *)
929
(char **, npy_intp *, npy_intp *, void *)
930
void PyUFunc_O_O_method \
931
(char **, npy_intp *, npy_intp *, void *)
932
void PyUFunc_OO_O_method \
933
(char **, npy_intp *, npy_intp *, void *)
935
(char **, npy_intp *, npy_intp *, void *)
936
int PyUFunc_GetPyValues \
937
(char *, int *, int *, PyObject **)
938
int PyUFunc_checkfperr \
939
(int, PyObject *, int *)
940
void PyUFunc_clearfperr()
941
int PyUFunc_getfperr()
942
int PyUFunc_handlefperr \
943
(int, PyObject *, int, int *)
944
int PyUFunc_ReplaceLoopBySignature \
945
(ufunc, PyUFuncGenericFunction, int *, PyUFuncGenericFunction *)
946
object PyUFunc_FromFuncAndDataAndSignature \
947
(PyUFuncGenericFunction *, void **, char *, int, int, int,
948
int, char *, char *, int, char *)
953
cdef inline void set_array_base(ndarray arr, object base):
954
cdef PyObject* baseptr
958
Py_INCREF(base) # important to do this before decref below!
959
baseptr = <PyObject*>base
963
cdef inline object get_array_base(ndarray arr):
967
return <object>arr.base