# Replacing in a complicate expression [duplicate]

I have a complicate expression like this

2 a g12 (b (-1 + b c) g12^2 + b (1 - b c) g11 g22 + c g22^2) +
g22 ((-1 + b^2 c^2) g12^2 + c^2 g22 (-b^2 g11 + g22)) +
a^2 (g12^2 g22 + b^2 g11 (g12^2 - g11 g22))


and would like to expand it and replace g11 a^2 + 2 g12 a c + g22 c^2 with g11. Tried out Expand, Replace, Collect, Sort, etc. but it didn't work. Can anyone help?

## marked as duplicate by Daniel Lichtblau, Coolwater, m_goldberg, Kuba♦Jan 19 '18 at 6:33

• Maybe In[2980]:= PolynomialReduce[ 2 a g12 (b (-1 + b c) g12^2 + b (1 - b c) g11 g22 + c g22^2) + g22 ((-1 + b^2 c^2) g12^2 + c^2 g22 (-b^2 g11 + g22)) + a^2 (g12^2 g22 + b^2 g11 (g12^2 - g11 g22)), g11 a^2 + 2 g12 a c + g22 c^2 - g11, Join[Complement[Variables[eee], {g11}], {g11}]][[2]] Out[2980]= b^2 g11 g12^2 - 2 a b g12^3 - b^2 g11^2 g22 + 2 a b g11 g12 g22 - g12^2 g22 + a^2 g12^2 g22 + 2 a c g12 g22^2 + c^2 g22^3 ? – Daniel Lichtblau Jan 18 '18 at 17:44
• This sort of thing probably qualifies as an FAQ on this site, by the way. – Daniel Lichtblau Jan 18 '18 at 17:44

Is this what you mean?

expr = 2 a g12 (b (-1 + b c) g12^2 + b (1 - b c) g11 g22 + c g22^2) +
g22 ((-1 + b^2 c^2) g12^2 + c^2 g22 (-b^2 g11 + g22)) +
a^2 (g12^2 g22 + b^2 g11 (g12^2 - g11 g22));
rep = g11 a^2 + 2 g12 a c + g22 c^2;
expr = Expand[expr]


And now

Simplify[expr,rep==g11]


• I don't think you need expr = Expand[expr], do you? – anderstood Jan 18 '18 at 16:39
• @anderstood no, ofcourse not. But I just wanted to show the expression when expanded on the screen, since OP said he wanted to expand it. But internally, it work without explicit expand ofcourse. Thanks. – Nasser Jan 18 '18 at 16:45
• Maybe it is what I'm looking for but I'm not sure... Is it possible to see, before it simplifies, how it explicitly collects the expression g11 a^2 + 2 g12 a c + g22 c^2 on one side (or in a parentheses) and only later replaces it? – Mark Jan 18 '18 at 17:29

You could try Eliminate to eliminate either g22 or g12:

Eliminate[{
eq1 == 2 a g12 (b (-1 + b c) g12^2 + b (1 - b c) g11 g22 +
c g22^2) + g22 ((-1 + b^2 c^2) g12^2 + c^2 g22 (-b^2 g11 + g22)) +
a^2 (g12^2 g22 + b^2 g11 (g12^2 - g11 g22)),
g11 a^2 + 2 g12 a c + g22 c^2 == g11
},
g22
]


If necessary you can then use Solve and Simplify to solve for the temporary variable eq1:

Simplify[eq1 /. First @ Solve[%, eq1]]