'Sympy rewrite expression to template

If I have an expression such as c1 / (c2*s + c3) I would like sympy to transform the expression to a template looking like C1 / (s + C2) such that C1 = c1/c2 and C2 = c3/c2.

Is there an easy way to do that?



Solution 1:[1]

So you are asking "What must C1 and C2 be to make this true?" solve can answer that question:

>>> v = var('c1:4 C1:3 s')
>>> expr = c1 / (c2*s + c3)
>>> tmpl = C1 / (s + C2)
>>> t = tmpl.free_symbols - expr.free_symbols
>>> solve(expr - tmpl, t, dict=True)
[{C1: c1/c2, C2: c3/c2}]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 smichr