You can make it into a SymPy expression with sympify
. This requires providing a dictionary so that things like add, mul, sub, div are interpreted correctly by SymPy:
locals = {
"add": Add,
"mul": Mul,
"sub": Lambda((x, y), x - y),
"div": Lambda((x, y), x/y)
}
sympify('sqrt(div(add(1.000, sub(div(sqrt(log(0.978)), X0), mul(-0.993, X0))), add(-0.583, 0.592)))', locals=locals)
This returns a SymPy expression, which prints as
sqrt(110.333333333333*X0 + 111.111111111111 + 16.5721799259414*I/X0)
The symbol X0 can be accessed as Symbol("X0")
. Or, which is a more robust approach, you can explicitly say what the symbols are, by creating them and adding them to the dictionary ahead of time.
X0 = symbols("X0")
locals = {
"add": Add,
"mul": Mul,
"sub": Lambda((x, y), x - y),
"div": Lambda((x, y), x/y),
"X0": X0
}
This is needed, for example, to parse I as a symbol "I" rather than "imaginary unit" as SymPy would do by default.
I'm not happy about the evaluation of sqrt(log(0.978))
. Although sympify
has option evaluate=False
, which prevents things like addition, it does not prevent functions with floating point arguments from being evaluated.