# How to solve an equation on a given relation

I was wondering whether it was possible to solve a linear equation in two unknowns for a specified relation of the two unknowns, meaning that, given $$x+y=\alpha x+\beta y+\gamma$$ I want to have $$x+y$$ on one side and everything else on the other side. Here, $$\alpha,\beta,\gamma$$ do not depennd on $$x$$ and $$y$$, in fact, they can be treated as constants. I tried using Solve and Collect but it wouldn't give me what I wanted. For example

Collect[x + y == 3 + 2 x + 2 y + 38/a, {x + y}]


and many variations did not work. I also tried

Solve[x + y == 3 + 2 x + 2 y + 38/a, {x,y}]


which somewhat gives me what I want, although I am looking for $$x+y$$ on one side. I do not know if this would work for harder equations, as I chose the above equation just for simplicity. Does anyone have any idea? Is there a function that achieves this?

• You can introduce a new variable $z=x+y$. Next you replace $y$ with $z-x$. Then you can solve for $z$ in terms of $x$. Feb 8, 2020 at 7:16

## 3 Answers

This works for the example you posted. If you have a different example, where this does not work, will try to fix it. This checks that the combination of a x + b y can factor out to a common x+y term, otherwise it returns no solution.

solve[x_, y_, eq0_] := Module[{lhs, rhs, eq, cx, cy, sol = {}, z},
rhs = eq0 /. (lhs_ == rhs_) :> rhs;
lhs = eq0 /. (lhs_ == rhs_) :> lhs;
eq = rhs - lhs == 0;
cx = Last@CoefficientList[rhs - lhs, x];
cy = Last@CoefficientList[rhs - lhs, y];
If[cx == cy,
sol = (x + y) /. (First@Solve[(eq /. (cx x + cy y) -> z), z] /. z -> ( x + y));
sol = sol/cx
];
x + y == sol
];


Now call it as

ClearAll[x, y, a];
eq = x + y == 3 + 2 x + 2 y + 38/a;
solve[x, y, eq]


eq = x + y == 3 + 4 x + 4 y + 38/a;
solve[x, y, eq]


eq = 2 x + y == 3 - 2 x - 3 y + 38/a;
solve[x, y, eq]


This returns no solution, since can't collect x+y into one term

eq = 2 x + y == 3 - 2 x - 1 y + 38/a;
solve[x, y, eq]


To solve for x+y you can simply do:

x + y /. Solve[x + y == 3 + 2 x + 2 y + 38/a, {x, y}][[1]]


-((38 + 3 a)/a)

To get an expression of the form x + y == ... you can use Reduce and post-process the output into desired form:

red = Reduce[{z == 3 + 2 x + 2 y + 38/a, z == x + y, a != 0}, z, {x, y}]


a != 0 && z == (-38 - 3 a)/a

red /. { a != 0 -> True, z -> x + y}


x + y == (-38 - 3 a)/a

Eliminate[z == x + y == 3 + 2 x + 2 y + 38/a, {x, y}]


results in

z == -3 - 38/a && a != 0