Skip to content Skip to sidebar Skip to footer

Is There A Way To Handle Constant Function Parameters In Sympy?

I am generating symbolic functions and using SymPy to simplify them. Now I would like a way to 'simplify' symbols that represent constant parameters in a function that is yet to b

Solution 1:

Perhaps something like this (applied to an expression that has been fully expanded):

defconsim(eq, *v):
    con = numbered_symbols('c', cls=Dummy)
    reps = {}
    for i in preorder_traversal(eq):
        if i.is_Mul or i.is_Add:
            c, d = i.as_independent(*v)
            if c != i.identity and c.free_symbols:
                c = reps.setdefault(c, next(con))
    return eq.subs(reps)


>>> from sympy.abc import a, b, c, d, x
>>> eq = 2*a*x**2 + b*c*x + d + e
>>> consim(eq, x)
         2
c₀ + c₁⋅x  + c₂⋅x

You probably want numbered symbols, not all symbols the same.

Post a Comment for "Is There A Way To Handle Constant Function Parameters In Sympy?"