Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions devito/symbolics/manipulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from sympy import Pow, Add, Mul, Min, Max, S, SympifyError, Tuple, sympify
from sympy.core.add import _addsort
from sympy.core.mul import _mulsort
from sympy.functions.elementary.piecewise import ExprCondPair

from devito.finite_differences.differentiable import (
EvalDerivative, IndexDerivative
Expand Down Expand Up @@ -356,6 +357,8 @@ def pow_to_mul(expr):
else:
# Default. We should not end up here as all cases are handled
return expr
elif expr.func is ExprCondPair:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likely doesn't need special casing but rather only add TypeError to the except line 373 and gaurd the expr. func

        # Some SymPy versions will evaluate the two-args case
        # `(negative integer, mul)` despite the `evaluate=False`. For example,
        # `Mul(-2, 1/a*a, evaluate=False)` gets evaluated to `-2/a**2`. By swapping
        # the args, the issue disappears...
        try:
            a0, a1 = args
            if a0.is_Number and a0 < 0 and not q_leaf(a1):
                args = [a1, a0]
        except (ValueError, TypeError):
            pass

        try:
            return expr.func(*args, evaluate=False)
        except TypeError:
            return expr.func(*args)

Which would make it robust to most non-standard case rather than just the ExprCondPair one

return expr.func(*[pow_to_mul(i) for i in expr.args])
else:
args = [pow_to_mul(i) for i in expr.args]

Expand Down
11 changes: 10 additions & 1 deletion tests/test_symbolics.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from sympy import And, Expr, Number, Symbol
from devito import (Constant, Dimension, Grid, Function, solve, TimeFunction, Eq, # noqa
Operator, SubDimension, norm, Le, Ge, Gt, Lt, Abs, sin, cos,
Min, Max, Real, Imag, Conj, SubDomain, configuration)
Piecewise, Min, Max, Real, Imag, Conj, SubDomain, configuration)
from devito.finite_differences.differentiable import SafeInv, Weights, Mul
from devito.ir import Expression, FindNodes, ccode
from devito.ir.support.guards import GuardExpr, simplify_and
Expand Down Expand Up @@ -1104,6 +1104,15 @@ def test_print_div():
assert cstr == 'sizeof(int)/sizeof(long)'


def test_piecewise():
grid = Grid(shape=(11,))
u = Function(name='u', grid=grid, space_order=2)
v = Function(name='v', grid=grid, space_order=2)
eq_u = Eq(u, Piecewise((1, v < 10), (2, True)))
op = Operator(eq_u)
# check that the code generated a condition
assert "v[x + 2] < 10" in str(op.ccode)

def test_customdtype_complex():
"""
Test that `CustomDtype` doesn't brak is_imag
Expand Down