Skip to content

Commit f636167

Browse files
authored
Fix PetabStrPrinter for powers (#421)
Power expressions with non-atomic bases or exponents were not printed correctly. Fixed here.
1 parent 5e26769 commit f636167

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

petab/v1/math/printer.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,13 @@ def _print_BooleanFalse(self, expr):
3737
def _print_Pow(self, expr: sp.Pow):
3838
"""Custom printing for the power operator"""
3939
base, exp = expr.as_base_exp()
40-
return f"{self._print(base)} ^ {self._print(exp)}"
40+
str_base = self._print(base)
41+
str_exp = self._print(exp)
42+
if not base.is_Atom:
43+
str_base = f"({str_base})"
44+
if not exp.is_Atom:
45+
str_exp = f"({str_exp})"
46+
return f"{str_base} ^ {str_exp}"
4147

4248
def _print_Infinity(self, expr):
4349
"""Custom printing for infinity"""

tests/v1/math/test_math.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ def test_assumptions():
3838

3939

4040
def test_printer():
41+
a, b, c, d = sp.symbols("a b c d", real=True)
4142
assert petab_math_str(None) == ""
4243
assert petab_math_str(BooleanTrue()) == "true"
4344
assert petab_math_str(BooleanFalse()) == "false"
45+
assert petab_math_str((a + b) ** (c + d)) == "(a + b) ^ (c + d)"
4446

4547

4648
def read_cases():

0 commit comments

Comments
 (0)