I have parsed a LaTeX equation in Sympy, but now I need to extract the variables to symbols so that I can have Sympy solve/rearrange the equation for specific variables.
I know I can use equation.free_symbols
to see the symbols contained in the equation, but how do I set those as symbols (automatically) so that I can specify them for the symbolic solution, i.e. solve(equation,C_L)
?
from sympy.parsing.latex import parse_latex
from sympy import init_printing
from sympy.solvers import solve
from sympy import symbols
init_printing()
equation = parse_latex(r"""
S_{\TO} = \frac{1}{\frac{2g*\rho}{2\left( \WoS \right)}(\mu C_{\L}-C_{\D_{0}}-KC_{\L}^2)}\ln(\frac{(\ToW)-\mu + (\frac{\rho}{2(\WoS)}(\mu C_{\L}-C_{\D_{0}}-KC_{\L}^2))V_{\f}^{2}}{(\ToW)-\mu + (\frac{\rho}{2(\WoS)}(\mu C_{\L}-C_{\D_{0}}-KC_{\L}^{2}))V_{\i}^{2}} )
+ \frac{V_{\TR}^{2}}{0.2g}\sin(\sin^{-1}((\ToW)-\frac{1}{(\LoD)}))
+ \frac{h_{\obstacle}-\frac{V_{\TR}^{2}}{0.2g}(1-\cos(\gamma_{\climb}))}{\tan(\gamma_{\climb})}
""")
test = (equation.free_symbols)
The var
function will inject the symbols it creates from a string into the workspace. So, for example, you can do:
>>> from sympy import *
>>> syms = lambda eq: tuple(ordered(var(','.join(str(i) for i in eq.free_symbols))))
>>> eq = S('x_a + Cb_1')
>>> eq.has(x_a) # not yet defined
...
NameError: name 'x_a' is not defined
>>> syms(eq) # now x_a and Cb_1 are defined
(Cb_1, x_a)
>>> eq.has(x_a)
True
>>> esyms = syms(equation)
>>> sol = solve(equation, C_L)