I have a function definition, as a string, say func_def = "f(x, y) = x + 2*y + 3"
.
I have a mathematical expression, as a string, including that function, say
expr = '2*a*b + f(b+a, 2*b)'
.
How can I replace, in my expression, the function by its value?
In other words, how do I get a string expr_eval = "2*a*b + ( (b+a) + 2*(2*b) + 3 )"
?
Is there an easy solution for this, for example with Sympy using a combination of sympify
and subs
and some other function? I feel there should be one, but can't find it. I have to do this for quite a few equations containing many symbols, so that creating Sympy symbols for each of them separately does not seem like a great option.
For now, I use regex, but I find this solution complicated to come up with, to generalise (other functions with other numbers of variables), to scale, and to read. It goes like this:
import re
func_def = "f(x, y) = x + 2*y + 3"
expr = '2*a*b + f(b+a, 2*b)'
# extract substring with function
str_to_replace = re.findall(r'f\([a-zA-z0-9\*\+\"\']*, [a-zA-z0-9\*\+\"\'/]*\)', expr)
str_to_replace = str_to_replace[0]
# extract function name and arguments in substring
func_name, args, _ = re.split('\(|\)', str_to_replace)
args = args.split(',')
args = [i.strip() for i in args]
# parse the function definition
func_def_lhs, func_def_rhs = func_def.split('=')
func_def_name, args_def, _ = re.split('\(|\)', func_def_lhs)
args_def = args_def.split(',')
args_def = [i.strip() for i in args_def]
# replace each argument in the definition by its expression
for i, arg_def in enumerate(args_def) :
func_def_rhs = func_def_rhs.replace(arg_def, '({})' .format(args[i]))
expr_eval = expr.replace(str_to_replace, '({})' .format(func_def_rhs))