On Wed, Feb 8, 2012 at 2:16 PM, janwillem <jwevand...@xs4all.nl> wrote: > I would like to read a formula from text input and than process it > further with sympy. I did not find a elegant solution for that. What > works is the following work around using an intermediate script that > is imported. I thought there might be a simpler and more elegant way. > import sympy > import string > import re > splits = '[\]\[ ()/*-=><^:]' > #the formula that might come from text file or textbox of a gui > f = 'fx = (x-2 * z)/f' > lst = re.split(splits, f) > #make temp script to be imported > f_tmp = open('sympy_symbols.py', 'w') > f_tmp.write('import sympy\n') > for s in lst: > if s != '' and s[0] in string.letters: > f_tmp.write('%s = sympy.Symbol("%s")\n' % (s, s)) > f_tmp.write(f) > f_tmp.close() > #import temp script > from sympy_symbols import * > print fx > dfdx = sympy.diff(fx, x) > print dfdx > Although parsing of equations has not been implemented (though there is a small script attached that does this at http://code.google.com/p/sympy/issues/detail?id=2966 ) parsing of expressions can be done with sympify() (alias S()):
>>> fx = S( ' (x-2 * z)/f ' ) >>> fx.diff(x) 1/f If your expression has some other syntax (like that allowed in Mathematica) there is a mathematica parser which is demonstrated in the script of the issue cited above. Let us know if you have more questions. Chris -- You received this message because you are subscribed to the Google Groups "sympy" group. To post to this group, send email to sympy@googlegroups.com. To unsubscribe from this group, send email to sympy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sympy?hl=en.