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
5 changes: 4 additions & 1 deletion petab/v1/math/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ def _print_Pow(self, expr: sp.Pow):
str_exp = self._print(exp)
if not base.is_Atom:
str_base = f"({str_base})"
if not exp.is_Atom:
# A non-integer Rational exponent (e.g. sqrt -> 1/2) is an Atom but prints as the
# multi-token "1/2", so without parentheses "x ^ 1/2" re-parses as (x^1)/2. The
# `not exp.is_Atom` check above (#421) misses it; parenthesize it explicitly.
if not exp.is_Atom or (exp.is_Rational and not exp.is_Integer):
str_exp = f"({str_exp})"
return f"{str_base} ^ {str_exp}"

Expand Down
4 changes: 4 additions & 0 deletions tests/v1/math/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ def test_printer():
assert petab_math_str(BooleanTrue()) == "true"
assert petab_math_str(BooleanFalse()) == "false"
assert petab_math_str((a + b) ** (c + d)) == "(a + b) ^ (c + d)"
# A non-integer rational exponent must be parenthesized, else "a ^ 1/2" re-parses as
# (a^1)/2 (i.e. sqrt(a) would round-trip to a/2). See #421 for the base/exponent case.
assert petab_math_str(sp.sqrt(a)) == "a ^ (1/2)"
assert petab_math_str(a ** sp.Rational(2, 3)) == "a ^ (2/3)"


def read_cases():
Expand Down