# Move variables to one side of equation

I have a bunch of linear equations of the form

x[1] == 5012 - 5x[3] - 2x[4] + 5x[7]


etc. Specifically there is one linear variable on the left, and the right-hand side is linear (generically with a constant).

I want to process these so that all the variables come on the left and the constant stays on the right. So for the above I would want

x[1] + 5x[3] + 2x[4] - 5x[7] == 5012


as the output. This feels like a very simple thing to try and do but I am struggling to implement it.

Try

eq = x[1] == 5012 - 5 x[3] - 2 x[4] + 5 x[7]
expr = eq /. Equal -> Subtract;
const = expr /. Map[# -> 0 &,  Variables[expr]]
neweq=expr - const == -const
(*x[1] + 5 x[3] + 2 x[4] - 5 x[7] == 5012*)

• Thank you -- this does just what I needed Commented Sep 15, 2023 at 13:26
• You're welcome! Commented Sep 15, 2023 at 13:27
tran = SubtractSides[SubtractSides[#], First@CoefficientArrays[#]] &;

eqn=x[1] == 5012 - 5x[3] - 2x[4] + 5x[7];
tran[eqn]


x[1] + 5 x[3] + 2 x[4] - 5 x[7] == 5012

eqns = {x[1] == 5012 - 5 x[3] - 2 x[4] + 5 x[7],
x[1] + 2 x[4] == -5 x[3] + 5 x[7] + 5012,
5012 == x[1] + 5 x[3] + 2 x[4] - 5 x[7]};
tran/@eqns


{x[1] + 5 x[3] + 2 x[4] - 5 x[7] == 5012, x[1] + 5 x[3] + 2 x[4] - 5 x[7] == 5012, -x[1] - 5 x[3] - 2 x[4] + 5 x[7] == -5012}

eq = x[1] == 5012 - 5 x[3] - 2 x[4] + 5 x[7]
vars = Variables[Subtract @@ eq]
ca = CoefficientArrays[eq] // Normal
vars .  Last[ca] == First@ca


x[1] + 5 x[3] + 2 x[4] - 5 x[7] == -5012

Here is yet another way. Check if the expression is of type Plus or not. If type Plus, loop over each term and move those to the right that do not depend on the variable. If not type Plus, do nothing.

ClearAll["Global*"];
adjust[eq_Equal] := Module[{expr, vars, lhs = 0, rhs = 0, n},
expr = First@SubtractSides[eq];
vars = Variables[expr];
Do[
If[InternalDependsOnQ[expr[[n]], vars],
lhs = lhs + expr[[n]]
,
rhs = rhs - expr[[n]]
]
,
{n, 1, Length@expr}
];
lhs == rhs
,
expr == 0
]
]


Now

 adjust[x[1] == 5012 - 5 x[3] - 2 x[4] + 5 x[7]]


adjust[x[1] + 34 + y + z == -300]


adjust[x[1] + 34 == -300 + Log[z] - Pi/2]