# "Cleverly" replace expression when simplify

I want to simplify $$5a\cdot x+5b\cdot y+6 c+c^2$$, if possible, given that $$e=a\cdot x+b\cdot y+c$$.

I don't want to replace $$c=e-a\cdot x-b\cdot y$$. I want to replace it "cleverly".

So, a nice simplification result can be $$5e+c+c^2$$.

Is this possible? and how?

• Use PolynomialReduce: In[53]:= PolynomialReduce[5*a*x + 5*b*y + 6*c + c^2, a*x + b*y + c - e, {x, y, a, b, c, d, e}][[2]] Out[53]= c + c^2 + 5 e Commented Mar 8, 2022 at 19:37

The second argument of FullSimplify can list known conditions, relationships, and assumptions:

FullSimplify[5 a x + 5 b y + 6 c + c^2, e == a x + b y + c]
(*    c + c^2 + 5 e    *)

• I get the same result using Simplify, i.e., Simplify[5 a*x + 5 b*y + 6 c + c^2, e == a*x + b*y + c] Commented Mar 8, 2022 at 1:23

Solve with elimination of ax or by or both together at the same time.

Solve[{5 ax + 5 by + 6 c + c^2 == f, e == ax + by + c}, f, {ax}]

(*   {{f -> c + c^2 + 5 e}}   *)

• Math representation is not so clear: $a x$ and $b y$ are not one variable but two: $a*x$ Commented Mar 7, 2022 at 20:29
• use $a~x$ to put space $a ~x$ Commented Mar 7, 2022 at 20:53
• @Chameleon Even better would be to post your formulas as code, not (just) as TeX. Makes it easy for others to help you: easier to copy/paste, fewer misinterpretations. Commented Mar 8, 2022 at 0:14
GroebnerBasis[{5*a*x + 5*b*y + 6*c + c^2, a*x + b*y + c - e}, {x, y,
a, b, c, d, e}]


{c + c^2 + 5 e, c - e + a x + b y}

• The answer depends on the ordering of the variables in the second argument of GroebnerBasis. For this method to work as requested, one would have to try all permutations of the variables and choose the minimal LeafCount: something like MinimalBy[GroebnerBasis[{5*a*x + 5*b*y + 6*c + c^2, a*x + b*y + c - e}, #][[1]] & /@ Permutations[{x, y, a, b, c, d, e}], LeafCount] // DeleteDuplicates. Commented Mar 8, 2022 at 21:00
• @Roman thanks for pointing this out. If you want to make it a fully-fledged answer, I am happy to delete mine
– user49048
Commented Mar 8, 2022 at 21:33
• @Roman The ordering used above is just fine for the task at hand. The problem is that this does not provide an indication of how the original polynomial transforms. Which is one reason I would instead advocate for using PolynomialReduce. Commented Mar 9, 2022 at 14:33