Skip to content

Support python 3.13 in symbolic_formula #543

@joshua-cogliati-inl

Description

@joshua-cogliati-inl

In python 3.13, how variables in exec are handled has changed (see PEP 667 https://peps.python.org/pep-0667/ ). This means that the lines:

                exec(f"x{ii} = sympy.Symbol('x_{ii}')")
                exec(f"x.append(x{ii})")

in symbolic_formula in MultKAN.py can error out with an error like:

>>> model.symbolic_formula()
Traceback (most recent call last):
  File "<python-input-45>", line 1, in <module>
    model.symbolic_formula()
    ~~~~~~~~~~~~~~~~~~~~~~^^
  File "/Users/fred/pykan/kan/MultKAN.py", line 2277, in symbolic_formula
    exec(f"x.append(x{ii})")
    ~~~~^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 1, in <module>
NameError: name 'x1' is not defined

I suggest either using something like:

                new_var = sympy.Symbol(f'x_{ii}')
                x.append(new_var)

or

                new_var = f"x{ii}"
                sys._getframe().f_locals[new_var] = sympy.Symbol(f'x_{ii}')
                x.append(sys._getframe().f_locals[new_var])

instead of exec.

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