Take the equation $f(x,y,z) = x y^2 + (1-x)(y-z)^2$. I want to rewrite this in the form $(y+a(x,z))^2 + b(x,z)$. This can easily be solved by hand, with $a(x,z) = - z(1-x)$ and $b(x,z) = x(1-x)z^2$. The actual problem I want to solve is more complicated than this and can't as easily be done by hand, but the essence of the problem should be the same - I want Mathematica to solve the equation

$$x y^2 + (1-x)(y-z)^2 = (y+a)^2 + b$$

for $a$ and $b$, with the constraint that $a$ and $b$ can only be functions of $x$ and $z$. Is there any way of imposing this condition? Simply plugging in the equation into Solve,

Solve[x*y^2 + (1 - x) ((y + z)^2) == (y + a)^2 + b, {a, b}]

obviously does not work, since the equation by itself is not sufficiently constrained. I tried replacing a with a[x,z] and likewise for b, but Mathematica didn't know what to do with that and just treated a[x,z] as a variable.


2 Answers 2


The following seems to work

expr1 = x y^2 + (1 - x) (y - z)^2;

expr2 = (y + a)^2 + b;

The following is inspired by the documentation for SolveAlways

eqn = 
 LogicalExpand[¬ Eliminate[¬ (expr1 == expr2), y]]
(* -2 a - 2 z + 2 x z == 0 && -a^2 - b + z^2 - x z^2 == 0 *)

Solve[eqn, {a, b}]
(* {{a -> (-1 + x) z, b -> x z^2 - x^2 z^2}} *)
  • $\begingroup$ Thanks a lot, this seems like the right approach! Unfortunately does not work on my more complicated problem, but this may very well be because a solution does not exist...will think about it more. $\endgroup$ May 27, 2021 at 18:26
  • $\begingroup$ @HenryShackleton you might try substituting numerical values for y and see if your equations are consistent. $\endgroup$
    – mikado
    May 27, 2021 at 21:33

Indeed, you can use SolveAlways to get a solution, if it exists. Hoever, to understand if a solution exists, you can try an approach that requires a few more lines of code and gives a little more insighy on what you are doing.

You are dealing with polynomial equation. There is a solution only if you can have all coefficient of the $y$ variable to be equal on both sides. There are many ways to do that, what you need to know is that 1.The polynomials need to have same degree in y. So if the RHS has degree 2 like in your example, then also f must have degree 2 in y. 2. If the degree is n in general, you get n+1 equations giving constraints, so you will need at least n+1 parameters.

In your example, $Deg f =2$ and you have 2 parameters, the thing still works because even tough you are hard-fixing one coefficient to be 1 in the RHS, that is indeed the right value to get a solution. If you were to choose $2(y-a)^2+b$ you would not have obtained a solution. If you post the more complicated function, or at least it degree in y, we could tell more.

Possible ways to get $n+1$ independent equations for the parameters:

  • Set the $i$-th derivative in y=0 to be euqal for f and the RHS, for $0\le i \le n$
  • Set $f$ and the RHS to be equal for $n+1$ consecutive integer values of $y$, like $y=0,1,2,...,n$.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.