|
37 | 37 | import numpy as np |
38 | 38 | from pymbolic.mapper import IdentityMapper as IdentityMapperBase |
39 | 39 | import pymbolic.primitives as prim |
| 40 | +import math |
40 | 41 |
|
41 | 42 | import logging |
42 | 43 | logger = logging.getLogger(__name__) |
@@ -111,6 +112,7 @@ def _find_symbolic_backend(): |
111 | 112 | Symbol = sym.Symbol |
112 | 113 | Derivative = sym.Derivative |
113 | 114 | Integer = sym.Integer |
| 115 | +Rational = sym.Rational |
114 | 116 | Matrix = sym.Matrix |
115 | 117 | Subs = sym.Subs |
116 | 118 | I = sym.I # noqa: E741 |
@@ -293,6 +295,27 @@ def map_Symbol(self, expr): # noqa: N802 |
293 | 295 | return SpatialConstant.from_sympy(expr) |
294 | 296 | return SympyToPymbolicMapperBase.map_Symbol(self, expr) |
295 | 297 |
|
| 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 | + |
296 | 319 |
|
297 | 320 | class PymbolicToSympyMapperWithSymbols(PymbolicToSympyMapper): |
298 | 321 | def map_variable(self, expr): |
|
0 commit comments