-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathnumpy.py
More file actions
181 lines (168 loc) · 4.73 KB
/
numpy.py
File metadata and controls
181 lines (168 loc) · 4.73 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
"""Create UDFs of numpy functions supported by numba.
See list of numpy ufuncs supported by numpy here:
https://numba.readthedocs.io/en/stable/reference/numpysupported.html#math-operations
"""
import numpy as _np
from .. import _STANDARD_OPERATOR_NAMES
from .. import binary as _binary
from .. import config as _config
_delayed = {}
_binary_names = {
# Math operations
"add",
"subtract",
"multiply",
"divide",
"logaddexp",
"logaddexp2",
"true_divide",
"floor_divide",
"power",
"float_power",
"remainder",
"mod",
"fmod",
"gcd",
"lcm",
# Trigonometric functions
"arctan2",
"hypot",
# Bit-twiddling functions
"bitwise_and",
"bitwise_or",
"bitwise_xor",
"left_shift",
"right_shift",
# Comparison functions
"greater",
"greater_equal",
"less",
"less_equal",
"not_equal",
"equal",
"logical_and",
"logical_or",
"logical_xor",
"maximum",
"minimum",
"fmax",
"fmin",
# Floating functions
"copysign",
"nextafter",
"ldexp",
# Misc.
# "around", # <non-bool>_<int> -> <non-bool>
}
_STANDARD_OPERATOR_NAMES.update(f"binary.numpy.{name}" for name in _binary_names)
__all__ = list(_binary_names)
_numpy_to_graphblas = {
# Monoids
"add": "plus",
"bitwise_and": "band",
"bitwise_or": "bor",
"bitwise_xor": "bxor",
"equal": "eq",
"fmax": "max", # ignores nan
"fmin": "min", # ignores nan
"logical_and": "land",
"logical_or": "lor",
"logical_xor": "lxor",
"multiply": "times",
# Other
"arctan2": "atan2",
"copysign": "copysign",
"divide": "truediv",
# "floor_divide": "floordiv", # floor_divide does not cast to int, but floordiv does
# "fmod": "fmod", # not the same!
"greater": "gt",
"greater_equal": "ge",
"ldexp": "ldexp",
"less": "lt",
"less_equal": "le",
# "mod": "remainder", # not the same!
"not_equal": "ne",
"power": "pow",
"float_power": None, # uses pow with everything coerced to float64 (constructed below)
# "remainder": "remainder", # not the same!
"subtract": "minus",
"true_divide": "truediv",
}
# _graphblas_to_numpy = {val: key for key, val in _numpy_to_graphblas.items()} # Soon...
# Not included: maximum, minimum, gcd, hypot, logaddexp, logaddexp2
# lcm, left_shift, nextafter, right_shift
_commutative = {
"add",
"bitwise_and",
"bitwise_or",
"bitwise_xor",
"equal",
"fmax",
"fmin",
"gcd",
"hypot",
"lcm",
"logaddexp",
"logaddexp2",
"logical_and",
"logical_or",
"logical_xor",
"maximum",
"minimum",
"multiply",
"not_equal",
}
_commutes_to = {
"greater": "less",
"greater_equal": "less_equal",
"less": "greater",
"less_equal": "greater_equal",
}
# Don't commute: arctan2, around, copysign, divide, floor_divide, fmod, ldexp,
# left_shift, mod, nextafter, power, remainder, right_shift, subtract, true_divide.
# If desired, we could create r-versions of these so they can commute to something.
def __dir__():
return globals().keys() | _delayed.keys() | _binary_names
def __getattr__(name):
if name in _delayed:
delayed_func, kwargs = _delayed.pop(name)
rv = delayed_func(**kwargs)
globals()[name] = rv
return rv
if name not in _binary_names:
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
if _config.get("mapnumpy") and name in _numpy_to_graphblas:
if name == "float_power":
from ..core.operator import binary
from ..dtypes import FP64
new_op = binary.BinaryOp(f"numpy.{name}")
builtin_op = _binary.pow
for dtype in builtin_op.types:
if dtype.name in {"FP32", "FC32", "FC64"}:
orig_dtype = dtype
else:
orig_dtype = FP64
orig_op = builtin_op[orig_dtype]
cur_op = binary.TypedBuiltinBinaryOp(
new_op,
new_op.name,
dtype,
builtin_op.types[orig_dtype],
orig_op.gb_obj,
orig_op.gb_name,
)
new_op._add(cur_op)
globals()[name] = new_op
else:
globals()[name] = getattr(_binary, _numpy_to_graphblas[name])
else:
numpy_func = getattr(_np, name)
def func(x, y): # pragma: no cover (numba)
return numpy_func(x, y)
_binary.register_new(f"numpy.{name}", func)
rv = globals()[name]
if name in _commutative:
rv._commutes_to = rv
elif name in _commutes_to and rv._commutes_to is None:
rv._commutes_to = f"numpy.{_commutes_to[name]}"
return rv