Search code examples
pythonreplacesympysymbolic-mathmathematical-expressions

Python: replace function by its expression, in mathematical expression as string, with Sympy?


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))


Solution

  • You can use a re.sub:

    import re
    
    strip_f = lambda f: (f.split("(")[0], f.split("(")[1][:-1].split(", "))
    
    def explicit(expr, functions):
        d = {}
        for f in functions:
            func_vars, func_def = f.split(" = ")
            func_name, func_vars = strip_f(func_vars)
            d[func_name] = (func_vars, func_def)
    
        def replace(match):
            m = match.groups()[0]
            fn_name, expr_vars = strip_f(m)
            func_vars, s = d[fn_name]
            for fv, ev in zip(func_vars, expr_vars):
                s = s.replace(fv, "("+ev+")")
            s = "("+s+")"
            return s
        return re.sub(r"(.\(([^\)]+,?)+?\))", replace, expr)
    
    
    expr = '2*a*b + f(b+a, 2*b) + g(5, 2*c, 3+a)'
    f1 = "f(x, y) = x + 2*y + 3"
    f2 = "g(x, y, z) = x + y + z - 2"
    
    print(explicit(expr, [f1, f2]))
    

    Displays:

    2*a*b + ((b+a) + 2*(2*b) + 3) + ((5) + (2*c) + (3+a) - 2)
    

    The regex, broken down:

    (                   begin capturing group for function
       .                match any character (function nname)
       \(               match open parenthesis
       (                begin capturing group for variable
         [^\)]+         match at least one non-parenthesis character
         ,?             match a comma if it's there
       )                end variable capturing 
       +?               match at least one variable
       \)               match close parenthesis
    )                   end capturing group
    

    If you don't mind if the output is simplified, you can use the sympy methods that you mentioned:

    import sympy
    
    expr = '2*a*b + f(b+a, 2*b) + g(5, 2*c, 3+a)'
    f1 = "f(x, y) = x + 2*y + 3"
    f2 = "g(x, y, z) = x + y + z - 2"
    
    def split(fn):
        return str(fn).partition("(")
    
    def check_fn(ex):
        name, sep, rest = split(ex)
        return name and sep
    
    def parse_functions(functions):
        fns = {}
        for f in functions:
            name, _, rest = split(f)
            fn = rest.split(" = ")
            fns[name] = fn[0][:-1].split(", "), fn[1]
        return fns
    
    def expand(expr, functions):
        fns = parse_functions(functions)
    
        def sub_fn(ex):
            with sympy.evaluate(False):
                vs, fn = fns[split(ex)[0]]
                fn = sympy.UnevaluatedExpr(sympy.sympify(fn))
                return fn.subs(dict(zip(vs, str(ex)[2:-1].split(", "))))
    
        return sympy.sympify(expr).replace(check_fn, sub_fn)
    
    print(sympy.StrPrinter({'order':'none'})._print(expand(expr, [f1, f2])))
    

    Displays:

    2*a*b + a + b + 2*(2*b) + 3 + 5 + 2*c + a + 3 - 2
    

    Note that this assumes that you want the full, unsimplified, unordered equation.