-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlazy_expr.py
More file actions
235 lines (207 loc) · 8.47 KB
/
Copy pathlazy_expr.py
File metadata and controls
235 lines (207 loc) · 8.47 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
###########################################################################################
# Copyright INAOS GmbH, Thalwil, 2018.
# Copyright Francesc Alted, 2018.
#
# All rights reserved.
#
# This software is the confidential and proprietary information of INAOS GmbH
# and Francesc Alted ("Confidential Information"). You shall not disclose such Confidential
# Information and shall use it only in accordance with the terms of the license agreement.
###########################################################################################
import iarray as ia
def fuse_operands(operands1, operands2):
new_operands = {}
dup_operands = {}
new_pos = len(operands1)
for k2, v2 in operands2.items():
try:
k1 = list(operands1.keys())[list(operands1.values()).index(v2)]
# The operand is duplicated; keep track of it
dup_operands[k2] = k1
except ValueError:
# The value is not among operands1, so rebase it
new_op = f"o{new_pos}"
new_pos += 1
new_operands[new_op] = operands2[k2]
return new_operands, dup_operands
def fuse_expressions(expr, new_base, dup_op):
new_expr = ""
skip_to_char = 0
old_base = 0
prev_pos = {}
for i in range(len(expr)):
if i < skip_to_char:
continue
if expr[i] == "o":
if i > 0 and (expr[i - 1] != " " and expr[i - 1] != "("):
# Not a variable
new_expr += expr[i]
continue
# This is a variable. Find the end of it.
j = i + 1
for k in range(len(expr[j :])):
if expr[j + k] == " ":
j = k
break
if expr[j + k] == ")":
j = k
break
if expr[i + j] == ")":
j -= 1
old_pos = int(expr[i + 1 : i + j + 1])
old_op = f"o{old_pos}"
if old_op not in dup_op:
if old_pos in prev_pos:
# Keep track of duplicated old positions inside expr
new_pos = prev_pos[old_pos]
else:
new_pos = old_base + new_base
old_base += 1
new_expr += f"o{new_pos}"
prev_pos[old_pos] = new_pos
else:
new_expr += dup_op[old_op]
skip_to_char = i + j + 1
else:
new_expr += expr[i]
return new_expr
class LazyExpr:
"""Class for hosting lazy expressions.
This is not meant to be called directly from user space.
Once the lazy expression is created, it can be evaluated via :func:`LazyExpr.eval`.
"""
def __init__(self, new_op):
value1, op, value2 = new_op
if value2 is None:
# ufunc
if isinstance(value1, LazyExpr):
self.expression = f"{op}({self.expression})"
else:
self.operands = {"o0": value1}
self.expression = f"{op}(o0)"
return
elif op in ("atan2", "pow"):
self.operands = {"o0": value1, "o1": value2}
self.expression = f"{op}(o0, o1)"
return
if isinstance(value1, (int, float)) and isinstance(value2, (int, float)):
self.expression = f"({value1} {op} {value2})"
elif isinstance(value2, (int, float)):
self.operands = {"o0": value1}
self.expression = f"(o0 {op} {value2})"
elif isinstance(value1, (int, float)):
self.operands = {"o0": value2}
self.expression = f"({value1} {op} o0)"