@@ -16,13 +16,27 @@ from cpython.buffer cimport PyObject_GetBuffer
16
16
from cpython.type cimport type
17
17
cimport libc.stdio as stdio
18
18
19
+
20
+ cdef extern from * :
21
+ # Leave a marker that the NumPy declarations came from NumPy itself and not from Cython.
22
+ # See https://github.com/cython/cython/issues/3573
23
+ """
24
+ /* Using NumPy API declarations from "numpy/__init__.pxd" */
25
+ """
26
+
27
+
19
28
cdef extern from " Python.h" :
20
29
ctypedef int Py_intptr_t
21
30
bint PyObject_TypeCheck(object obj, PyTypeObject* type )
22
31
23
32
cdef extern from " numpy/arrayobject.h" :
24
- ctypedef Py_intptr_t npy_intp
25
- ctypedef size_t npy_uintp
33
+ # It would be nice to use size_t and ssize_t, but ssize_t has special
34
+ # implicit conversion rules, so just use "long".
35
+ # Note: The actual type only matters for Cython promotion, so long
36
+ # is closer than int, but could lead to incorrect promotion.
37
+ # (Not to worrying, and always the status-quo.)
38
+ ctypedef signed long npy_intp
39
+ ctypedef unsigned long npy_uintp
26
40
27
41
ctypedef unsigned char npy_bool
28
42
@@ -63,36 +77,28 @@ cdef extern from "numpy/arrayobject.h":
63
77
ctypedef long double npy_float128
64
78
65
79
ctypedef struct npy_cfloat:
66
- float real
67
- float imag
80
+ pass
68
81
69
82
ctypedef struct npy_cdouble:
70
- double real
71
- double imag
83
+ pass
72
84
73
85
ctypedef struct npy_clongdouble:
74
- long double real
75
- long double imag
86
+ pass
76
87
77
88
ctypedef struct npy_complex64:
78
- float real
79
- float imag
89
+ pass
80
90
81
91
ctypedef struct npy_complex128:
82
- double real
83
- double imag
92
+ pass
84
93
85
94
ctypedef struct npy_complex160:
86
- long double real
87
- long double imag
95
+ pass
88
96
89
97
ctypedef struct npy_complex192:
90
- long double real
91
- long double imag
98
+ pass
92
99
93
100
ctypedef struct npy_complex256:
94
- long double real
95
- long double imag
101
+ pass
96
102
97
103
ctypedef struct PyArray_Dims:
98
104
npy_intp * ptr
@@ -154,7 +160,7 @@ cdef extern from "numpy/arrayobject.h":
154
160
NPY_COMPLEX512
155
161
156
162
NPY_INTP
157
- NPY_DEFAULT_INT
163
+ NPY_DEFAULT_INT # Not a compile time constant (normally)!
158
164
159
165
ctypedef enum NPY_ORDER:
160
166
NPY_ANYORDER
@@ -350,7 +356,10 @@ cdef extern from "numpy/arrayobject.h":
350
356
351
357
PyObject * PyArray_BASE(ndarray) nogil # returns borrowed reference!
352
358
PyArray_Descr * PyArray_DESCR(ndarray) nogil # returns borrowed reference to dtype!
359
+ PyArray_Descr * PyArray_DTYPE(ndarray) nogil # returns borrowed reference to dtype! NP 1.7+ alias for descr.
353
360
int PyArray_FLAGS(ndarray) nogil
361
+ void PyArray_CLEARFLAGS(ndarray, int flags) nogil # Added in NumPy 1.7
362
+ void PyArray_ENABLEFLAGS(ndarray, int flags) nogil # Added in NumPy 1.7
354
363
npy_intp PyArray_ITEMSIZE(ndarray) nogil
355
364
int PyArray_TYPE(ndarray arr) nogil
356
365
@@ -371,7 +380,6 @@ cdef extern from "numpy/arrayobject.h":
371
380
bint PyTypeNum_ISOBJECT(int ) nogil
372
381
373
382
npy_intp PyDataType_ELSIZE(dtype) nogil
374
- void PyDataType_SET_ELSIZE(dtype, npy_intp) nogil
375
383
npy_intp PyDataType_ALIGNMENT(dtype) nogil
376
384
PyObject* PyDataType_METADATA(dtype) nogil
377
385
PyArray_ArrayDescr* PyDataType_SUBARRAY(dtype) nogil
@@ -501,6 +509,12 @@ cdef extern from "numpy/arrayobject.h":
501
509
void * PyArray_MultiIter_DATA(broadcast multi, npy_intp i) nogil
502
510
void PyArray_MultiIter_NEXTi(broadcast multi, npy_intp i) nogil
503
511
bint PyArray_MultiIter_NOTDONE(broadcast multi) nogil
512
+ npy_intp PyArray_MultiIter_SIZE(broadcast multi) nogil
513
+ int PyArray_MultiIter_NDIM(broadcast multi) nogil
514
+ npy_intp PyArray_MultiIter_INDEX(broadcast multi) nogil
515
+ int PyArray_MultiIter_NUMITER(broadcast multi) nogil
516
+ npy_intp* PyArray_MultiIter_DIMS(broadcast multi) nogil
517
+ void ** PyArray_MultiIter_ITERS(broadcast multi) nogil
504
518
505
519
# Functions from __multiarray_api.h
506
520
@@ -700,11 +714,10 @@ ctypedef npy_double float_t
700
714
ctypedef npy_double double_t
701
715
ctypedef npy_longdouble longdouble_t
702
716
703
- ctypedef npy_cfloat cfloat_t
704
- ctypedef npy_cdouble cdouble_t
705
- ctypedef npy_clongdouble clongdouble_t
706
-
707
- ctypedef npy_cdouble complex_t
717
+ ctypedef float complex cfloat_t
718
+ ctypedef double complex cdouble_t
719
+ ctypedef double complex complex_t
720
+ ctypedef long double complex clongdouble_t
708
721
709
722
cdef inline object PyArray_MultiIterNew1(a):
710
723
return PyArray_MultiIterNew(1 , < void * > a)
@@ -939,13 +952,6 @@ cdef inline int import_ufunc() except -1:
939
952
except Exception :
940
953
raise ImportError (" numpy._core.umath failed to import" )
941
954
942
- cdef extern from * :
943
- # Leave a marker that the NumPy declarations came from this file
944
- # See https://github.com/cython/cython/issues/3573
945
- """
946
- /* NumPy API declarations from "numpy/__init__.pxd" */
947
- """
948
-
949
955
950
956
cdef inline bint is_timedelta64_object(object obj):
951
957
"""
@@ -999,3 +1005,137 @@ cdef inline NPY_DATETIMEUNIT get_datetime64_unit(object obj) nogil:
999
1005
returns the unit part of the dtype for a numpy datetime64 object.
1000
1006
"""
1001
1007
return < NPY_DATETIMEUNIT> (< PyDatetimeScalarObject* > obj).obmeta.base
1008
+
1009
+
1010
+ # Iterator API added in v1.6
1011
+ ctypedef int (* NpyIter_IterNextFunc)(NpyIter* it) noexcept nogil
1012
+ ctypedef void (* NpyIter_GetMultiIndexFunc)(NpyIter* it, npy_intp* outcoords) noexcept nogil
1013
+
1014
+ cdef extern from " numpy/arrayobject.h" :
1015
+
1016
+ ctypedef struct NpyIter:
1017
+ pass
1018
+
1019
+ cdef enum :
1020
+ NPY_FAIL
1021
+ NPY_SUCCEED
1022
+
1023
+ cdef enum :
1024
+ # Track an index representing C order
1025
+ NPY_ITER_C_INDEX
1026
+ # Track an index representing Fortran order
1027
+ NPY_ITER_F_INDEX
1028
+ # Track a multi-index
1029
+ NPY_ITER_MULTI_INDEX
1030
+ # User code external to the iterator does the 1-dimensional innermost loop
1031
+ NPY_ITER_EXTERNAL_LOOP
1032
+ # Convert all the operands to a common data type
1033
+ NPY_ITER_COMMON_DTYPE
1034
+ # Operands may hold references, requiring API access during iteration
1035
+ NPY_ITER_REFS_OK
1036
+ # Zero-sized operands should be permitted, iteration checks IterSize for 0
1037
+ NPY_ITER_ZEROSIZE_OK
1038
+ # Permits reductions (size-0 stride with dimension size > 1)
1039
+ NPY_ITER_REDUCE_OK
1040
+ # Enables sub-range iteration
1041
+ NPY_ITER_RANGED
1042
+ # Enables buffering
1043
+ NPY_ITER_BUFFERED
1044
+ # When buffering is enabled, grows the inner loop if possible
1045
+ NPY_ITER_GROWINNER
1046
+ # Delay allocation of buffers until first Reset* call
1047
+ NPY_ITER_DELAY_BUFALLOC
1048
+ # When NPY_KEEPORDER is specified, disable reversing negative-stride axes
1049
+ NPY_ITER_DONT_NEGATE_STRIDES
1050
+ NPY_ITER_COPY_IF_OVERLAP
1051
+ # The operand will be read from and written to
1052
+ NPY_ITER_READWRITE
1053
+ # The operand will only be read from
1054
+ NPY_ITER_READONLY
1055
+ # The operand will only be written to
1056
+ NPY_ITER_WRITEONLY
1057
+ # The operand's data must be in native byte order
1058
+ NPY_ITER_NBO
1059
+ # The operand's data must be aligned
1060
+ NPY_ITER_ALIGNED
1061
+ # The operand's data must be contiguous (within the inner loop)
1062
+ NPY_ITER_CONTIG
1063
+ # The operand may be copied to satisfy requirements
1064
+ NPY_ITER_COPY
1065
+ # The operand may be copied with WRITEBACKIFCOPY to satisfy requirements
1066
+ NPY_ITER_UPDATEIFCOPY
1067
+ # Allocate the operand if it is NULL
1068
+ NPY_ITER_ALLOCATE
1069
+ # If an operand is allocated, don't use any subtype
1070
+ NPY_ITER_NO_SUBTYPE
1071
+ # This is a virtual array slot, operand is NULL but temporary data is there
1072
+ NPY_ITER_VIRTUAL
1073
+ # Require that the dimension match the iterator dimensions exactly
1074
+ NPY_ITER_NO_BROADCAST
1075
+ # A mask is being used on this array, affects buffer -> array copy
1076
+ NPY_ITER_WRITEMASKED
1077
+ # This array is the mask for all WRITEMASKED operands
1078
+ NPY_ITER_ARRAYMASK
1079
+ # Assume iterator order data access for COPY_IF_OVERLAP
1080
+ NPY_ITER_OVERLAP_ASSUME_ELEMENTWISE
1081
+
1082
+ # construction and destruction functions
1083
+ NpyIter* NpyIter_New(ndarray arr, npy_uint32 flags, NPY_ORDER order,
1084
+ NPY_CASTING casting, dtype datatype) except NULL
1085
+ NpyIter* NpyIter_MultiNew(npy_intp nop, PyArrayObject** op, npy_uint32 flags,
1086
+ NPY_ORDER order, NPY_CASTING casting, npy_uint32*
1087
+ op_flags, PyArray_Descr** op_dtypes) except NULL
1088
+ NpyIter* NpyIter_AdvancedNew(npy_intp nop, PyArrayObject** op,
1089
+ npy_uint32 flags, NPY_ORDER order,
1090
+ NPY_CASTING casting, npy_uint32* op_flags,
1091
+ PyArray_Descr** op_dtypes, int oa_ndim,
1092
+ int ** op_axes, const npy_intp* itershape,
1093
+ npy_intp buffersize) except NULL
1094
+ NpyIter* NpyIter_Copy(NpyIter* it) except NULL
1095
+ int NpyIter_RemoveAxis(NpyIter* it, int axis) except NPY_FAIL
1096
+ int NpyIter_RemoveMultiIndex(NpyIter* it) except NPY_FAIL
1097
+ int NpyIter_EnableExternalLoop(NpyIter* it) except NPY_FAIL
1098
+ int NpyIter_Deallocate(NpyIter* it) except NPY_FAIL
1099
+ int NpyIter_Reset(NpyIter* it, char ** errmsg) except NPY_FAIL
1100
+ int NpyIter_ResetToIterIndexRange(NpyIter* it, npy_intp istart,
1101
+ npy_intp iend, char ** errmsg) except NPY_FAIL
1102
+ int NpyIter_ResetBasePointers(NpyIter* it, char ** baseptrs, char ** errmsg) except NPY_FAIL
1103
+ int NpyIter_GotoMultiIndex(NpyIter* it, const npy_intp* multi_index) except NPY_FAIL
1104
+ int NpyIter_GotoIndex(NpyIter* it, npy_intp index) except NPY_FAIL
1105
+ npy_intp NpyIter_GetIterSize(NpyIter* it) nogil
1106
+ npy_intp NpyIter_GetIterIndex(NpyIter* it) nogil
1107
+ void NpyIter_GetIterIndexRange(NpyIter* it, npy_intp* istart,
1108
+ npy_intp* iend) nogil
1109
+ int NpyIter_GotoIterIndex(NpyIter* it, npy_intp iterindex) except NPY_FAIL
1110
+ npy_bool NpyIter_HasDelayedBufAlloc(NpyIter* it) nogil
1111
+ npy_bool NpyIter_HasExternalLoop(NpyIter* it) nogil
1112
+ npy_bool NpyIter_HasMultiIndex(NpyIter* it) nogil
1113
+ npy_bool NpyIter_HasIndex(NpyIter* it) nogil
1114
+ npy_bool NpyIter_RequiresBuffering(NpyIter* it) nogil
1115
+ npy_bool NpyIter_IsBuffered(NpyIter* it) nogil
1116
+ npy_bool NpyIter_IsGrowInner(NpyIter* it) nogil
1117
+ npy_intp NpyIter_GetBufferSize(NpyIter* it) nogil
1118
+ int NpyIter_GetNDim(NpyIter* it) nogil
1119
+ int NpyIter_GetNOp(NpyIter* it) nogil
1120
+ npy_intp* NpyIter_GetAxisStrideArray(NpyIter* it, int axis) except NULL
1121
+ int NpyIter_GetShape(NpyIter* it, npy_intp* outshape) nogil
1122
+ PyArray_Descr** NpyIter_GetDescrArray(NpyIter* it)
1123
+ PyArrayObject** NpyIter_GetOperandArray(NpyIter* it)
1124
+ ndarray NpyIter_GetIterView(NpyIter* it, npy_intp i)
1125
+ void NpyIter_GetReadFlags(NpyIter* it, char * outreadflags)
1126
+ void NpyIter_GetWriteFlags(NpyIter* it, char * outwriteflags)
1127
+ int NpyIter_CreateCompatibleStrides(NpyIter* it, npy_intp itemsize,
1128
+ npy_intp* outstrides) except NPY_FAIL
1129
+ npy_bool NpyIter_IsFirstVisit(NpyIter* it, int iop) nogil
1130
+ # functions for iterating an NpyIter object
1131
+ NpyIter_IterNextFunc* NpyIter_GetIterNext(NpyIter* it, char ** errmsg) except NULL
1132
+ NpyIter_GetMultiIndexFunc* NpyIter_GetGetMultiIndex(NpyIter* it,
1133
+ char ** errmsg) except NULL
1134
+ char ** NpyIter_GetDataPtrArray(NpyIter* it) nogil
1135
+ char ** NpyIter_GetInitialDataPtrArray(NpyIter* it) nogil
1136
+ npy_intp* NpyIter_GetIndexPtr(NpyIter* it)
1137
+ npy_intp* NpyIter_GetInnerStrideArray(NpyIter* it) nogil
1138
+ npy_intp* NpyIter_GetInnerLoopSizePtr(NpyIter* it) nogil
1139
+ void NpyIter_GetInnerFixedStrideArray(NpyIter* it, npy_intp* outstrides) nogil
1140
+ npy_bool NpyIter_IterationNeedsAPI(NpyIter* it) nogil
1141
+ void NpyIter_DebugPrint(NpyIter* it)
0 commit comments