Skip to content

Fix PetabStrPrinter for non-integer rational exponents#489

Open
wshlavacek wants to merge 1 commit into
PEtab-dev:mainfrom
wshlavacek:fix/petabstrprinter-rational-exponent
Open

Fix PetabStrPrinter for non-integer rational exponents#489
wshlavacek wants to merge 1 commit into
PEtab-dev:mainfrom
wshlavacek:fix/petabstrprinter-rational-exponent

Conversation

@wshlavacek

Copy link
Copy Markdown

petab_math_str(sympify_petab(x)) is not the identity for square roots and other non-integer rational exponents. For example, petab_math_str(sqrt(a)) returns a ^ 1/2, which sympify_petab re-parses as (a^1)/2 = a/2. This is a silent mangling of the expression.

The cause is that a non-integer Rational exponent, such as 1/2, is a sympy Atom but prints as the multi-token string 1/2, so the not exp.is_Atom guard added in #421 does not parenthesize it. Since ^ binds tighter than /, the unparenthesized a ^ 1/2 then parses as (a^1)/2.

This fix parenthesizes a non-integer rational exponent explicitly, so petab_math_str(sqrt(a)) == "a ^ (1/2)", which re-parses correctly. Integer powers and the non-atomic base/exponent cases from #421 are unchanged.

A non-integer Rational exponent (e.g. a square root, exponent 1/2) is a sympy
Atom but prints as the multi-token "1/2", so PetabStrPrinter emitted
`sqrt(a)` as the unparenthesized `a ^ 1/2`. Since `^` binds tighter than `/`,
that re-parses as `(a^1)/2 = a/2` -- a silent round-trip corruption
(`petab_math_str(sympify_petab(...))` is not the identity for square roots).

The `not exp.is_Atom` guard added in PEtab-dev#421 covers non-atomic exponents but not
this atomic-yet-multi-token case; parenthesize a non-integer rational exponent
explicitly, so `petab_math_str(sqrt(a)) == "a ^ (1/2)"`, which re-parses
correctly. Integer powers and the PEtab-dev#421 cases are unchanged.
@wshlavacek wshlavacek requested a review from a team as a code owner June 20, 2026 18:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant