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) + "')")
Then the Python variable "F" is associated with the Sympy Symbol "F"
>>> F
F
>>> type(F)
<class 'sympy.core.symbol.Symbol'>
For the relevance of this thread, see
https://groups.google.com/d/msg/sympy/_RnbbOqhERM/YAoJAbyPAgAJ
No comments:
Post a Comment