Skip to content

sbml_math_to_sympy fails on powers with a dimensionless numeric base and a symbolic exponent #50

Open

Description

@dweindl

sbml_math_to_sympy cannot parse <power/> with a numeric base that has sbml:units and a non-numeric exponent. set_math (with the default literals_dimensionless=True) writes exactly such MathML for, e.g., 10**x, so the output of set_math cannot be read back:

import libsbml
import sympy as sp
from sbmlmath import sbml_math_to_sympy, set_math

doc = libsbml.SBMLDocument(3, 2)
model = doc.createModel()
ia = model.createInitialAssignment()
ia.setSymbol("y")
set_math(ia, sp.Integer(10) ** sp.Symbol("x"))
print(libsbml.formulaToL3String(ia.getMath()))  # 10 dimensionless^x
sbml_math_to_sympy(ia)
  File ".../sbmlmath/mathml_parser.py", line 54, in _pow
  ...
  File ".../pint/util.py", line 650, in __pow__
TypeError: Cannot power UnitsContainer by <class 'sympy.core.symbol.Symbol'>

The same happens for 2.5**x. x**2 (units on the exponent) works.

The base is converted to a pint quantity, and pint can't raise units to a symbolic power. For a dimensionless base, the result is dimensionless, so the units could be dropped (or the magnitude used) before taking the power. For a base with other units and a non-numeric exponent, the units of the result are not defined; this could raise a clear error, or drop the units with a warning.

Workaround: write the MathML with SBMLMathMLPrinter(literals_dimensionless=False).

Found in libpetab-python (PEtab-dev/libpetab-python#560), where p = 10^log10_p initial assignments are written.

sbmlmath 0.4.1, pint 0.26.1, sympy 1.14.0, python-libsbml 5.21.2, Python 3.12.10

🤖 Generated with Claude Code

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions