Feb-12-2019, 06:28 AM
Hello! I have some equations which I read from a file, and then turn into sympy expressions and I need the variables that appear in each equation. However, for the purpose of my project, I need them in the order in which they appear in the equation. So if the equation is 'a+b*c' I need a program which gives me: a,b,c but not a,c,b or any other combination. Here is what I tried, based on what I found online:
x1**(-1.0)*x2 {x1, x2}
x1**(-1.0)*x2 {x1, x2}
x1**(-1.0)*x2 {x2, x1}
x1**(-1.0)*x2 {x2, x1}
x1**(-1.0)*x2 {x2, x1}
x1**(-1.0)*x2 {x2, x1}
x1**(-1.0)*x2 {x1, x2}
So the equation looks fine, but the order of the variables seem to change randomly and this makes it useless for what I need. Can someone tell me how to pick the variables in the order I want (actually any order would be good as long as it doesn't randomly change)? Thank you!
from sympy import * from sympy.parsing.sympy_parser import parse_expr eq1 = "x1**(-1.0)*x2" # I normally read this from a file eq1 = parse_expr(eq1) print(eq1,eq1.free_symbols)So, normally, eq1.free_symbols should give me a set of the variables in my expression. However after I ran this code (and other equations, too, with the same problem) several times, I got this output:
x1**(-1.0)*x2 {x1, x2}
x1**(-1.0)*x2 {x1, x2}
x1**(-1.0)*x2 {x2, x1}
x1**(-1.0)*x2 {x2, x1}
x1**(-1.0)*x2 {x2, x1}
x1**(-1.0)*x2 {x2, x1}
x1**(-1.0)*x2 {x1, x2}
So the equation looks fine, but the order of the variables seem to change randomly and this makes it useless for what I need. Can someone tell me how to pick the variables in the order I want (actually any order would be good as long as it doesn't randomly change)? Thank you!