working with Sympy symbols extracted from a Latex expression
Published 2020-05-28T02:13:00.001Z by Physics Derivation Graph
I'm using SymPy to work with Latex expressions >>> import sympy >>> sympy.__version__ '1.5.1'
I can convert Latex to SymPy using >>> from sympy.parsing.latex import parse_latex >>> eq = parse_latex("F = m a") >>> eq.rhs a*m
However, to work with the symbols in SymPy I need to extract them from the expression >>> set_of_symbols_in_eq = eq.free_symbols >>> set_of_symbols_in_eq {a, F, m}
The entries exist in the set >>> type(list(set_of_symbols_in_eq)[0]) <class 'sympy.core.symbol.Symbol'>
but they are not defined as Python variables >>> F Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'F' is not defined
To associate a Python variable with each symbol, I used >>> for symb in set_of_symbols_in_eq: ... exec(str(symb) + " = sympy.symbols('" + str(symb) + "')")