1

I would like to use sympy to locate and group instances of a given symbolic expression within a set of expressions, so that the given expression may be replaced by a variable. For instance, with expressions like

a + 2 * x * y + b * x + 2 * x * z

I would like to group factors of an expression such as y + z, to obtain

a + b * x + 2 * x * (y + z)

and then substitute symbol w for y + z:

a + b * x + 2 * x * w

What is the proper way to achieve that?

amWhy
  • 209,954
  • 1
    isn't this a programming problem? – Siong Thye Goh Mar 24 '22 at 14:37
  • I hope SymPy can do the grouping for me, without substantial programming effort. I have tried replace(), but it doesn't seem very good at finding multi-term expressions. It works if the exact expression is already grouped within the input expression. – Steve White Mar 25 '22 at 17:39

0 Answers0