|
13 | 13 | from devito.symbolics.extended_sympy import DefFunction, rfunc |
14 | 14 | from devito.symbolics.queries import q_leaf |
15 | 15 | from devito.symbolics.search import retrieve_indexed, retrieve_functions |
16 | | -from devito.symbolics.unevaluation import Mul as UMul |
| 16 | +from devito.symbolics.unevaluation import ( |
| 17 | + Add as UnevalAdd, Mul as UnevalMul, Pow as UnevalPow |
| 18 | +) |
17 | 19 | from devito.tools import as_list, as_tuple, flatten, split, transitive_closure |
18 | 20 | from devito.types.basic import Basic, Indexed |
19 | 21 | from devito.types.array import ComponentAccess |
|
22 | 24 |
|
23 | 25 | __all__ = ['xreplace_indices', 'pow_to_mul', 'indexify', 'subs_op_args', |
24 | 26 | 'normalize_args', 'uxreplace', 'Uxmapper', 'subs_if_composite', |
25 | | - 'reuse_if_untouched', 'evalrel', 'flatten_args'] |
| 27 | + 'reuse_if_untouched', 'evalrel', 'flatten_args', 'unevaluate'] |
26 | 28 |
|
27 | 29 |
|
28 | 30 | def uxreplace(expr, rule): |
@@ -338,7 +340,7 @@ def pow_to_mul(expr): |
338 | 340 | # but at least we traverse the base looking for other Pows |
339 | 341 | return expr.func(pow_to_mul(base), exp, evaluate=False) |
340 | 342 | elif exp > 0: |
341 | | - return UMul(*[pow_to_mul(base)]*int(exp), evaluate=False) |
| 343 | + return UnevalMul(*[pow_to_mul(base)]*int(exp), evaluate=False) |
342 | 344 | elif exp < 0: |
343 | 345 | # Reciprocal powers become inverse of the negative power |
344 | 346 | # for example Pow(expr, -2) becomes Pow(expr * expr, -1) |
@@ -502,3 +504,18 @@ def evalrel(func=min, input=None, assumptions=None): |
502 | 504 | except TypeError: |
503 | 505 | pass |
504 | 506 | return rfunc(func, *input) |
| 507 | + |
| 508 | + |
| 509 | +uneval_mapper = {Add: UnevalAdd, Mul: UnevalMul, Pow: UnevalPow} |
| 510 | + |
| 511 | + |
| 512 | +def unevaluate(expr): |
| 513 | + if q_leaf(expr): |
| 514 | + return expr |
| 515 | + |
| 516 | + args = [unevaluate(a) for a in expr.args] |
| 517 | + |
| 518 | + try: |
| 519 | + return uneval_mapper[expr.func](*args) |
| 520 | + except KeyError: |
| 521 | + return reuse_if_untouched(expr, args) |
0 commit comments