This repository was archived by the owner on Apr 1, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathnumeric.py
More file actions
376 lines (237 loc) · 6.83 KB
/
numeric.py
File metadata and controls
376 lines (237 loc) · 6.83 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
# Contains code from https://github.com/ibis-project/ibis/blob/9.2.0/ibis/expr/operations/numeric.py
"""Operations for numeric expressions."""
from __future__ import annotations
import operator
from typing import Optional
from bigframes_vendored.ibis import util
from bigframes_vendored.ibis.common.annotations import attribute
import bigframes_vendored.ibis.expr.datatypes as dt
from bigframes_vendored.ibis.expr.operations.core import Binary, Unary, Value
import bigframes_vendored.ibis.expr.rules as rlz
from public import public
Integer = Value[dt.Integer]
SoftNumeric = Value[dt.Numeric | dt.Boolean]
StrictNumeric = Value[dt.Numeric]
@public
class NumericBinary(Binary):
left: SoftNumeric
right: SoftNumeric
@public
class Add(NumericBinary):
"""Add two values."""
dtype = rlz.numeric_like("args", operator.add)
@public
class Multiply(NumericBinary):
"""Multiply two values."""
dtype = rlz.numeric_like("args", operator.mul)
@public
class Power(NumericBinary):
"""Raise the left value to the power of the right value."""
@property
def dtype(self):
dtypes = (arg.dtype for arg in self.args)
if util.all_of(dtypes, dt.Integer):
return dt.float64
else:
return rlz.highest_precedence_dtype(self.args)
@public
class Subtract(NumericBinary):
"""Subtract the right value from the left value."""
dtype = rlz.numeric_like("args", operator.sub)
@public
class Divide(NumericBinary):
"""Divide the left value by the right value."""
dtype = dt.float64
@public
class FloorDivide(Divide):
"""Divide the left value by the right value and round down to the nearest integer."""
dtype = dt.int64
@public
class Modulus(NumericBinary):
"""Return the remainder after the division of the left value by the right value."""
dtype = rlz.numeric_like("args", operator.mod)
@public
class Negate(Unary):
"""Negate the value."""
arg: Value[dt.Numeric | dt.Interval]
dtype = rlz.dtype_like("arg")
@public
class IsNan(Unary):
"""Check if the value is NaN."""
arg: Value[dt.Floating]
dtype = dt.boolean
@public
class IsInf(Unary):
"""Check if the value is infinite."""
arg: Value[dt.Floating]
dtype = dt.boolean
@public
class Abs(Unary):
"""Absolute value."""
arg: SoftNumeric
dtype = rlz.dtype_like("arg")
@public
class Ceil(Unary):
"""Round up to the nearest integer value greater than or equal to this value."""
arg: SoftNumeric
@property
def dtype(self):
if self.arg.dtype.is_decimal():
return self.arg.dtype
else:
return dt.int64
@public
class Floor(Unary):
"""Round down to the nearest integer value less than or equal to this value."""
arg: SoftNumeric
@property
def dtype(self):
if self.arg.dtype.is_decimal():
return self.arg.dtype
else:
return dt.int64
@public
class Round(Value):
"""Round a value."""
arg: StrictNumeric
# TODO(kszucs): the default should be 0 instead of being None
digits: Optional[Integer] = None
shape = rlz.shape_like("arg")
@property
def dtype(self):
if self.arg.dtype.is_decimal():
return self.arg.dtype
elif self.digits is None:
return dt.int64
else:
return dt.double
@public
class Clip(Value):
"""Clip a value to a specified range."""
arg: StrictNumeric
lower: Optional[StrictNumeric] = None
upper: Optional[StrictNumeric] = None
dtype = rlz.dtype_like("arg")
shape = rlz.shape_like("arg")
@public
class BaseConvert(Value):
"""Convert a number from one base to another."""
# TODO(kszucs): this should be Integer simply
arg: Value[dt.Integer | dt.String]
from_base: Integer
to_base: Integer
dtype = dt.string
shape = rlz.shape_like("args")
@public
class MathUnary(Unary):
"""Base class for unary math operations."""
arg: SoftNumeric
@attribute
def dtype(self):
return dt.higher_precedence(self.arg.dtype, dt.float64)
class ExpandingMathUnary(MathUnary):
@attribute
def dtype(self):
if self.arg.dtype.is_decimal():
return self.arg.dtype
else:
return dt.float64
@public
class Exp(ExpandingMathUnary):
"""Exponential function."""
@public
class Sign(Unary):
"""Sign of the value."""
arg: SoftNumeric
dtype = rlz.dtype_like("arg")
@public
class Sqrt(MathUnary):
"""Square root of the value."""
@public
class Logarithm(MathUnary):
"""Base class for logarithmic operations."""
arg: StrictNumeric
@public
class Log(Logarithm):
"""Logarithm with a specific base."""
base: Optional[StrictNumeric] = None
@public
class Ln(Logarithm):
"""Natural logarithm."""
@public
class Log2(Logarithm):
"""Logarithm base 2."""
@public
class Log10(Logarithm):
"""Logarithm base 10."""
@public
class Degrees(ExpandingMathUnary):
"""Converts radians to degrees."""
@public
class Radians(MathUnary):
"""Converts degrees to radians."""
@public
class TrigonometricUnary(MathUnary):
"""Trigonometric base unary."""
@public
class TrigonometricBinary(Binary):
"""Trigonometric base binary."""
left: SoftNumeric
right: SoftNumeric
dtype = dt.float64
@public
class Acos(TrigonometricUnary):
"""Returns the arc cosine of x."""
@public
class Asin(TrigonometricUnary):
"""Returns the arc sine of x."""
@public
class Atan(TrigonometricUnary):
"""Returns the arc tangent of x."""
@public
class Atan2(TrigonometricBinary):
"""Returns the arc tangent of x and y."""
@public
class Cos(TrigonometricUnary):
"""Returns the cosine of x."""
@public
class Cot(TrigonometricUnary):
"""Returns the cotangent of x."""
@public
class Sin(TrigonometricUnary):
"""Returns the sine of x."""
@public
class Tan(TrigonometricUnary):
"""Returns the tangent of x."""
@public
class BitwiseNot(Unary):
"""Bitwise NOT operation."""
arg: Value[dt.Integer | dt.Binary]
dtype = rlz.numeric_like("args", operator.invert)
@public
class BitwiseBinary(Binary):
"""Base class for bitwise binary operations."""
left: Integer
right: Integer
@public
class BitwiseAnd(BitwiseBinary):
"""Bitwise AND operation."""
dtype = rlz.numeric_like("args", operator.and_)
@public
class BitwiseOr(BitwiseBinary):
"""Bitwise OR operation."""
dtype = rlz.numeric_like("args", operator.or_)
@public
class BitwiseXor(BitwiseBinary):
"""Bitwise XOR operation."""
dtype = rlz.numeric_like("args", operator.xor)
@public
class BitwiseLeftShift(BitwiseBinary):
"""Bitwise left shift operation."""
shape = rlz.shape_like("args")
dtype = dt.int64
@public
class BitwiseRightShift(BitwiseBinary):
"""Bitwise right shift operation."""
shape = rlz.shape_like("args")
dtype = dt.int64