# How to collect/reduce with respect to multiple variables?

I have this code:

a == b + c + d - e


Is it possible to collect or reduce this expression into this:

c + d == a + e - b


where I focus on the left side of the equation.

I am only aware of ways to reduce an expression with respect to one single variable, but here I would like to reduce it with respect to (c + d).

Is this possible somehow?

• Are you only interested in this simple linear case? Perhaps you should give a more involved example, otherwise many answers will only deal with the trivial case... I don't believe there's a solution for reducing anything to $G(c,d) = H(a,b,e)$ starting from $F(a,b,c,d,e)=0$, but if these functions are polynomials then maybe yes. – Szabolcs May 12 '13 at 18:47

c + d + # & /@ (Subtract @@ (a == b + c + d - e) == 0) // Reverse

a == b + c + d - e /. a == c + d + x_ -> c + d == a - x

With[{t = c + d},
Reduce[a == b + c + d - e /. t -> x, x] /. x -> t]

With[{t = c + d},
Reduce[Eliminate[a == b + c + d - e && t == x, {c, d}], x] /. x -> t]

(*c + d == a - b + e*)
(*c + d == a - b + e*)
(*c + d == a - b + e*)
(*c + d == a - b + e*)


Subtract @@ (a - d == b + c - e) == 0 /.  x_ - (c + d) == 0 -> c + d == x
With[{t = c + d},
Reduce[-Subtract @@ (a - d == b + c - e) == 0 /. t -> x, x] /.  x -> t]

• But the first two solutions won't work if the equation is something like a - d == b + c - e. – xzczd May 13 '13 at 6:24
• @xzczd, Updated. – chyanog May 13 '13 at 7:36
• +1 for the first one, which can avoid possible duplication of the name of the variables. – xzczd May 13 '13 at 10:43

This is easier to do with Map, rather than with Reduce. Like this:

eq = a == b + c + d - e;
Map[Subtract[#, b - e] &, eq]


or like this:

Map[Plus[#, -b + e] &, eq]


In both cases the result is the desired one:

a - b + e == c + d

The combination of Reduce and ReplaceAll may help:

Reduce[a == b + c + d - e /. c -> aa - d, aa] /. aa -> c + d
(* c + d == a - b + e *)


There's a defection in the solution above: it won't work correctly if the original equation contains the variable aa, of course in most cases we can avoid the duplication of name manually without much effort, but here I'd like to add a safer one:

Reduce[a == b + c + d - e /. c -> # - d, #] /. # -> c + d &@Unique["a"]
(* c + d == a - b + e *)


Unique is used to create a symbol that has never been used. This solution will work even if a has got an value:

a = 1;
Reduce[a == b + c + d - e /. c -> # - d, #] /. # -> c + d &@Unique["a"]
(* c + d == 1 - b + e *)


Let's make it into a funtion:

newReduce[x_Equal, y_] := Reduce[x /. Solve[y == #, Cases[y, _Symbol, {-1}, 1]], #]
/. # -> y &@Unique["a"]


The first argument of newReduce is the equation, and the second one is the "multiple variables", for example:

Clear[a]
newReduce[a == b + c + d - e, c + d]
(* c + d == a - b + e *)

a = 2;
newReduce[x + r y + z == d a + 2 u - op/3, x + y + z]
(* x + y + z == 1/3 (6 d - op + 6 u + 3 y - 3 r y) *)

newReduce[x + r /y + z == d /a + 2 u - op/3, x y + y z]
(* x y + y z == 1/6 (-6 r + 3 d y - 2 op y + 12 u y) && y != 0 *)

newReduce[x + r /y + z == d a + 2 u - op/3, x y z]
(* x y z == -(1/3) z (3 r - 6 d y + op y - 6 u y + 3 y z) && y z != 0 *)


As you see, now it works not only for the "multiple variables" with the head Plus.