1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
|
"""
Template for each `dtype` helper function for sparse ops
WARNING: DO NOT edit .pxi FILE directly, .pxi is generated from .pxi.in
"""
# ----------------------------------------------------------------------
# Sparse op
# ----------------------------------------------------------------------
ctypedef fused sparse_t:
float64_t
int64_t
cdef float64_t __div__(sparse_t a, sparse_t b):
if b == 0:
if a > 0:
return INF
elif a < 0:
return -INF
else:
return NaN
else:
return float(a) / b
cdef float64_t __truediv__(sparse_t a, sparse_t b):
return __div__(a, b)
cdef sparse_t __mod__(sparse_t a, sparse_t b):
if b == 0:
if sparse_t is float64_t:
return NaN
else:
return 0
else:
return a % b
cdef sparse_t __floordiv__(sparse_t a, sparse_t b):
if b == 0:
if sparse_t is float64_t:
# Match non-sparse Series behavior implemented in mask_zero_div_zero
if a > 0:
return INF
elif a < 0:
return -INF
return NaN
else:
return 0
else:
return a // b
# ----------------------------------------------------------------------
# sparse array op
# ----------------------------------------------------------------------
{{py:
# dtype, arith_comp_group, logical_group
dtypes = [('float64', True, False),
('int64', True, True),
('uint8', False, True)]
# do not generate arithmetic / comparison template for uint8,
# it should be done in fused types
def get_op(tup):
assert isinstance(tup, tuple)
assert len(tup) == 4
opname, lval, rval, dtype = tup
ops_dict = {'add': '{0} + {1}',
'sub': '{0} - {1}',
'mul': '{0} * {1}',
'div': '__div__({0}, {1})',
'mod': '__mod__({0}, {1})',
'truediv': '__truediv__({0}, {1})',
'floordiv': '__floordiv__({0}, {1})',
'pow': '{0} ** {1}',
'eq': '{0} == {1}',
'ne': '{0} != {1}',
'lt': '{0} < {1}',
'gt': '{0} > {1}',
'le': '{0} <= {1}',
'ge': '{0} >= {1}',
'and': '{0} & {1}', # logical op
'or': '{0} | {1}',
'xor': '{0} ^ {1}'}
return ops_dict[opname].format(lval, rval)
def get_dispatch(dtypes):
ops_list = ['add', 'sub', 'mul', 'div', 'mod', 'truediv',
'floordiv', 'pow',
'eq', 'ne', 'lt', 'gt', 'le', 'ge',
'and', 'or', 'xor']
for opname in ops_list:
for dtype, arith_comp_group, logical_group in dtypes:
if opname in ('div', 'truediv'):
rdtype = 'float64'
elif opname in ('eq', 'ne', 'lt', 'gt', 'le', 'ge'):
# comparison op
rdtype = 'uint8'
elif opname in ('and', 'or', 'xor'):
# logical op
rdtype = 'uint8'
else:
rdtype = dtype
if opname in ('and', 'or', 'xor'):
if logical_group:
yield opname, dtype, rdtype
else:
if arith_comp_group:
yield opname, dtype, rdtype
}}
{{for opname, dtype, rdtype in get_dispatch(dtypes)}}
{{if opname == "pow"}}
@cython.cpow(True) # Cython 3 matches Python pow, which isn't what we want here
{{endif}}
@cython.wraparound(False)
@cython.boundscheck(False)
cdef tuple block_op_{{opname}}_{{dtype}}({{dtype}}_t[:] x_,
BlockIndex xindex,
{{dtype}}_t xfill,
{{dtype}}_t[:] y_,
BlockIndex yindex,
{{dtype}}_t yfill):
"""
Binary operator on BlockIndex objects with fill values
"""
cdef:
BlockIndex out_index
Py_ssize_t xi = 0, yi = 0, out_i = 0 # fp buf indices
int32_t xbp = 0, ybp = 0 # block positions
int32_t xloc, yloc
Py_ssize_t xblock = 0, yblock = 0 # block numbers
{{dtype}}_t[:] x, y
ndarray[{{rdtype}}_t, ndim=1] out
# to suppress Cython warning
x = x_
y = y_
out_index = xindex.make_union(yindex)
out = np.empty(out_index.npoints, dtype=np.{{rdtype}})
# Wow, what a hack job. Need to do something about this
# walk the two SparseVectors, adding matched locations...
for out_i in range(out_index.npoints):
if yblock == yindex.nblocks:
# use y fill value
out[out_i] = {{(opname, 'x[xi]', 'yfill', dtype) | get_op}}
xi += 1
# advance x location
xbp += 1
if xbp == xindex.lenbuf[xblock]:
xblock += 1
xbp = 0
continue
if xblock == xindex.nblocks:
# use x fill value
out[out_i] = {{(opname, 'xfill', 'y[yi]', dtype) | get_op}}
yi += 1
# advance y location
ybp += 1
if ybp == yindex.lenbuf[yblock]:
yblock += 1
ybp = 0
continue
yloc = yindex.locbuf[yblock] + ybp
xloc = xindex.locbuf[xblock] + xbp
# each index in the out_index had to come from either x, y, or both
if xloc == yloc:
out[out_i] = {{(opname, 'x[xi]', 'y[yi]', dtype) | get_op}}
xi += 1
yi += 1
# advance both locations
xbp += 1
if xbp == xindex.lenbuf[xblock]:
xblock += 1
xbp = 0
ybp += 1
if ybp == yindex.lenbuf[yblock]:
yblock += 1
ybp = 0
elif xloc < yloc:
# use y fill value
out[out_i] = {{(opname, 'x[xi]', 'yfill', dtype) | get_op}}
xi += 1
# advance x location
xbp += 1
if xbp == xindex.lenbuf[xblock]:
xblock += 1
xbp = 0
else:
# use x fill value
out[out_i] = {{(opname, 'xfill', 'y[yi]', dtype) | get_op}}
yi += 1
# advance y location
ybp += 1
if ybp == yindex.lenbuf[yblock]:
yblock += 1
ybp = 0
return out, out_index, {{(opname, 'xfill', 'yfill', dtype) | get_op}}
{{if opname == "pow"}}
@cython.cpow(True) # Cython 3 matches Python pow, which isn't what we want here
{{endif}}
@cython.wraparound(False)
@cython.boundscheck(False)
cdef tuple int_op_{{opname}}_{{dtype}}({{dtype}}_t[:] x_,
IntIndex xindex,
{{dtype}}_t xfill,
{{dtype}}_t[:] y_,
IntIndex yindex,
{{dtype}}_t yfill):
cdef:
IntIndex out_index
Py_ssize_t xi = 0, yi = 0, out_i = 0 # fp buf indices
int32_t xloc, yloc
int32_t[:] xindices, yindices, out_indices
{{dtype}}_t[:] x, y
ndarray[{{rdtype}}_t, ndim=1] out
# suppress Cython compiler warnings due to inlining
x = x_
y = y_
# need to do this first to know size of result array
out_index = xindex.make_union(yindex)
out = np.empty(out_index.npoints, dtype=np.{{rdtype}})
xindices = xindex.indices
yindices = yindex.indices
out_indices = out_index.indices
# walk the two SparseVectors, adding matched locations...
for out_i in range(out_index.npoints):
if xi == xindex.npoints:
# use x fill value
out[out_i] = {{(opname, 'xfill', 'y[yi]', dtype) | get_op}}
yi += 1
continue
if yi == yindex.npoints:
# use y fill value
out[out_i] = {{(opname, 'x[xi]', 'yfill', dtype) | get_op}}
xi += 1
continue
xloc = xindices[xi]
yloc = yindices[yi]
# each index in the out_index had to come from either x, y, or both
if xloc == yloc:
out[out_i] = {{(opname, 'x[xi]', 'y[yi]', dtype) | get_op}}
xi += 1
yi += 1
elif xloc < yloc:
# use y fill value
out[out_i] = {{(opname, 'x[xi]', 'yfill', dtype) | get_op}}
xi += 1
else:
# use x fill value
out[out_i] = {{(opname, 'xfill', 'y[yi]', dtype) | get_op}}
yi += 1
return out, out_index, {{(opname, 'xfill', 'yfill', dtype) | get_op}}
cpdef sparse_{{opname}}_{{dtype}}({{dtype}}_t[:] x,
SparseIndex xindex, {{dtype}}_t xfill,
{{dtype}}_t[:] y,
SparseIndex yindex, {{dtype}}_t yfill):
if isinstance(xindex, BlockIndex):
return block_op_{{opname}}_{{dtype}}(x, xindex.to_block_index(), xfill,
y, yindex.to_block_index(), yfill)
elif isinstance(xindex, IntIndex):
return int_op_{{opname}}_{{dtype}}(x, xindex.to_int_index(), xfill,
y, yindex.to_int_index(), yfill)
else:
raise NotImplementedError
{{endfor}}
|