forked from exaloop/codon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperators.codon
More file actions
292 lines (221 loc) · 7.43 KB
/
operators.codon
File metadata and controls
292 lines (221 loc) · 7.43 KB
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
# Copyright (C) 2022-2026 Exaloop Inc. <https://exaloop.io>
import operator
import util
from .ndarray import ndarray
from .routines import asarray
@tuple
class _OpWrap:
op: F
F: type
def __call__(self, a, b):
A = type(a)
B = type(b)
c1, c2 = util.op_types(A, B)
C1 = type(c1)
C2 = type(c2)
return self.op(util.cast(a, C1), util.cast(b, C2))
def _fix_scalar(x, A: type):
X = type(x)
a_is_int: Literal[bool] = (A is int or A is byte or isinstance(A, Int)
or isinstance(A, UInt))
x_is_int: Literal[bool] = X is int
a_is_float: Literal[bool] = (A is float or A is float32 or A is float16
or A is bfloat16 or A is float128)
x_is_float: Literal[bool] = X is float
a_is_complex: Literal[bool] = (A is complex or A is complex64)
x_is_complex: Literal[bool] = X is complex
should_cast: Literal[bool] = ((x_is_int and
(a_is_int or a_is_float or a_is_complex)) or
(x_is_float and (a_is_float or a_is_complex)) or
(x_is_complex and a_is_complex))
if (A is float16 or A is float32) and X is complex:
return util.cast(x, complex64)
elif should_cast:
return util.cast(x, A)
else:
return x
@inline
def _binop_impl(arr: ndarray, other, op):
op = _OpWrap(op)
if isinstance(other, ndarray):
return arr._op_elemwise(other, op)
if (isinstance(other, bool) or isinstance(other, int) or
isinstance(other, float) or isinstance(other, complex)):
return arr._op_scalar(_fix_scalar(other, arr.dtype), op)
return arr._op_elemwise(asarray(other), op)
@inline
def _ibinop_impl(arr: ndarray, other, op):
op = _OpWrap(op)
if isinstance(other, ndarray):
return arr._iop_elemwise(other, op)
if (isinstance(other, bool) or isinstance(other, int) or
isinstance(other, float) or isinstance(other, complex)):
return arr._iop_scalar(_fix_scalar(other, arr.dtype), op)
return arr._iop_elemwise(asarray(other), op)
@inline
def _rbinop_impl(arr: ndarray, other, op):
op = _OpWrap(op)
if isinstance(other, ndarray):
return arr._rop_elemwise(other, op)
if (isinstance(other, bool) or isinstance(other, int) or
isinstance(other, float) or isinstance(other, complex)):
return arr._rop_scalar(_fix_scalar(other, arr.dtype), op)
return arr._rop_elemwise(asarray(other), op)
def _floor_divide(x, y):
X = type(x)
Y = type(y)
if isinstance(X, Int) and isinstance(Y, Int):
return util.pydiv(x, y)
else:
return x // y
def _remainder(x, y):
X = type(x)
Y = type(y)
if isinstance(X, Int) and isinstance(Y, Int):
return util.pymod(x, y)
elif ((X is float and Y is float) or
(X is float32 and Y is float32) or
(X is float16 and Y is float16)):
return util.pyfmod(x, y)
else:
return x % y
@extend
class ndarray:
@inline
def __add__(self, other):
return _binop_impl(self, other, operator.add)
@inline
def __radd__(self, other):
return _rbinop_impl(self, other, operator.add)
@inline
def __iadd__(self, other):
return _ibinop_impl(self, other, operator.add)
@inline
def __sub__(self, other):
return _binop_impl(self, other, operator.sub)
@inline
def __rsub__(self, other):
return _rbinop_impl(self, other, operator.sub)
@inline
def __isub__(self, other):
return _ibinop_impl(self, other, operator.sub)
@inline
def __mul__(self, other):
return _binop_impl(self, other, operator.mul)
@inline
def __rmul__(self, other):
return _rbinop_impl(self, other, operator.mul)
@inline
def __imul__(self, other):
return _ibinop_impl(self, other, operator.mul)
@inline
def __mod__(self, other):
return _binop_impl(self, other, _remainder)
@inline
def __rmod__(self, other):
return _rbinop_impl(self, other, _remainder)
@inline
def __imod__(self, other):
return _ibinop_impl(self, other, _remainder)
@inline
def __pow__(self, other):
return _binop_impl(self, other, operator.pow)
@inline
def __rpow__(self, other):
return _rbinop_impl(self, other, operator.pow)
@inline
def __ipow__(self, other):
return _ibinop_impl(self, other, operator.pow)
@inline
def __truediv__(self, other):
return _binop_impl(self, other, operator.truediv)
@inline
def __rtruediv__(self, other):
return _rbinop_impl(self, other, operator.truediv)
@inline
def __itruediv__(self, other):
return _ibinop_impl(self, other, operator.truediv)
@inline
def __floordiv__(self, other):
return _binop_impl(self, other, _floor_divide)
@inline
def __rfloordiv__(self, other):
return _rbinop_impl(self, other, _floor_divide)
@inline
def __ifloordiv__(self, other):
return _ibinop_impl(self, other, _floor_divide)
@inline
def __lshift__(self, other):
return _binop_impl(self, other, operator.lshift)
@inline
def __rlshift__(self, other):
return _rbinop_impl(self, other, operator.lshift)
@inline
def __ilshift__(self, other):
return _ibinop_impl(self, other, operator.lshift)
@inline
def __rshift__(self, other):
return _binop_impl(self, other, operator.rshift)
@inline
def __rrshift__(self, other):
return _rbinop_impl(self, other, operator.rshift)
@inline
def __irshift__(self, other):
return _ibinop_impl(self, other, operator.rshift)
@inline
def __and__(self, other):
return _binop_impl(self, other, operator.and_)
@inline
def __rand__(self, other):
return _rbinop_impl(self, other, operator.and_)
@inline
def __iand__(self, other):
return _ibinop_impl(self, other, operator.and_)
@inline
def __or__(self, other):
return _binop_impl(self, other, operator.or_)
@inline
def __ror__(self, other):
return _rbinop_impl(self, other, operator.or_)
@inline
def __ior__(self, other):
return _ibinop_impl(self, other, operator.or_)
@inline
def __xor__(self, other):
return _binop_impl(self, other, operator.xor)
@inline
def __rxor__(self, other):
return _rbinop_impl(self, other, operator.xor)
@inline
def __ixor__(self, other):
return _ibinop_impl(self, other, operator.xor)
@inline
def __pos__(self):
return self._op_unary(operator.pos)
@inline
def __neg__(self):
return self._op_unary(operator.neg)
@inline
def __invert__(self):
return self._op_unary(operator.invert)
@inline
def __abs__(self):
return self._op_unary(operator.abs)
@inline
def __eq__(self, other):
return _binop_impl(self, other, operator.eq)
@inline
def __ne__(self, other):
return _binop_impl(self, other, operator.ne)
@inline
def __lt__(self, other):
return _binop_impl(self, other, operator.lt)
@inline
def __le__(self, other):
return _binop_impl(self, other, operator.le)
@inline
def __gt__(self, other):
return _binop_impl(self, other, operator.gt)
@inline
def __ge__(self, other):
return _binop_impl(self, other, operator.ge)