Skip to content

Commit a3efacc

Browse files
isurufinducer
authored andcommitted
Convert negative exponentiation to positive + division
1 parent 82d6d43 commit a3efacc

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

sumpy/symbolic.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import numpy as np
3838
from pymbolic.mapper import IdentityMapper as IdentityMapperBase
3939
import pymbolic.primitives as prim
40+
import math
4041

4142
import logging
4243
logger = logging.getLogger(__name__)
@@ -111,6 +112,7 @@ def _find_symbolic_backend():
111112
Symbol = sym.Symbol
112113
Derivative = sym.Derivative
113114
Integer = sym.Integer
115+
Rational = sym.Rational
114116
Matrix = sym.Matrix
115117
Subs = sym.Subs
116118
I = sym.I # noqa: E741
@@ -293,6 +295,27 @@ def map_Symbol(self, expr): # noqa: N802
293295
return SpatialConstant.from_sympy(expr)
294296
return SympyToPymbolicMapperBase.map_Symbol(self, expr)
295297

298+
def map_Pow(self, expr): # noqa: N802
299+
if expr.exp == -1:
300+
return 1/self.rec(expr.base)
301+
else:
302+
return SympyToPymbolicMapperBase.map_Pow(self, expr)
303+
304+
def map_Mul(self, expr): # noqa: N802
305+
num_args = []
306+
den_args = []
307+
for child in expr.args:
308+
if isinstance(child, Pow) and isinstance(child.exp, Integer) \
309+
and child.exp < 0:
310+
den_args.append(self.rec(child.base)**(-self.rec(child.exp)))
311+
elif isinstance(child, Rational) and not isinstance(child, Integer):
312+
num_args.append(self.rec(child.p))
313+
den_args.append(self.rec(child.q))
314+
else:
315+
num_args.append(self.rec(child))
316+
317+
return math.prod(num_args) / math.prod(den_args)
318+
296319

297320
class PymbolicToSympyMapperWithSymbols(PymbolicToSympyMapper):
298321
def map_variable(self, expr):

0 commit comments

Comments
 (0)